很久以前遇到过一次这样的问题,当时搞定之后,忘记整理记录了。峰回路转,结果这次又踩坑了,还是记录一下比较好…
这里用虚拟机搭建一个实验环境演示
1.验证安装成功
Complete!
[root@localhost tmp]# systemctl start mysqld
[root@localhost tmp]# systemctl enable mysqld
[root@localhost tmp]# systemctl daemon-reload
[root@localhost tmp]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-11-12 03:45:33 EST; 21s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 10167 (mysqld)
CGroup: /system.slice/mysqld.service
└─10167 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/my...
Nov 12 03:45:28 localhost.localdomain systemd[1]: Starting MySQL Server...
Nov 12 03:45:33 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost tmp]# grep 'temporary password' /var/log/mysqld.log
2019-11-12T08:45:30.496074Z 1 [Note] A temporary password is generated for root@localhost: qo?klvT.M4qO
[root@localhost tmp]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28
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> exit;
Bye
[root@localhost tmp]#
2.修改 my.cnf 配置文件
vi /etc/my.cnf
主要是添加跳过登录验证的命令skip-grant-tables
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#跳过登录验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
重启MySQL
systemctl restart mysqld
3.修改密码
mysql
注意: MySQL5.7密码规则改了,得根据版本用不同的字段名称
MySQL 5.7 以下版本:
mysql> update mysql.user set password=password('123456') where user='root';
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql>
MySQL 5.7 版本:
mysql> update mysql.user set authentication_string=password('123456') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
允许远程连接
update mysql.user set host='%' where user ='root';
更新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
注释 my.cnf 中的 skip-grant-tables
再次重启数据库