MySQL補充——忘記密碼怎么辦
摘要:本文主要記錄了在忘記密碼時怎么辦,
部分內容來自以下博客:
https://www.cnblogs.com/wuotto/p/9682400.html
關閉MySQL資料庫
使用命令檢查MySQL資料庫是否已經關閉:
1 [root@localhost ~]# systemctl status mysql
出現“Active: inactive (dead)”表示資料庫已關閉,如果是“Active: active (exited)”表示已開啟,需要手動關閉:
1 [root@localhost ~]# systemctl stop mysql
修改MySQL的組態檔
找到MySQL的組態檔“my.cnf”,默認路徑是:“/etc/my.cnf”,
打開檔案并在“[mysqld]”下添加:
1 skip-grant-tables
這句話的作用是在登錄MySQL的時候可以跳過密碼直接登錄,
保存修改并退出,
啟動MySQL
使用命令啟動MySQL:
1 [root@localhost ~]# systemctl start mysql
輸入“mysql”即可進入資料庫,
修改密碼
連接“mysql”資料庫,修改用戶密碼:
1 mysql> use mysql; 2 Reading table information for completion of table and column names 3 You can turn off this feature to get a quicker startup with -A 4 5 Database changed 6 mysql> update mysql.user set password=password('123456') where user='root'; 7 Query OK, 5 rows affected (0.00 sec) 8 Rows matched: 5 Changed: 5 Warnings: 0 9 10 mysql>
將root用戶的密碼設為123456,
重繪使改動生效:
1 mysql> flush privileges; 2 Query OK, 0 rows affected (0.00 sec)
回改檔案并重啟
退出MySQL,重新打開“my.cnf”組態檔,洗掉“skip-grant-tables”,保存退出,
重啟MySQL,需要密碼登錄,輸入設定的密碼即可:
1 [root@localhost ~]# vim /etc/my.cnf 2 [root@localhost ~]# systemctl restart mysql 3 [root@localhost ~]# mysql 4 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 5 [root@localhost ~]# mysql -u root -p 6 Enter password: 7 Welcome to the MySQL monitor. Commands end with ; or \g. 8 Your MySQL connection id is 4 9 Server version: 5.6.45 MySQL Community Server (GPL) 10 11 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 12 13 Oracle is a registered trademark of Oracle Corporation and/or its 14 affiliates. Other names may be trademarks of their respective 15 owners. 16 17 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 18 19 mysql>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/19752.html
標籤:MySQL
上一篇:You can't specify target table 'Person' for update in FROM clause
下一篇:MySQL 游標
