当前密码未知时重置 MySQL 根密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10338015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resetting MySQL root password when current password is not known
提问by Bad Programmer
I have inherited a server that has mysql installed on it. I don't have the mysql password for any user, not even root (although I have the linux root password). Plus, I am only aware of one other user account besdies root, and that one does not have privileges to perform any action, not even SELECT.
我继承了一台安装了 mysql 的服务器。我没有任何用户的 mysql 密码,甚至没有 root(尽管我有 linux root 密码)。另外,我只知道另一个用户帐户 besdies root,并且该用户没有执行任何操作的权限,甚至没有执行 SELECT 的权限。
I tried stopping the mysql servicw, restarting with the skip grant tables option, and just logging in without password:
我尝试停止 mysql 服务,使用跳过授权表选项重新启动,然后在没有密码的情况下登录:
service mysqld stop
service mysqld start --skip-grant-tables &
mysql -u root
But get the following error:
但得到以下错误:
Access denied for user 'root'@'localhost' (using password: NO)
I then tried resetting the password:
然后我尝试重置密码:
mysqladmin -u root password 'newpw'
But that also gives an access denied error.
但这也给出了拒绝访问错误。
I also tried logging in as the other user (without pw) and executing the following command:
我还尝试以其他用户(没有密码)的身份登录并执行以下命令:
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
and got this error:
并收到此错误:
ERROR 1142 (42000): UPDATE command denied to user ''@'localhost' for table 'user'
I have also tried removing mysql and reinstalling, but I get the same errors.
我也试过删除 mysql 并重新安装,但我遇到了同样的错误。
Any suggestions?
有什么建议?
采纳答案by Michael Mior
Two options:
两种选择:
If you want to use --skip-grant-tables you may need to start mysqld directly as the options aren't necessarily passed along through the init script.
如果您想使用 --skip-grant-tables,您可能需要直接启动 mysqld,因为选项不一定通过 init 脚本传递。
Otherwise, try these instructionsfrom the manual.
否则,请尝试手册中的这些说明。
回答by Amadu Bah
try sudo dpkg-reconfigure mysql-server-5.5
and you will be asked for the new root password.
尝试sudo dpkg-reconfigure mysql-server-5.5
,您将被要求输入新的 root 密码。
Replace 5.5 with your current mysql-server version
将 5.5 替换为您当前的 mysql-server 版本
回答by charles zeng
You need to make sure you are running the command as root by using sudo command.
您需要确保使用 sudo 命令以 root 身份运行该命令。
I found this instructionfor resetting mysql password works well.
我发现这个重置 mysql 密码的指令很有效。
Following this procedure, you will disable access control on the MySQL server. All connexions will have a root access. It is a good thing to unplug your server from the network or at least disable remote access.
按照此过程,您将禁用 MySQL 服务器上的访问控制。所有连接都将具有根访问权限。从网络上拔下服务器或至少禁用远程访问是一件好事。
To reset your mysqld password just follow these instructions :
要重置您的 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 &
Because you are not checking user privs at this point, it's safest to disable networking. In Dapper, /usr/bin/mysqld... did not work. However, mysqld --skip-grant-tables did.
因为此时您没有检查用户权限,所以禁用网络是最安全的。在 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 root@'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';
For either method, once have received a message indicating a successful query (one or more rows affected), flush privileges:
对于任一方法,一旦收到指示成功查询的消息(受影响的一个或多个行),刷新权限:
FLUSH PRIVILEGES;
同花顺特权;
Then stop the mysqld process and relaunch it with the classical way:
然后停止 mysqld 进程并以经典方式重新启动它:
sudo /etc/init.d/mysql stop
须藤 /etc/init.d/mysql 停止
sudo /etc/init.d/mysql start
须藤 /etc/init.d/mysql 启动
回答by harpej
`UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; FLUSH PRIVILEGES;
`UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root'; 同花顺特权;
Write the UPDATE and FLUSH statements each on a single line. The UPDATE statement resets the password for all root accounts, and the FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.`
将 UPDATE 和 FLUSH 语句分别写在一行上。UPDATE 语句重置所有 root 帐户的密码,FLUSH 语句告诉服务器将授权表重新加载到内存中,以便它注意到密码更改。`
回答by StackUP
In my case was happening same but I was making mistake in logging into mysql prompt. I was doing by mysql instead of mysql -u root -p
在我的情况下发生了同样的事情,但我在登录 mysql 提示符时犯了错误。我是用 mysql 而不是 mysql -u root -p
回答by Urszula Karzelek
For mysql 5.7.16 on Ubuntu 16.04 this worked:
对于 Ubuntu 16.04 上的 mysql 5.7.16,这有效:
login without password:
sudo mysqld_safe --skip-grant-tables --skip-networking &
change password:
SET PASSWORD FOR 'root'@'localhost' = 'mypass';
无需密码登录:
须藤 mysqld_safe --skip-grant-tables --skip-networking &
更改密码:
为'root'@'localhost' = 'mypass' 设置密码;
Found solution on MySQL 5.7 Reference Manual
在MySQL 5.7 参考手册上找到解决方案