在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。
(1)下载并安装MySQL官方的 Yum Repository
[root@localhost mysql]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。
[root@localhost mysql]# yum -y install mysql57-community-release-el7-10.noarch.rpm
之后就开始安装MySQL服务器
[root@localhost mysql]# yum -y install mysql-community-server
这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。
至此MySQL就安装完成了,然后是对MySQL的一些设置。
(2)MySQL数据库设置
首先启动MySQL
[root@localhost mysql]# systemctl start mysqld.service
查看MySQL运行状态,运行状态如图:
此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:
[root@localhost mysql]# grep "password" /var/log/mysqld.log
2020-01-11T19:19:11.869507Z 1 [Note] A temporary password is generated for root@localhost: 6x-tiUE!IvQw
[root@localhost mysql]#
如下命令进入数据库:
[root@localhost mysql]# mysql -uroot -p
Enter password:
mysql>
输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
这里有个问题,新密码设置的时候如果设置的过于简单会报错:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
原因是因为MySQL有密码设置的规范,具体是与validate_password_policy的值有关:
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:
解决方案:
(1)首先,修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.01 sec)
(2)validate_password_length(密码长度)参数默认为8,我们修改为1
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
(3)完成之后再次执行修改密码语句即可成功
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
退出后,重新登陆即可
mysql> exit;
Bye
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>