如何找回我的 MySQL 用户名和密码?

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

How do I retrieve my MySQL username and password?

mysql

提问by Marcel

I lost my MySQL username and password. How do I retrieve it?

我丢失了 MySQL 用户名和密码。我如何找回它?

回答by Xenph Yan

Stop the MySQL process.

Start the MySQL process with the --skip-grant-tables option.

Start the MySQL console client with the -u root option.

停止 MySQL 进程。

使用 --skip-grant-tables 选项启动 MySQL 进程。

使用 -u root 选项启动 MySQL 控制台客户端。

List all the users;

列出所有用户;

SELECT * FROM mysql.user;

Reset password;

重设密码;

UPDATE mysql.user SET Password=PASSWORD('[password]') WHERE User='[username]';


But DO NOT FORGETto

不要忘记,以

Stop the MySQL process

Start the MySQL Process normally (i.e. without the --skip-grant-tables option)

停止 MySQL 进程

正常启动 MySQL 进程(即没有 --skip-grant-tables 选项)

when you are finished. Otherwise, your database's security could be compromised.

当你完成时。否则,您的数据库的安全性可能会受到损害。

回答by jake

Unfortunately your user password is irretrievable. It has been hashed with a one way hash which if you don't know is irreversible. I recommend go with Xenph Yan above and just create an new one.

不幸的是,您的用户密码无法找回。它已经用单向散列进行了散列,如果您不知道它是不可逆的。我建议使用上面的 Xenph Yan 并创建一个新的。

You can also use the following procedure from the manualfor resetting the password for any MySQL rootaccounts on Windows:

您还可以使用手册中的以下过程重置Windows 上任何 MySQL帐户的密码:

  1. Log on to your system as Administrator.
  2. Stop the MySQL server if it is running. For a server that is running as a Windows service, go to the Services manager:
  1. 以管理员身份登录您的系统。
  2. 如果 MySQL 服务器正在运行,请停止它。对于作为 Windows 服务运行的服务器,请转到服务管理器:

Start Menu -> Control Panel -> Administrative Tools -> Services

开始菜单 -> 控制面板 -> 管理工具 -> 服务

Then find the MySQL service in the list, and stop it. If your server is not running as a service, you may need to use the Task Manager to force it to stop.

然后在列表中找到MySQL服务,并停止它。如果您的服务器没有作为服务运行,您可能需要使用任务管理器来强制它停止。

  1. Create a text file and place the following statements in it. Replace the password with the password that you want to use.

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

    The UPDATEand FLUSHstatements each must be written on a single line. The UPDATEstatement resets the password for all existing root accounts, and the FLUSHstatement tells the server to reload the grant tables into memory.

  2. Save the file. For this example, the file will be named C:\mysql-init.txt.
  3. Open a console window to get to the command prompt:

    Start Menu -> Run -> cmd

  4. Start the MySQL server with the special --init-fileoption:

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

    If you installed MySQL to a location other than C:\mysql, adjust the command accordingly.

    The server executes the contents of the file named by the --init-fileoption at startup, changing each rootaccount password.

    You can also add the --consoleoption to the command if you want server output to appear in the console window rather than in a log file.

    If you installed MySQL using the MySQL Installation Wizard, you may need to specify a --defaults-fileoption:

    C:\> "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt.exe" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.0\my.ini" --init-file=C:\mysql-init.txt
    

    The appropriate --defaults-filesetting can be found using the Services Manager:

    Start Menu -> Control Panel -> Administrative Tools -> Services

    Find the MySQL service in the list, right-click on it, and choose the Properties option. The Path to executable field contains the --defaults-filesetting.

  5. After the server has started successfully, delete C:\mysql-init.txt.
  6. Stop the MySQL server, then restart it in normal mode again. If you run the server as a service, start it from the Windows Services window. If you start the server manually, use whatever command you normally use.
  1. 创建一个文本文件并将以下语句放入其中。将密码替换为您要使用的密码。

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

    UPDATEFLUSH每个语句必须在一行内写完。的UPDATE语句重置所有现有的根帐户的密码,而FLUSH语句告诉服务器重装授权表到内存中。

  2. 保存文件。对于此示例,文件将命名为C:\mysql-init.txt。
  3. 打开控制台窗口以进入命令提示符:

    开始菜单 -> 运行 -> cmd

  4. 使用特殊的--init-file选项启动 MySQL 服务器:

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

    如果您将 MySQL 安装到C:\mysql以外的位置,请相应地调整命令。

    服务器在启动时执行由--init-file选项命名的文件的内容,更改每个root帐户密码。

    如果您希望服务器输出显示在控制台窗口而不是日志文件中,您还可以将--console选项添加到命令中。

    如果您使用 MySQL 安装向导安装 MySQL,则可能需要指定--defaults-file选项:

    C:\> "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt.exe" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.0\my.ini" --init-file=C:\mysql-init.txt
    

    可以使用服务管理器找到适当的--defaults-file设置:

    开始菜单 -> 控制面板 -> 管理工具 -> 服务

    在列表中找到 MySQL 服务,右键单击它,然后选择 Properties 选项。可执行文件路径包含--defaults-file设置。

  5. 服务器启动成功后,删除C:\mysql-init.txt
  6. 停止 MySQL 服务器,然后再次以正常模式重新启动它。如果您将服务器作为服务运行,请从 Windows 服务窗口启动它。如果您手动启动服务器,请使用您通常使用的任何命令。

You should now be able to connect to MySQL as root using the new password.

您现在应该能够使用新密码以 root 身份连接到 MySQL。

回答by ThinkingMonkey

An improvement to the most useful answer here:

对这里最有用的答案的改进:

1] No need to restart the mysql server
2] Security concern for a MySQL server connected to a network

1] 无需重新启动 mysql 服务器
2] 连接到网络的 MySQL 服务器的安全问题

There is no need to restart the MySQL server.

无需重新启动 MySQL 服务器。

use FLUSH PRIVILEGES;after the update mysql.user statement for password change.

FLUSH PRIVILEGES;在更新 mysql.user 语句后使用更改密码。

The FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.

FLUSH 语句告诉服务器将授权表重新加载到内存中,以便它注意到密码更改。

The --skip-grant-optionsenables anyone to connect without a password and with all privileges. Because this is insecure, you might want to

--skip-grant-options使任何人都可以在没有密码和所有权限的情况下进行连接。因为这是不安全的,你可能想要

use --skip-grant-tablesin conjunction with --skip-networkingto prevent remote clients from connecting.

--skip-grant-tables--skip-networking结合使用以防止远程客户端连接。

from: reference: resetting-permissions-generic

来自:参考:resetting-permissions-generic

回答by snemarch

While you can't directly recover a MySQL password without bruteforcing, there might be another way - if you've used MySQL Workbench to connect to the database, and have saved the credentials to the "vault", you're golden.

虽然您不能在没有暴力破解的情况下直接恢复 MySQL 密码,但可能还有另一种方法 - 如果您使用 MySQL Workbench 连接到数据库,并将凭据保存到“保险库”,那么您就是黄金。

On Windows, the credentials are stored in %APPDATA%\MySQL\Workbench\workbench_user_data.dat- encrypted with CryptProtectData(without any additional entropy). Decrypting is easy peasy:

在 Windows 上,凭据存储在%APPDATA%\MySQL\Workbench\workbench_user_data.dat- 使用CryptProtectData加密(没有任何额外的熵)。解密很简单:

std::vector<unsigned char> decrypt(BYTE *input, size_t length) {
    DATA_BLOB inblob { length, input };
    DATA_BLOB outblob;

    if (!CryptUnprotectData(&inblob, NULL, NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &outblob)) {
            throw std::runtime_error("Couldn't decrypt");
    }

    std::vector<unsigned char> output(length);
    memcpy(&output[0], outblob.pbData, outblob.cbData);

    return output;
}

Or you can check out this DonationCoder threadfor source + executable of a quick-and-dirty implementation.

或者,您可以查看此DonationCoder 线程以获取快速和脏实现的源代码 + 可执行文件。

回答by Sajjad Ashraf

If you have root access to the server where mysql is running you should stop the mysql server using this command

如果您对运行 mysql 的服务器具有 root 访问权限,则应使用此命令停止 mysql 服务器

sudo service mysql stop

Now start mysql using this command

现在使用这个命令启动mysql

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

Now you can login to mysql using

现在您可以使用登录到 mysql

sudo mysql
FLUSH PRIVILEGES;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

Full instructions can be found here http://www.techmatterz.com/recover-mysql-root-password/

可以在此处找到完整说明http://www.techmatterz.com/recover-mysql-root-password/

回答by S.M.Mousavi

Do it without down time

无需停机即可完成

Run following command in the Terminal to connect to the DBMS (you need root access):

在终端中运行以下命令以连接到 DBMS(您需要 root 访问权限):

sudo mysql -u root -p;

run update password of the target user (for my example username is mousaviand it's password must be 123456):

运行目标用户的更新密码(例如,用户名是mousavi,密码必须是123456):

UPDATE mysql.user SET authentication_string=PASSWORD('123456') WHERE user='mousavi';  

at this point you need to do a flush to apply changes:

此时您需要执行刷新以应用更改:

FLUSH PRIVILEGES;

Done! You did it without any stop or restart mysql service.

完毕!您没有停止或重新启动 mysql 服务就完成了。

回答by Syeful Islam

Login MySql from windows cmd using existing user:

使用现有用户从 windows cmd 登录 MySql:

mysql -u username -p
Enter password:****

mysql -u 用户名 -p
输入密码:****

Then run the following command:

然后运行以下命令:

mysql> SELECT * FROM mysql.user;

After that copy encrypted md5 password for corresponding user and there are several online password decrypted application available in web. Using this decrypt password and use this for login in next time. or update user password using flowing command:

在为相应用户复制加密的 md5 密码之后,网络上有几个在线密码解密应用程序可用。使用此解密密码并在下次登录时使用此密码。或使用流动命令更新用户密码:

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

Then login using the new password and user.

然后使用新密码和用户登录。

回答by user12261119

Save the file. For this example, the file will be named C:\mysql-init.txt. it asking administrative permisions for saving the file

保存文件。对于此示例,文件将命名为 C:\mysql-init.txt。它要求保存文件的管理权限

回答by samplesize1

IF you happen to have ODBC set up, you can get the password from the ODBC config file. This is in /etc/odbc.ini for Linux and in the Software/ODBC folder in the registry in Windows (there are several - it may take some hunting)

如果您碰巧设置了 ODBC,则可以从 ODBC 配置文件中获取密码。这在 Linux 的 /etc/odbc.ini 和 Windows 注册表中的 Software/ODBC 文件夹中(有几个 - 可能需要一些搜索)

回答by shine

Although a strict, logical, computer science'ish interpretation of the op's question would be to require both"How do I retrieve my MySQL username" and"password" - I thought It might be useful to someone to also address the ORinterpretation. In other words ...

虽然严格,逻辑,计算机science'ish运算的问题的解释是,要求双方“我怎么找回我的MySQL用户名”“密码” -我想这可能是有用的人也解决解释。换句话说 ...

1) How do I retrieve my MySQL username?

1) 如何找回我的 MySQL 用户名?

OR

或者

2) password

2) 密码

This latter condition seems to have been amply addressed already so I won't bother with it. The following is a solution for the case "How do i retreive my MySQL username" alone. HIH.

后一种情况似乎已经得到充分解决,所以我不会打扰它。以下是针对“How do i retreive my MySQL username”这个案例的解决方案。嗨。

To find your mysql username run the following commands from the mysql shell ...

要查找您的 mysql 用户名,请从 mysql shell 运行以下命令...

SELECT User FROM mysql.user;

从 mysql.user 中选择用户;

it will print a table of all mysql users.

它将打印所有 mysql 用户的表。