Centos7如何修改或重置MySQL8.0的root密码
一、修改配置文件
1、停止mysql服务
service mysqld stop
2、修改配置文件/etc/my.cnf
1)输入命令:vim /etc/my.cnf
2)按I进入编辑模式,在my.cnf文件中添加skip-grant-tables(意思是忽略密码),按esc键,输入:wq保存退出
3、启动/重启Mysql
service mysqld start
或
service mysqld restart
二、免密码登录mysql并设置把密码先设置为空字符串
1、登录:mysql -u root -p
2、按回车直接进入数据库
3、使用数据库:use mysql;
4、查看root用户信息:select host, user, authentication_string, plugin from user;
5、更新root用户信息,把密码设置为空字符串:
update user set authentication_string=’’ where user = ‘root’;
6、刷新权限表:FLUSH PRIVILEGES
7、退出:exit
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> 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
mysql> update user set authentication_string='' where user = 'root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
三、修改配置表,并重启
1、修改配置文件/etc/my.cnf,把上述步骤添加的skip-grant-tables注释掉
2、重启:service mysqld restart
四、设置root登录mysql的密码
1、登录:mysql -u root -p(这时候还是不用输入密码,因为上面已经把密码设置为空字符串了);
2、修改root用户密码:ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
3、刷新权限表:FLUSH PRIVILEGES
3、退出:exit
五、用新设置的密码登录mysql