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, 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>
提示:多實例的啟動要指定啟動配置文件的位置,--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, 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>
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, 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>
提示:以上是忘記密碼,重新設置密碼的流程 不要和mysqladmin混淆,mysqladmin用於空密碼設置密碼或者知道密碼修改密碼。多實例的啟動一定要加--defaults-file來指定啟動配置文件,登錄多實例
一定要用-S(大寫)指定mysql的sock文件。。
mysql忘記密碼怎麽辦??