Mysql忘记root密码解决方法

1、更改my.cnf配置文件
1.1、用命令编辑/etc/my.cnf配置文件,即:vim /etc/my.cnf 或者 vi /etc/my.cnf
1.2、在[mysqld]下添加
skip-grant-tables,然后保存并退出
1.3、重启mysql服务:service mysqld restart

[root@master ~]# service mysqld restart
Redirecting to /bin/systemctl restart  mysqld.service
2、更改root用户名
2.1、重启以后,执行mysql命令进入mysql命令行
2.2、修改root用户密码

[root@master ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> UPDATE mysql.user SET Password=PASSWORD('123qwe') where USER='root';
Query OK, 5 rows affected (0.04 sec)
Rows matched: 5  Changed: 5  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@master ~]#
2.3、把/etc/my.cnf中的skip-grant-tables注释掉,然后重启mysql,即:service mysqld restart
[root@master ~]# service mysqld restart
Redirecting to /bin/systemctl restart  mysqld.service
3、使用新密码登录
然后,就可以用root新的密码登录了!
[root@master ~]# mysql -hlocalhost -uroot -p123qwe
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 |
| amon               |
| cm                 |
| hive               |
| mysql              |
| oozie              |
| performance_schema |
| rept               |
+--------------------+
8 rows in set (0.00 sec)

mysql> 

猜你喜欢

转载自blog.csdn.net/babyfish13/article/details/80252724