如何找出我的 MySQL 根密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5683154/
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
How do I find out my root MySQL password?
提问by Genadinik
I just installed MySQL on Ubuntu and the root user can't log in :)
我刚刚在 Ubuntu 上安装了 MySQL,root 用户无法登录:)
How can I recover or find out my password? Using blank for password does not work.
如何恢复或找出我的密码?使用空白作为密码不起作用。
回答by Benjamin Manns
You can reset the root password by running the server with --skip-grant-tablesand logging in without a password by running the following as root (or with sudo):
您可以通过以 root 用户--skip-grant-tables身份(或使用 sudo)运行以下命令,通过使用密码运行服务器并在不使用密码的情况下登录来重置 root 密码:
# service mysql stop
# mysqld_safe --skip-grant-tables &
$ mysql -u root
mysql> use mysql;
mysql> update user set authentication_string=PASSWORD("YOUR-NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
# service mysql stop
# service mysql start
$ mysql -u root -p
Now you should be able to login as root with your new password.
现在您应该能够使用您的新密码以 root 身份登录。
It is also possible to find the query that reset the password in /home/$USER/.mysql_historyor /root/.mysql_historyof the user who reset the password, but the above will always work.
也可以找到查询,在重置密码/home/$USER/.mysql_history或/root/.mysql_history谁重置密码的用户,但上面永远是可行的。
Note: prior to MySQL 5.7 the column was called passwordinstead of authentication_string. Replace the line above with
注意:在 MySQL 5.7 之前,该列被称为password而不是authentication_string. 将上面的行替换为
mysql> update user set password=PASSWORD("YOUR-NEW-ROOT-PASSWORD") where User='root';
回答by theinfinitenigma
I realize that this is an old thread, but I thought I'd update it with my results.
我意识到这是一个旧线程,但我想我会用我的结果更新它。
Alex, it sounds like you installed MySQL server via the meta-package 'mysql-server'. This installs the latest package by reference (in my case, mysql-server-5.5). I, like you, was not prompted for a MySQL password upon setup as I had expected. I suppose there are two answers:
亚历克斯,听起来你是通过元包“mysql-server”安装了 MySQL 服务器。这将通过引用安装最新的包(在我的情况下,mysql-server-5.5)。我和你一样,在设置时没有像我预期的那样提示输入 MySQL 密码。我想有两个答案:
Solution #1: install MySQL by it's full name:
解决方案#1:按全名安装MySQL:
$ sudo apt-get install mysql-server-5.5
Or
或者
Solution #2: reconfigure the package...
解决方案#2:重新配置包...
$ sudo dpkg-reconfigure mysql-server-5.5
You must specific the full package name. Using the meta-package 'mysql-server' did not have the desired result for me. I hope this helps someone :)
您必须指定完整的包名称。使用元包 'mysql-server' 没有得到我想要的结果。我希望这可以帮助别人 :)
Reference: https://help.ubuntu.com/12.04/serverguide/mysql.html
回答by Alex Ivasyuv
sudo mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD_HERE';
FLUSH PRIVILEGES;
mysql -u root -p # and it works
回答by eebbesen
MySQL 5.5 on Ubuntu 14.04 required slightly different commands as recommended here. In a nutshell:
Ubuntu 14.04 上的 MySQL 5.5 需要此处推荐的略有不同的命令。简而言之:
sudo /etc/init.d/mysql stop
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
mysql -u root
And then from the MySQL prompt
然后从 MySQL 提示符
FLUSH PRIVILEGES;
SET PASSWORD FOR root@'localhost' = PASSWORD('password');
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
FLUSH PRIVILEGES;
And the cited sourceoffers an alternate method as well.
和所引用的源提供的另一种方法为好。
回答by Gurumurthy
For RHEL-mysql 5.5:
对于 RHEL-mysql 5.5:
/etc/init.d/mysql stop
/etc/init.d/mysql start --skip-grant-tables
mysql> UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
mysql -uroot -pnewpwd
mysql>
回答by Stevan Tosic
There is a simple solution.
有一个简单的解决方案。
MySql 5.7 comes with anonymous user so you need to reconfigure MySQL server.
MySql 5.7 自带匿名用户,所以你需要重新配置 MySQL 服务器。
You can do that with this command
你可以用这个命令做到这一点
try to find temp pass:
尝试找到临时通行证:
grep 'temporary password' /var/log/mysqld.log
then:
然后:
sudo mysql_secure_installation
On this link is more info about mysql 5.7
在此链接上是有关 mysql 5.7 的更多信息
https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html
https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html
回答by Joseph Briggs
Hmm Mysql 5.7.13 to reset all I did was:
嗯 Mysql 5.7.13 重置我所做的一切是:
$ sudo service mysql stopTo stop mysql
$ sudo service mysql stop停止mysql
$ mysqld_safe --skip-grant-tables &Start mysql
$ mysqld_safe --skip-grant-tables &启动mysql
$ mysql -u root
$ mysql -u root
Just like the correct answer. Then all I did was do what @eebbesen did.
就像正确答案一样。然后我所做的就是做@eebbesen所做的事情。
mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('NEW-password-HERE');
mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('NEW-password-HERE');
Hope it helps anyone out there :)
希望它可以帮助那里的任何人:)
回答by Irshad Khan
Here is the best way to set your root password : Source LinkStep 3 is working perfectly for me.
这是设置 root 密码的最佳方法:Source LinkStep 3 对我来说非常适合。
Commands for You
给你的命令
- sudo mysql
- SELECT user,authentication_string,plugin,host FROM mysql.user;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- FLUSH PRIVILEGES;
- SELECT user,authentication_string,plugin,host FROM mysql.user;
- exit
- 须藤mysql
- 从 mysql.user 中选择用户、authentication_string、插件、主机;
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- 同花顺特权;
- 从 mysql.user 中选择用户、authentication_string、插件、主机;
- 出口
Now you can use the Password for the root user is 'password' :
现在您可以使用 root 用户的密码为“密码”:
- mysql -u root -p
- CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';
- GRANT ALL PRIVILEGES ON .TO 'sammy'@'localhost' WITH GRANT OPTION;
- FLUSH PRIVILEGES;
- exit
- mysql -u 根 -p
- CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';
- 授予所有特权。TO 'sammy'@'localhost' WITH GRANT OPTION;
- 同花顺特权;
- 出口
Test your MySQL Service and Version:
测试您的 MySQL 服务和版本:
systemctl status mysql.service
sudo mysqladmin -p -u root version
回答by Santhosh Kalisamy
It is actually very simple. You don't have to go through a lot of stuff. Just run the following command in terminal and follow on-screen instructions.
其实很简单。你不必经历很多事情。只需在终端中运行以下命令并按照屏幕上的说明进行操作。
sudo mysql_secure_installation
回答by u445908
Under MYSQL 5.7, If you are using mysql for development purpose, just :
在 MYSQL 5.7 下,如果您使用 mysql 进行开发,只需:
1.kill mysql :
1.杀死mysql:
$ sudo service mysql stop
2.start mysql under --skip-grant-tables mode:
2.在--skip-grant-tables模式下启动mysql:
$ sudo mysqld_safe --skip-grant-tables
and, further, you could try to change the user table under "skip-grant-table" mode, however I failed.
此外,您可以尝试在“skip-grant-table”模式下更改用户表,但是我失败了。
so, this is just a workaround.
所以,这只是一种解决方法。

