本人由于root密码设置太复杂,距离买云服务器过去好长时间,记不起来了,只好重置密码。

编辑配置文件/etc/my.cnf,在 [mysqld] 段加入一行:skip-grant-tables

如下:

[mysqld]
skip-grant-tables
skip-ssl
port = 3306
......

执行重启指令:service mysqld restart

[root@centos ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL... SUCCESS! 

重启成功后,即可免密码登录root用户:mysql -u root

[root@centos ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log 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.

mysql> 

登录成功后,执行SQL更改root密码(假设为123456):

UPDATE mysql.user SET authentication_string = PASSWORD('123456') WHERE User = 'root';

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

mysql> 

注意上面的SQL语句,在5.7版本后,原来的密码字段名称Password改成了authentication_string。

退出mysql控制台,把添加到/etc/my.cnf配置文件的 skip-grant-tables 这行删除,再重启服务:service mysqld restart。之后,我们就可以使用新密码登录了。

[root@centos ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@centos ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log 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.

mysql>