mysql-centos安装配置MySql8.0

MySQL8.0和MySQL5.7具有众多不同之处,此处不述。这里,只简单讲讲在安装过程中遇到的问题之一和解决办法:
MySQL8.0安装完成之后的默认密码是多少?如何修改初始密码?

1. 安装MySQL8.0

  • yum仓库下载MySQL:
1
shell> yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
  • yum安装MySQL:
1
shell> yum install mysql-community-server

2. 启动MySQL服务

  • 启动MySQL服务的命令:
1
2
3
shell> service mysqld start

Starting mysqld:[ OK ]
  • 检查MySQL服务器的运行状态:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
shell> sudo service mysqld status

● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2018-06-03 18:31:51 CST; 6min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 5281 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 5299 (mysqld)
Status: "SERVER_OPERATING"
CGroup: /system.slice/mysqld.service
└─5299 /usr/sbin/mysqld

Jun 03 18:31:50 {your-server-name} systemd[1]: Starting MySQL Server...
Jun 03 18:31:51 {your-server-name} systemd[1]: Started MySQL Server.

以上信息表示MySQL服务启动成功。

出现Job for mysqld.service failed because the control process exited with error code问题

删除/var/lib/mysql /后重启MySQL服务就可以了!

1
2
rm -rf /var/lib/mysql
service mysqld start

3. MySQL默认密码和修改密码

在启动MySQL服务的时候,主要会发生以下4件事

  • MySQL Server初始化并启动起来;
  • MySQL的data文件夹中生成SSL证书和key文件;
  • 密码验证组件被安装并且生效;
  • 创建一个超级管用户‘root‘@’localhost‘。超级用户设置的密码被保存在错误日志文件中,可以通过以下命令查看:
1
2
3
shell> sudo grep 'temporary password' /var/log/mysqld.log

2018-06-03T10:15:57.448920Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 0xxXxxXx?xXX

通过默认密码登录MySQL服务器,并马上修改密码(强烈建议)!!!。

有些时候使用上面的筛选命令检索不到文件或内容,可以手动查看/var/log/mysqld.log文件获取初始密码。

用默认密码(0xxXxxXx?xXX)登录:

1
shell> mysql -uroot -p

修改密码:

1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-password';

mysql8.0 设置简单密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
【起因】
有时候只是因为内网访问使用测试服务器,不想设置复杂的密码,比如root:123456
但是新版 mysql 加入密码安全度检测机制,导致报错
解决方法如下
1.查看当前安全变量值

1
mysql> SHOW VARIABLES LIKE 'validate_password%';

2.修改变量

注意到8.0 比5.7多了带“.”的变量导致只设置一半是不够的,

1
2
set global validate_password.policy=0; 
set global validate_password.length=4;

然后退出后再执行(我没有执行这句话,直接重置了)

1
mysql_secure_installation

继续重置root密码就可以设置为123456了
重置语句

1
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

4. 设置允许远程连接

在终端登录mysql之后查看是否允许远程访问:

1
mysql -u root -p
1
2
3
4
5
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
1
2
3
4
5
6
7
8
9
10
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)

可以看到最后一行root 用户的host为localhost,要远程访问,需要将它改成%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> update user set host='%' where user ='root';
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)

最后刷新权限

1
2
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.10 sec)