Mysql root密码丢失后重置
假装密码丢失
mysql> set password=password('dsfsdfjsaldfjldsfsdf4dsf46453131321'); Query OK, 0 rows affected, 1 warning (0.00 sec)
1.关闭服务
[root@logan ~]# killall mysqld [root@logan ~]# killall mysqld mysqld: no process found
2.跳过授权表不验证;这里有两种方法:
1)直接启动服务
/usr/local/mysql/bin/mysqld_safe --user=mysql --skip-grant-tables &
2)对/etc/my.cnf 文件进行配置
[root@logan etc]# vim my.cnf 1 [mysqld] 2 skip-grant-tables
这里我用的第二种方法
3.重启mysql服务
[root@logan etc]# systemctl restart mysqldd
4.验证是否跳过授权表验证?
[root@logan etc]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 Source distribution Copyright (c) 2000, 2019, 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>
可以看到,我们跳过的密码环节,直接进入到数据库
5.修改root 密码,我们先查看一下root密码存放位置
mysql> select * from mysql.user\G; *************************** 1. row *************************** Host: localhost User: root Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Reload_priv: Y Shutdown_priv: Y Process_priv: Y File_priv: Y Grant_priv: Y References_priv: Y Index_priv: Y Alter_priv: Y Show_db_priv: Y Super_priv: Y Create_tmp_table_priv: Y Lock_tables_priv: Y Execute_priv: Y Repl_slave_priv: Y Repl_client_priv: Y Create_view_priv: Y Show_view_priv: Y Create_routine_priv: Y Alter_routine_priv: Y Create_user_priv: Y Event_priv: Y Trigger_priv: Y Create_tablespace_priv: Y ssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0 max_updates: 0 max_connections: 0 max_user_connections: 0 plugin: mysql_native_password authentication_string: *CB46BA0A9F8F5BFD02046E4AF418EE9FC7BE2E0D #这个是root密码 password_expired: N password_last_changed: 2019-07-03 14:16:59 password_lifetime: NULL account_locked: N
我们需要把这个密码换成我们自己的
mysql> update mysql.user set authentication_string=password('123') where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1
提示成功后,我们需要删除my.cnf文件里的内容并重启服务
[root@logan etc]# systemctl restart mysqldd [root@logan etc]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
现在提示需要我们输入密码
[root@logan etc]# mysql -uroot -p123 mysql: [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.7.25 Source distribution Copyright (c) 2000, 2019, 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>
重置成功