MySQL 如何找出MySQL root密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10895163/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:40:23  来源:igfitidea点击:

How to find out the MySQL root password

mysqldatabase

提问by Om3ga

I cannot figure out my MySQL root password; how can I find this out? Is there any file where this password is stored?

我不知道我的 MySQL 根密码;我怎样才能找到这个?是否有任何文件存储此密码?

I am following this linkbut I do not have directadmin directory in local.

我正在关注此链接,但我在本地没有 directadmin 目录。

回答by eeezyy

thanks to @thusharaK I could reset the root password without knowing the old password.

感谢@thusharaK,我可以在不知道旧密码的情况下重置 root 密码。

On ubuntu I did the following:

在 ubuntu 上我做了以下事情:

sudo service mysql stop
sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking

Then run mysql in a new terminal:

然后在新终端中运行 mysql:

mysql -u root

And run the following queries to change the password:

并运行以下查询以更改密码:

UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root';
FLUSH PRIVILEGES;

In MySQL 5.7, the password field in mysql.user table field was removed, now the field name is 'authentication_string'.

在 MySQL 5.7 中,mysql.user 表字段中的密码字段被删除,现在字段名称为“authentication_string”。

Quit the mysql safe mode and start mysql service by:

退出mysql安全模式并通过以下方式启动mysql服务:

mysqladmin shutdown
sudo service mysql start

回答by tk_

You can't view the hashed password; the only thing you can do is reset it!

您无法查看散列密码;你唯一能做的就是重置它!

Stop MySQL:

停止 MySQL:

sudo service mysql stop

or

或者

$ sudo /usr/local/mysql/support-files/mysql.server stop

Start it in safe mode:

以安全模式启动它:

$ sudo mysqld_safe --skip-grant-tables

(above line is the whole command)

(上一行是整个命令)

This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:

这将是一个持续的命令,直到该过程完成,所以打开另一个 shell/终端窗口,在没有密码的情况下登录:

$ mysql -u root

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

MySQL 5.7 and over:

MySQL 5.7 及以上:

mysql> use mysql; 
mysql> update user set authentication_string=password('password') where user='root'; 

Start MySQL:

启动 MySQL:

sudo mysql start

or

或者

sudo /usr/local/mysql/support-files/mysql.server start

Your new password is 'password'.

您的新密码是“密码”。

回答by Vlad

MySQL 5.7 and above saves root in MySQL log file.

MySQL 5.7 及以上版本将 root 保存在 MySQL 日志文件中。

Please try this:

请试试这个:

sudo grep 'temporary password' /var/log/mysqld.log

回答by lanzz

You cannot find it. It is stored in a database, which you need the root password to access, and even if you did get access somehow, it is hashed with a one-way hash. You can reset it: http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

你找不到它。它存储在数据库中,您需要 root 密码才能访问该数据库,即使您确实以某种方式获得了访问权限,它也会使用单向散列进行散列。您可以重置它:http: //dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

回答by Kit Ramos

one thing that tripped me up on a new install of mySQL and wonder why I couldn't get the default password to work and why even the reset methods where not working. well turns out that on Ubuntu 18 the most recent version of mysql server does not use password auth at all for the root user by default. So this means it doesn't matter what you set it to, it won't let you use it. it's expecting you to login from a privileged socket. so

一件事让我在新安装的 mySQL 上绊倒了,想知道为什么我无法使用默认密码,以及为什么即使是重置方法也不起作用。事实证明,在 Ubuntu 18 上,默认情况下,最新版本的 mysql 服务器根本不为 root 用户使用密码身份验证。所以这意味着不管你把它设置成什么,它不会让你使用它。它希望您从特权套接字登录。所以

mysql -u root -p

will not work at all, even if you are using the correct password!!! it will deny access no matter what you put in.

即使您使用正确的密码,也根本无法工作!!!无论您输入什么,它都会拒绝访问。

Instead you need to use

相反,您需要使用

sudo mysql

that will work with out any password. then once you in you need type in

这将在没有任何密码的情况下工作。然后一旦你进入你需要输入

 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password you want to use';

then log out and now the bloody thing will finally accept your password

然后注销,现在该死的东西最终会接受您的密码

回答by Lokesh kumar Chippada

Follow these steps to reset password in Windows system

按照以下步骤在 Windows 系统中重置密码

  1. Stop Mysql service from task manager

  2. Create a text file and paste the below statement

  1. 从任务管理器停止 Mysql 服务

  2. 创建一个文本文件并粘贴以下语句

MySQL 5.7.5 and earlier:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yournewpassword');

MySQL 5.7.5 及更早版本:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yournewpassword');



MySQL 5.7.6 and later:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'yournewpassword';

MySQL 5.7.6 及更高版本:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'yournewpassword';

  1. Save as mysql-init.txtand place it in 'C' drive.

  2. Open command prompt and paste the following

  1. 另存为mysql-init.txt并将其放入'C' drive.

  2. 打开命令提示符并粘贴以下内容

C:\> mysqld --init-file=C:\\mysql-init.txt

C:\> mysqld --init-file=C:\\mysql-init.txt

回答by mszmurlo

Unless the package manager requests you to type the root password during installation, the default root password is the empty string. To connect to freshly installed server, type:

除非包管理器要求您在安装过程中输入 root 密码,否则默认的 root 密码为空字符串。要连接到新安装的服务器,请键入:

shell> mysql -u root --password=
mysql>

To change the password, get back the unix shell and type:

要更改密码,请返回 unix shell 并键入:

shell> mysqladmin -u root --password= password root

The new password is 'root'. Now connect to the server:

新密码是“root”。现在连接到服务器:

shell> mysql -u root --password=
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Oops, the password has changed. Use the new one, root:

糟糕,密码已更改。使用新的,root

shell> mysql -u root --password=root
...
blah, blah, blah : mysql welcome banner
...
mysql> 

Bingo! New do something interesting

答对了!新做一些有趣的事情

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

Maurycy

莫里西

回答by Ohad Cohen

As addition to the other answers, in a cpanel installation, the mysql root password is stored in a file named /root/.my.cnf. (and the cpanel service resets it back on change, so the other answers here won't help)

除了其他答案之外,在 cpanel 安装中,mysql root 密码存储在名为/root/.my.cnf. (并且 cpanel 服务会在更改时将其重置,因此此处的其他答案无济于事)

回答by user889030

you can view mysql root password , well i have tried it on mysql 5.5 so do not know about other new version well work or not

你可以查看mysql root密码,我已经在mysql 5.5上试过了,所以不知道其他新版本是否好用

nano ~/.my.cnf

回答by Light

This worked for me:

这对我有用:

On terminal type the following

在终端输入以下内容

$ sudo mysql -u root -p

$ sudo mysql -u root -p

Enter password://just press enter

输入密码://只需按回车

mysql>

mysql>