MySQL的root密碼忘記後重設方法

來源:互聯網
上載者:User

標籤: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密碼忘記後重設方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.