在CentOS7中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。
1. 下载并安装MySQL官方的 Yum Repository
# wget -i -c http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
# 具体版本可以在官网查找
http://repo.mysql.com/
使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。
Installed:
mysql-community-libs.x86_64 0:5.7.24-1.el7 mysql-community-libs-compat.x86_64 0:5.7.24-1.el7
mysql-community-server.x86_64 0:5.7.24-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.24-1.el7 mysql-community-common.x86_64 0:5.7.24-1.el7
Replaced:
mariadb-libs.x86_64 1:5.5.56-2.el7
# systemctl start mysqld.service
# systemctl status mysqld.service
service mysqld start
service mysqld stop
service mysqld restart
service mysqld status
systemctl start mysqld
service mysqld stop
service mysqld restart
systemctl status mysqld
此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:
# grep "password" /var/log/mysqld.log
# mysql -uroot -p # 回车后会提示输入密码
输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
这里有个问题,新密码设置的时候如果设置的过于简单会报错:
原因是因为MySQL有密码设置的规范,具体是与validate_password_policy的值有关:
MySQL完整的初始密码规则可以通过如下命令查看:
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 4 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+-------+
密码的长度是由validate_password_length决定的,而validate_password_length的计算公式是:
validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'z?guwrBhH7p>';
Query OK, 0 rows affected (0.00 sec)
mysql>
这时候我们要把密码规则改一下,执行下面sql就可以了:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
设置之后就是我上面查出来的那几个值了,此时密码就可以设置的很简单,例如1234之类的。到此数据库的密码设置就完成了。
但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:
# yum -y remove mysql57-community-release-el7-10.noarch
可视化工具的登录授权:(如果授权不成功,请查看防火墙)
操作完成上面的,现在还不能用可视化的客户端进行连接,需要我们进行授权:
1grant all on *.* to root@'%' identified by '数据库密码';
设置用户 root 可以在任意 IP 下被访问:
grant all privileges on *.* to root@"%" identified by "new password";
设置用户 root 可以在本地被访问:
grant all privileges on *.* to root@"localhost" identified by "new password";
刷新权限使之生效:
flush privileges;
mysql> grant all privileges on mq.* to test@localhost identified by '1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
否则登录时提示:ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES )
update mysql.user set authentication_string=password("L_monite") where User="l_m" and Host="%";
参考:https://www.cnblogs.com/jorzy/p/8455519.html
http://www.cnblogs.com/brianzhu/p/8575243.html