Linux下修改Mysql的使用者(root)的密碼

來源:互聯網
上載者:User

標籤:yun   end   startup   read   使用者   inpu   acl   reading   刪除   

一、擁有原來的myqlroot的密碼;
方法一:

在mysql系統外,使用mysqladmin

# mysqladmin -u root -p password "test123"

Enter password: 【輸入原來的密碼】

 

方法二:

通過登入mysql系統,

mysql> use mysql;

mysql> update user set password=password("oracle") where user=‘root‘;

mysql> flush privileges;

mysql> exit;    

  

方法三: 用SET PASSWORD命令 

首先登入MySQL。 

格式:mysql> set password for ‘使用者名稱‘@‘localhost‘ = password(‘新密碼‘); 

例子:mysql> set password for ‘root‘@‘localhost‘ = password(‘123‘); 

 

 

 


二、忘記原來的myqlroot的密碼;

1、skip-grant-tables

 

我們常用的方法是使用skip-grant-tables選項,mysqld server啟動之後並不使用許可權系統(privilege system)。使用者不需要任何帳號、不受任何限制的訪問資料庫中所有資料。為了安全起見,通常加上skip-networking,mysqld不偵聽任何TCP/IP串連請求。操作過程如下,

 

1)修改my.cnf設定檔,在mysqld選項中添加skip-grant-tables和skip-networking。

 

2)再重啟mysqld server。

 

3)通過sql語句修改mysql.user表中儲存密碼。執行flush privileges,重新啟用mysql許可權系統。

 

UPDATE mysql.user SET password=PASSWORD(‘newpwd‘)WHERE user=‘root‘;

 

FLUSH PRIVILEGES;

 

4)刪除或者注釋設定檔中skip-grant-tables和skip-networking的參數選項。如果使用skip-networking,則需要再次重啟mysqld。因為skip-networking不是系統變數,只是mysqld的參數選項,而不能通過系統變數動態進行設定。如果沒有適用skip-networking,只需要執行flush privileges就可以使許可權系統重新生效。

 

2. --init-file

 

mysqld_safe可以使–init-file參數選項來執行重新設定密碼的sql語句。

 

1)建立一個初始設定檔案,如/tmp/initfile,檔案內容為上面修改密碼的sql語句。

 

UPDATE mysql.user SET Password=PASSWORD(‘newpwd‘) WHERE User=‘root‘;

 

FLUSH PRIVILEGES;

 

2)關閉mysqld服務進程。

 

3)使用mysqld_safe啟動mysqld;

 

mysqld_safe --init-file=/home/me/mysql-init &

上面的兩種方法是在忘記root密碼情況下重新設定密碼的方法,可以發現都需要重啟mysqld服務。很多人都是使用第一種進行重設root密碼,但是比較推薦的做法反而是第二種,即安全有快捷簡單。

 

 

 

3、不重啟mysqld的方法

 

1、首先得有一個可以擁有修改許可權的mysql資料庫帳號,當前的mysql執行個體帳號(較低許可權的帳號,比如可以修改test資料庫)或者其他相同版本執行個體的帳號。把data/mysql目錄下面的user表相關的檔案複製到data/test目錄下面。

 

[[email protected] mysql]# cp mysql/user.* test/

[[email protected] mysql]# chown mysql.mysql test/user.*

 

2、使用另一個較低許可權的帳號連結資料庫,設定test資料庫中的user儲存的密碼資料。

[[email protected] mysql]# mysql -utest -p12345

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 17

Server version: 5.5.25a-log Source distribution

 

Copyright (c) 2000, 2011, 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> use test

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

Database changed

mysql> update user set password=password(‘yayun‘) where user=‘root‘;

Query OK, 0 rows affected (0.00 sec)

Rows matched: 5  Changed: 0  Warnings: 0

 

mysql>

 

3、把修改後的user.MYD和user.MYI複製到mysql目錄下,記得備份之前的檔案。

mv mysql/user.MYD mysql/user.MYD.bak

mv mysql/user.MYI mysql/user.MYI.bak

cp test/user.MY* mysql/

chown mysql.mysql mysql/user.*

 

4、尋找mysql進程號,並且發送SIGHUP訊號,重新載入許可權表。

 

[[email protected] mysql]# pgrep -n mysql

2184

[[email protected] mysql]#

[[email protected] mysql]# kill -SIGHUP 2184

 

5.登陸測試

[[email protected] mysql]# mysql -uroot -pyayun

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 20

Server version: 5.5.25a-log Source distribution

 

Copyright (c) 2000, 2011, 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>

 

 

 

Linux下修改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.