【MySQL】mysql忘记root密码

mysql数据库忘记root密码的解决方法,步骤如下

① # vim /etc/my.cnf
在[mysqld]下添加skip-grant-tables,然后保存并退出
② 重启mysql服务:service mysqld restart
③ 执行 mysql 命令进入数据库,修改数据库密码
MySQL> UPDATE mysql.user SET Password=PASSWORD('新密码') where USER='root';
如果是MySQL5.7,需要改为MySQL> UPDATE mysql.user SET authentication_string=PASSWORD('新密码') where USER='root';
mysql> flush privileges;
④ 把/etc/my.cnf中的skip-grant-tables注释掉,然后重启mysql,
即:service mysqld restart
⑤ 下面就可以用root新的密码登录了

[root@ops04 scripts]# vim /etc/my.cnf
[root@ops04 scripts]# service mysqld restart
Restarting mysqld (via systemctl):                         [  OK  ]
[root@ops04 scripts]# 
[root@ops04 scripts]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.1.22-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> set password=password('000000');
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
MariaDB [(none)]> UPDATE mysql.user SET Password=PASSWORD('000000') where USER='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@ops04 scripts]# vim /etc/my.cnf
[root@ops04 scripts]# service mysqld restart
Restarting mysqld (via systemctl):                         [  OK  ]
[root@ops04 scripts]# 
[root@ops04 scripts]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.1.22-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

你可能感兴趣的:(mysql)