標籤:mysql 密碼
如果忘記了MySQL的root密碼怎麼辦?
我們預設的情況下是沒有給MySQL設定密碼的,如下
預設的登入MySQL
[[email protected] ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.40-log MySQL Community Server (GPL)
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>quit
Bye
正常情況下為了安全考慮我們應該給MySQL去設定一個密碼
[[email protected] ~]# mysqladmin -uroot password ‘lamlinux‘
設定完成,我們再次登入就要輸入密碼了
[[email protected] ~]# mysql -uroot -plamlinux
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.40-log MySQL Community Server (GPL)
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>quit
Bye
假如說我們密碼忘記了怎麼辦,登陸不上MySQL怎麼辦?
把密碼初始化,重新設定一個,
進入設定檔
[[email protected] ~]# vim /etc/my.cnf
在‘慢查詢’下面加入一句話
skip-grant (跳過授權)
:wq
重啟MySQL
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
初始化完成
可以直接用“mysql”命令,不需要輸入-p密碼,就可以直接進入MySQL了,
[[email protected] ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.40-log MySQL Community Server (GPL)
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>
然後去使用mysql庫
mysql> use mysql
No connection. Trying to reconnect...
Connection id: 2
Current database: *** NONE ***
Database changed
然後去更新一個表,即更改root使用者的密碼
mysql> update user set password=password(‘lam2linux‘) where user=‘root‘;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: mysql
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
從資訊中我們看到Query OK, 3 rows affected (0.01 sec)顯示第3行發生改變,
用以下命令可以查看變更資訊
mysql>select * from user where user=‘root‘\G;
退出MySQL
mysql>exit
我們再把MySQL設定檔裡的“skip-grant”去掉
[[email protected] ~]# vim /etc/my.cnf
去掉 skip-grant
:wq
重啟MySQL
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL. SUCCESS!
再次登入MySQL需要輸入密碼
[[email protected] ~]# mysql -uroot -plam2linux
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.40-log MySQL Community Server (GPL)
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>
至此,修改密碼後並成功登入
MySQL的root密碼忘記後重設方法