首页 > 技术文章 > centos7安装my sql 5.7

sichenzhao 2018-06-04 14:14 原文

如果直接运行: sudo yum install sql-server, 会导致报错,找不到该sql,因CentOS已经使用MariaDB替代了MySQL,如直接输入“yum install mysql-server”则安装MariaDB。

因此需要通过手动安装my sql 的rpm包来实现安装:

首先下载my sql的源:

wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm

然后在当前目录下可以看到rpm包下载完毕。

接着安装该rpm:

sudo rpm -ivh mysql57-community-release-el7-8.noarch.rpm

my sql的rpm安装好后,安装my sql:

sudo rpm -ivh mysql57-community-release-el7-8.noarch.rpm

期间会有需要依赖包的安装,全部点y就行。

安装好后,启动my sql服务:

sudo service mysqld start

到此就安装好了my sql,然后对其进行配置:

首先以root用户身份进入my sql,root身份是my sql默认的用户:

mysql -uroot -p

然后会弹出让输入密码,一般密码都是root,若不对,则如下查看密码:

cat /var/log/mysqld.log  | grep password

其中A temporary password is generated for root@localhost: bGlY?13TtFyX–:后面的就是root的初始密码。然后我们对密码进行修改,修改为我们易记的:

设置密码的等级和长度:4

set global validate_password_policy=0;
set global validate_password_length=4;

设置密码为root:

alter user 'root'@'localhost' identified by 'root';

但要注意,密码等级为0和长度为4其实不太安全,my sql为了安全考虑,这次的set和密码生效后,不会保存set设置,也就是下次重新启动my sql并修改密码时,默认的policy和length还是1和8。

安全策略值可以通过以下查看:

SHOW VARIABLES LIKE 'validate_password%';

默认策略为1也就是meduim,长度为8.

因此,如果要修改密码为4位,必须每次修改前都要先修改其策略。


在mysql -uroot -p命令输入后,输入秘密后,可能出现如下错误:

RROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

这是因为/var/lib/mysql文件夹的权限问题,将该目录的用户和所有组改为mysql即可:

sudo chown -R mysql:mysql /var/lib/mysql


自此,my sql安装完毕:

[c@localhost ~]$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

推荐阅读