2012-04-26 108 views
18

我已經繼承了一臺安裝了mysql的服務器。我沒有任何用戶的mysql密碼,甚至沒有root用戶(儘管我擁有linux root密碼)。此外,我只知道其他用戶帳戶besdies root,並且沒有執行任何操作的權限,甚至沒有SELECT。當前密碼未知時重置MySQL root密碼

我試圖停止MySQL servicw,跳越授權表選項重新啓動,只是記錄在無密碼:

service mysqld stop 
service mysqld start --skip-grant-tables & 
mysql -u root 

但得到以下錯誤:

Access denied for user 'root'@'localhost' (using password: NO) 

然後我嘗試重置密碼:

mysqladmin -u root password 'newpw' 

但是,這也給拒絕訪問埃羅河

我也嘗試登錄的其他用戶(不PW)和執行以下命令:

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; 

,並得到這個錯誤:

ERROR 1142 (42000): UPDATE command denied to user ''@'localhost' for table 'user' 

我也試圖消除MySQL和重新安裝,但我得到同樣的錯誤。

有什麼建議嗎?

+2

嘗試溶液[這些指令(http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html#resetting-permissions-unix)從手冊。 – 2012-04-26 17:03:23

+1

請注意,如果您要使用'--skip-grant-tables',則可能需要直接啓動'mysqld',因爲這些選項不一定要通過init腳本傳遞。 – 2012-04-26 17:04:53

+1

這個問題確實屬於[數據庫管理員](http://dba.stackexchange.com/)或[超級用戶](http://superuser.com/),而不是SO。 – eggyal 2012-04-26 17:06:31

回答

7

兩個選項:

如果你想使用--skip-贈款表,你可能需要的mysqld直接啓動的選項不一定沿着穿過的init腳本傳遞。

否則,請嘗試從手動these instructions

2

`UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; FLUSH PRIVILEGES;

每個寫在一行的UPDATE和FLUSH語句。 UPDATE語句對所有root賬戶重置密碼,而FLUSH語句告訴服務器重裝授權表到內存中,以便它注意到了密碼change.`

34

嘗試sudo dpkg-reconfigure mysql-server-5.5,你會被要求提供新的root密碼。

與您當前的MySQL服務器的版本替換5.5

+3

最方便,快捷,優雅的Debian風格的方式。像魅力一樣工作 – 2014-03-23 19:03:15

+3

無法將密碼設置爲無密碼。在這種情況下,設置任何密碼,登錄到MySQL和'SET PASSWORD FOR root @ localhost = PASSWORD('');' – bartekbrak 2014-03-24 12:07:26

+0

這就是我使用它的工作原理 – 2016-09-27 15:25:40

0

在我的情況是發生相同的,但我是在登錄到MySQL提示犯錯誤。 我在做mysql而不是mysql -u root -p

3

你需要確保你使用sudo命令以root身份運行這個命令。

我發現這個instruction重置mysql密碼效果很好。

按照此過程,您將禁用MySQL服務器上的訪問控制。所有連接都將具有root訪問權限。從網絡上拔下服務器或者至少禁用遠程訪問是一件好事。

要重置密碼的mysqld只是按照下列指示:

Stop the mysql demon process using this command : 

     sudo /etc/init.d/mysql stop 

Start the mysqld demon process using the --skip-grant-tables option with this command 

     sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking & 

因爲你不檢查此時用戶PRIVS,這是最安全的禁用聯網。在Dapper中,/ usr/bin/mysqld ...沒有工作。但是,mysqld --skip-grant-tables沒有。

start the mysql client process using this command 

     mysql -u root 

from the mysql prompt execute this command to be able to change any password 

     FLUSH PRIVILEGES; 

Then reset/update your password 

     SET PASSWORD FOR [email protected]'localhost' = PASSWORD('password'); 

If you have a mysql root account that can connect from everywhere, you should also do: 

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

Alternate Method: 

     USE mysql 
     UPDATE user SET Password = PASSWORD('newpwd') 
     WHERE Host = 'localhost' AND User = 'root'; 

And if you have a root account that can access from everywhere: 

     USE mysql 
     UPDATE user SET Password = PASSWORD('newpwd') 
     WHERE Host = '%' AND User = 'root'; 

對於任一種方法中,一旦已經接收到消息,表明成功的查詢(受影響的一個或多個行),齊平特權:

FLUSH特權;

然後停止mysqld進程,並與經典的方式重新啓動它:

須藤/etc/init.d/mysql停止

須藤/etc/init.d/mysql開始

0

對於在Ubuntu 16.04 MySQL的16年7月5日這工作:

  • 登陸無密碼:

    須藤mysqld_safe的--skip發放桌--skip聯網&

  • 改變密碼:

    SET密碼 '根' @ '本地主機'= '爲mypass';

實測值上MySQL 5.7 Reference Manual