MySQL忘记密码怎么办??

前端之家收集整理的这篇文章主要介绍了MySQL忘记密码怎么办??前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、停掉MysqL

1.1单实例停止方式

[root@qiuhom ~]# /etc/init.d/MysqLd stop
Shutting down MysqL.                                       [  OK  ]

  1.2多实例停止方式 

[root@qiuhom ~]# /data/3306/MysqL stop
Stopping MysqL ...  

当然停止MysqLd的方式有很多比如也可以用kill -9 +MysqLd的pid号,killall MysqLd 或者pkill MysqLd都可以将进程杀死,但是这种简单粗暴的方式我们不推荐。

2、用MysqLd_safe 启动数据库

2.1单实例启动

[root@qiuhom ~]# MysqLd_safe --skip-grant-tables --user=MysqL &
[1] 1285
[root@qiuhom ~]# 181004 17:23:52 MysqLd_safe Logging to '/application/MysqL-5.5.32/data/qiuhom.err'.
181004 17:23:52 MysqLd_safe Starting MysqLd daemon with databases from /application/MysqL-5.5.32/data

[root@qiuhom ~]# MysqL
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 1
Server version: 5.5.32-log Source distribution

Copyright (c) 2000,2013,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> 

提示:用MysqLd_safe启动数据库一定要加--skip-grant-tables 忽略授权表的方式启动,当然也要指定用户

通过以上MysqLd_safe启动后此时登录数据库就没有密码了 我可以MysqL直接进入 也可以MysqL -uroot -p 回车 密码为空直接回车也可以进入数据库

2.2多实例启动

[root@qiuhom ~]# MysqLd_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables &
[1] 798
[root@qiuhom ~]# 181003 10:16:53 MysqLd_safe Logging to '/data/3306/MysqL_lee3306.err'.
181003 10:16:53 MysqLd_safe Starting MysqLd daemon with databases from /data/3306/data

[root@qiuhom ~]# MysqL -S /data/3306/MysqL.sock 
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 2
Server version: 5.5.32-log Source distribution

Copyright (c) 2000,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> 

提示:多实例的启动要指定启动配置文件的位置,--defaults-file 就是用来指定启动配置文件的位置。值得注意的是登录多实例MysqL 一定要指定MysqL的sock文件

-S 来指定登录MysqL的sock文件位置

3、进入数据库后,我们用update对MysqL库里的user表进行修改

MysqL> update MysqL.user set password=password("admin123.com") where user='test' and host='localhost';
Query OK,1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MysqL> select user,host,password from MysqL.user;
+------------+-----------+-------------------------------------------+
| user       | host      | password                                  |
+------------+-----------+-------------------------------------------+
| root       | localhost | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| root       | 127.0.0.1 |                                           |
| qiuhom     | %         | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| test       | localhost | *2D9AC2437F9E59A51BE8BA89A3D59E76F32F55E8 |
| test       | %         | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| qiuhom_db1 | localhost | *2D9AC2437F9E59A51BE8BA89A3D59E76F32F55E8 |
| qiuhom_db2 | localhost | *2D9AC2437F9E59A51BE8BA89A3D59E76F32F55E8 |
+------------+-----------+-------------------------------------------+
7 rows in set (0.00 sec)
MysqL> flush privileges;
Query OK,0 rows affected (0.00 sec) 

提示:设置的密码我不能直接写password=“xxx”,一定要用password函数将自己设置的密码加密,我们在数据库里看到的都是密文密码。用update 修改表里的内容,一定要注意 后面一定要条件,条件越多越精确。最后不要忘记刷新权限哟!!!

4、停止掉MysqLd_safe启动的数据库,重新以正常的方式启动数据库

4.1停止MysqLd_safe启动的数据库实例(单实例)

[root@qiuhom ~]# MysqLadmin -utest -padmin123.com shutdown
181004 17:53:37 MysqLd_safe MysqLd from pid file /application/MysqL-5.5.32/data/qiuhom.pid ended
[1]+  Done                    MysqLd_safe --skip-grant-tables --user=MysqL
[root@qiuhom ~]# ps -ef |grep MysqL|grep -v grep 
[root@qiuhom ~]# 

  4.2停止MysqLd_safe启动的数据库实例(多实例)

[root@qiuhom ~]# MysqLadmin -utest -padmin123.com -S /data/3306/MysqL.sock shutdown
181003 10:46:59 MysqLd_safe MysqLd from pid file /data/3306/MysqLd.pid ended
[1]+  Done                    MysqLd_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables
[root@qiuhom ~]# ps -ef |grep 3306|grep -v grep 
[root@qiuhom ~]# 

  4.3正常启动MysqL(单实例)

[root@qiuhom ~]# /etc/init.d/MysqLd start
Starting MysqL                                             [  OK  ]
[root@qiuhom ~]# MysqL -utest -padmin123.com
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 3
Server version: 5.5.32-log Source distribution

Copyright (c) 2000,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> 

  4.4正常启动MysqL(多实例)

[root@qiuhom ~]# /data/3306/MysqL start
Starting MysqL...
[root@qiuhom ~]# MysqL -utest -padmin123.com -S /data/3306/MysqL.sock 
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 1
Server version: 5.5.32-log Source distribution

Copyright (c) 2000,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> 

提示:以上是忘记密码,重新设置密码的流程 不要和MysqLadmin混淆,MysqLadmin用于空密码设置密码或者知道密码修改密码。多实例的启动一定要加--defaults-file来指定启动配置文件登录多实例一定要用-S(大写)指定MysqL的sock文件。。

猜你在找的MySQL相关文章