对于DBA来讲,丢失超管用户root的密码是致命的,mysql
能够经过添加--ship-grant-tables参数来跳过权限表。sql
这个时候须要强制停库,先查看MySQL进程号数据库
Kill掉MySQL进程,命令以下:测试
Kill完能够再查看是否还有进程spa
[root@tse2 tmp]# kill -9 9840 1 [root@tse2 tmp]# ps -ef |grep mysql
建议停库这种操做不要影响业务的状况下,停服操做的,还有最好这种密码记录好的,能够用keepass保存密码软件!code
而后加跳过权限表参数,重启数据库。这样即便不输入密码,也能够进入数据库。orm
[root@tse2 bin]# mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables & [1] 4854
直接输入mysql不须要密码便可进入blog
给root用户设置新的密码并刷新权限,MySQL5.7以后,库下的password字段用authentication_string字段代替。进程
(product)root@localhost:mysql.sock [(none)]> use mysql; Database changed (product)root@localhost:mysql.sock [mysql]> update user set authentication_string=password('123456') where user='root'; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 1 (product)root@localhost:mysql.sock [mysql]> flush privileges; Query OK, 0 rows affected (0.00 sec)
设置完成以后,重启数据库。注意重启的时候不须要再加--skip-grant-tables参数了,正常启动服务,输入新的密码能够正常进入数据库了。这边重启之后测试直接mysql没有密码是没法进入的了。ip
[root@tse2 bin]# mysqld_safe --deaults-file=/etc/my.cnf & [2] 6720 [root@tse2 bin]# 2020-01-16T02:55:45.223195Z mysqld_safe Logging to '/mysql/mysql3306/logs/error.log'. 2020-01-16T02:55:45.262302Z mysqld_safe A mysqld process already exists [2]+ Exit 1 mysqld_safe --deaults-file=/etc/my.cnf [root@tse2 bin]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
这边输入密码链接,便可正常进入,是修改后的密码123456
[root@tse2 bin]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.23-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. (product)root@localhost:mysql.sock [(none)]> use mysql Database changed (product)root@localhost:mysql.sock [mysql]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)