CentOS下重設MySQL的root密碼的教程_Mysql

來源:互聯網
上載者:User

本人在CentOS6.4上安裝萬mysql後,無法通過root進入,因為安裝的時候,並沒有設定root密碼,似乎有個初始隨機密碼,但是不記得了,太麻煩,直接重設root密碼。
首先,你必須要有作業系統的root許可權了。要是連繫統的root許可權都沒有的話,先考慮root系統再走下面的步驟。

PS:賦予用於root許可權
方法一: 修改 /etc/sudoers 檔案,找到%wheel一行,把前面的注釋(#)去掉

## Allows people in group wheel to run all commands%wheel ALL=(ALL) ALL

然後修改使用者,使其屬於root組(wheel),命令如下:

#usermod -g root tommy

修改完畢,現在可以用tommy帳號登入,然後用命令 sudo su - ,即可獲得root許可權進行操作。

方法二: 修改 /etc/sudoers 檔案,找到root一行,在root下面添加一行,如下所示:

## Allow root to run any commands anywhereroot ALL=(ALL)  ALLtommy ALL=(ALL)  ALL

修改完畢,現在可以用tommy帳號登入,然後用命令 sudo su - ,即可獲得root許可權進行操作。

方法三: 修改 /etc/passwd 檔案,找到如下行,把使用者ID修改為 0 ,如下所示:

tommy:x:500:500:tommy:/home/tommy:/bin/bash

修改後如下

tommy:x:0:500:tommy:/home/tommy:/bin/bash

儲存,用tommy賬戶登入後,直接擷取的就是root帳號的許可權。


root下類似於安全模式登入系統,有人建議說是pkill mysql,但是我不建議哈。因為當你執行了這個命令後,會導致這樣的狀況:

/etc/init.d/mysqld statusmysqld dead but subsys locked

這樣即使你是在安全模式下啟動mysql都未必會有用的,所以一般是這樣/etc/init.d/mysqld stop,如果你不幸先用了pkill,那麼就start一下再stop咯。
使用rpm包安裝完mysql後,按照一下步驟重設root密碼:
啟動mysql:

#/etc/init.d/mysql start

啟動成功後查看mysql進程資訊,擷取mysqld_safe的安裝目錄(非常關鍵):

#ps -ef | grep -i mysqlroot  3466  1 0 01:45 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/BSC.TEST.pidmysql  3569 3466 16 01:45 pts/1 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/BSC.TEST.err --pid-file=/var/lib/mysql/BSC.TEST.pidroot  3597 3105 0 01:45 pts/1 00:00:00 grep -i mysql

可以看到mysqld_safe的安裝位置(上面標藍色部分):/usr/bin/
接著執行一下命令停止mysql:

/etc/init.d/mysql stop

以安全方式啟動mysql:

#/usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &

稍等5秒鐘,然後執行以下語句:

#/usr/bin/mysql -u root mysql

注意:mysql與mysql_safe目錄一樣,都是:/usr/bin/下,這個是通過“ps -ef | grep -i mysql”命令得到的。

出現“mysql>”提示符後輸入:

mysql> update user set password = Password('root') where User = 'root';

斷行符號後執行(重新整理MySQL系統許可權相關的表):

mysql> flush privileges;

再執行exit退出:

mysql> exit;

退出後,使用以下命令登陸mysql,試試是否成功:

#mysql -u root -p 

按提示輸入密碼:

root

但是執行查看資料庫命令報錯:

mysql> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

解決辦法:

mysql> SET PASSWORD=PASSWORD('root');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+| Database   |+--------------------+| information_schema || mysql    || performance_schema || test    |+--------------------+4 rows in set (0.00 sec)

PS:如果在使用mysqladmin的話:

# mysqladmin -u root -p password "test123"
Enter password: 【輸入原來的密碼】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.