如何重置或更改 MySQL 根密码?

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

How to reset or change the MySQL root password?

mysqlubuntuphpmyadmin

提问by asm234

How do I change the MySQL root password and username in ubuntu server? Do I need to stop the mysql service before setting any changes?

如何在 ubuntu 服务器中更改 MySQL 根密码和用户名?在设置任何更改之前是否需要停止 mysql 服务?

I have a phpmyadmin setup as well, will phpmyadmin get updated automatically?

我也有一个 phpmyadmin 设置,phpmyadmin 会自动更新吗?

回答by Christian Mark

Set / change / reset the MySQL root password on Ubuntu Linux. Enter the following lines in your terminal.

在 Ubuntu Linux 上设置/更改/重置 MySQL 根密码。在终端中输入以下行。

  1. Stop the MySQL Server: sudo /etc/init.d/mysql stop
  2. Start the mysqldconfiguration: sudo mysqld --skip-grant-tables &

    In some cases, you've to create the /var/run/mysqldfirst:

    sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld
    
  3. Run: sudo service mysql start
  4. Login to MySQL as root: mysql -u root mysql
  5. Replace YOURNEWPASSWORDwith your new password:

    UPDATE
      mysql.user
    SET
      Password = PASSWORD('YOURNEWPASSWORD')
    WHERE
      User = 'root';
    FLUSH PRIVILEGES;
    exit;
    
  1. 停止 MySQL 服务器: sudo /etc/init.d/mysql stop
  2. 开始mysqld配置:sudo mysqld --skip-grant-tables &

    在某些情况下,您必须创建第/var/run/mysqld一个:

    sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld
    
  3. 跑: sudo service mysql start
  4. 以 root 身份登录 MySQL: mysql -u root mysql
  5. 替换YOURNEWPASSWORD为您的新密码:

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

Note: on some versions, if passwordcolumn doesn't exist, you may want to try:
UPDATE user SET authentication_string=password('YOURNEWPASSWORD') WHERE user='root';

注意:在某些版本上,如果password列不存在,您可能想尝试:
UPDATE user SET authentication_string=password('YOURNEWPASSWORD') WHERE user='root';

Note: This method is not regarded as the most secure way of resetting the password, however, it works.

注意:此方法不被视为最安全的密码重置方式,但它确实有效。

References:

参考:

  1. Set / Change / Reset the MySQL root password on Ubuntu Linux
  2. How to Reset the Root Password
  1. 在 Ubuntu Linux 上设置/更改/重置 MySQL 根密码
  2. 如何重置根密码

回答by narko

The only method that worked for me is the one described here(I am running ubuntu 14.04). For the sake of clarity, these are the steps I followed:

唯一对我有用的方法是这里描述的方法(我正在运行 ubuntu 14.04)。为了清楚起见,这些是我遵循的步骤:

  1. sudo vim /etc/mysql/my.cnf
  2. Add the following lines at the end:

    [mysqld]
    
    skip-grant-tables
  3. sudo service mysql restart

  4. mysql -u root

  5. use mysql

  6. select * from mysql.user where user = 'root';- Look at the top to determine whether the password column is called passwordor authentication_string

  7. UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost';- Use the proper password column from above

  8. FLUSH PRIVILEGES;

  9. exit

  10. sudo vim /etc/mysql/my.cnf

  11. Remove the lines added in step 2if you want to keep your security standards.

  12. sudo service mysql restart

  1. sudo vim /etc/mysql/my.cnf
  2. 在最后添加以下几行:

    [mysqld]
    
    skip-grant-tables
  3. sudo service mysql restart

  4. mysql -u root

  5. use mysql

  6. select * from mysql.user where user = 'root';- 查看顶部确定密码列是叫 password还是authentication_string

  7. UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost';- 使用上面的正确密码列

  8. FLUSH PRIVILEGES;

  9. exit

  10. sudo vim /etc/mysql/my.cnf

  11. 如果您想保持安全标准,请删除步骤 2 中添加的行

  12. sudo service mysql restart

For reference : https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

供参考:https: //dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

回答by user12345

The official and easy way to reset the root password on an ubuntu server...

在 ubuntu 服务器上重置 root 密码的官方且简单的方法...

If you are on 16.04, 14.04, 12.04:

如果您在 16.04、14.04、12.04:

sudo dpkg-reconfigure mysql-server-5.5

If you are on 10.04:

如果您使用的是 10.04:

sudo dpkg-reconfigure mysql-server-5.1

If you are not sure which mysql-server version is installed you can try:

如果您不确定安装了哪个 mysql-server 版本,您可以尝试:

dpkg --get-selections | grep mysql-server

Updated notes for mysql-server-5.7

mysql-server-5.7 的更新说明

Note that if you are using mysql-server-5.7 you can not use the easier dpkg-reconfigure method shown above.

请注意,如果您使用的是 mysql-server-5.7,则不能使用上面显示的更简单的 dpkg-reconfigure 方法。

If you know the password, login and run this:

如果您知道密码,请登录并运行:

UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE USER='root';
FLUSH PRIVILEGES;

Alternatively, you can use the following:

或者,您可以使用以下方法:

sudo mysql_secure_installation

This will ask you a series of questions about securing your installation (highly recommended), including if you want to provide a new root password.

这将询问您一系列有关保护安装的问题(强烈推荐),包括是否要提供新的 root 密码。

If you do NOT know the root password, refer to this Ubuntu-centric write up on the process.

如果您不知道 root 密码,请参考这篇以 Ubuntu 为中心的关于进程的文章

See for more info:

查看更多信息:

https://help.ubuntu.com/16.04/serverguide/mysql.htmlhttps://help.ubuntu.com/14.04/serverguide/mysql.html

https://help.ubuntu.com/16.04/serverguide/mysql.html https://help.ubuntu.com/14.04/serverguide/mysql.html

回答by Anvesh

I am sharing the step by step final solution to reset a MySQL password in Linux Ubuntu.

我正在分享在 Linux Ubuntu 中重置 MySQL 密码的分步最终解决方案。

Reference taken from blog (dbrnd.com)

参考来自博客 (dbrnd.com)

Step 1: Stop MySQL Service.

第 1 步:停止 MySQL 服务。

sudo stop mysql

Step 2: Kill all running mysqld.

第 2 步:杀死所有正在运行的 mysqld。

sudo killall -9 mysqld

Step 3: Starting mysqld in Safe mode.

第 3 步:以安全模式启动 mysqld。

sudo mysqld_safe --skip-grant-tables --skip-networking &

Step 4: Start mysql client

第四步:启动mysql客户端

mysql -u root

Step 5: After successful login, please execute this command to change any password.

第五步:登录成功后,请执行此命令更改任何密码。

FLUSH PRIVILEGES;

Step 6: You can update mysql root password .

第 6 步:您可以更新 mysql root 密码。

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

Note: On MySQL 5.7, column Passwordis called authentication_string.

注意:在 MySQL 5.7 上, columnPassword被称为authentication_string

Step 7: Please execute this command.

第 7 步:请执行此命令。

FLUSH PRIVILEGES;

回答by fabriciofreitag

What worked for me (Ubuntu 16.04, mysql 5.7):

什么对我有用(Ubuntu 16.04,mysql 5.7):

Stop MySQL

停止 MySQL

sudo service mysql stop

Make MySQL service directory.

制作 MySQL 服务目录。

sudo mkdir /var/run/mysqld

Give MySQL user permission to write to the service directory.

授予 MySQL 用户写入服务目录的权限。

sudo chown mysql: /var/run/mysqld

Start MySQL manually, without permission checks or networking.

手动启动 MySQL,无需权限检查或联网。

sudo mysqld_safe --skip-grant-tables --skip-networking &

On another console, log in without a password.

在另一个控制台上,无需密码即可登录。

mysql -uroot mysql

Then:

然后:

UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
EXIT;

Turn off MySQL.

关闭 MySQL。

sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

Start the MySQL service normally.

正常启动MySQL服务。

sudo service mysql start

回答by Jerfeson Guerreiro

I faced problems with ubuntu 18.04 and mysql 5.7, this is the solution

我遇到了 ubuntu 18.04 和 mysql 5.7 的问题,这是解决方案

Try restart mysql-server before execution the comands

在执行命令之前尝试重新启动 mysql-server

sudo service mysql restart

MYSQL-SERVER >= 5.7

MYSQL 服务器 >= 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET authentication_string=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;

MYSQL-SERVER < 5.7

MYSQL 服务器 < 5.7

sudo mysql -uroot -p
USE mysql;
UPDATE user SET password=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;

回答by faisalbhagat

Change the MySQL root password.

更改 MySQL 根密码。

This method exposes the password to the command-line history, these commands should be run as root.

此方法将密码公开给命令行历史记录,这些命令应以 root 身份运行。

  1. Login through mysql command line tool:

    mysql -uroot -poldpassword
    
  2. Run this command:

    SET PASSWORD FOR root@'localhost' = PASSWORD('newpassword');
    
  1. 通过mysql命令行工具登录:

    mysql -uroot -poldpassword
    
  2. 运行此命令:

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

or

或者

  1. Run this command, which sets a password for the current user ('root' for this case) :

    SET PASSWORD = PASSWORD('newpassword');

  1. 运行此命令,为当前用户设置密码(本例中为“root”):

    SET PASSWORD = PASSWORD('新密码');

回答by Amir karim

At first run this command:

首先运行这个命令:

sudo mysql

and then you should check which authentication method of your MySQL user accounts use.So run this command

然后你应该检查你的 MySQL 用户帐户使用的身份验证方法。所以运行这个命令

SELECT user,authentication_string,plugin,host FROM mysql.user;

now you can see something like this already :

现在你已经可以看到这样的事情了:

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

in the table that is in the above , you can see that all of your mysql users accounts status & if you have set a password for root account before you see mysql_native_passwordin plugin column instead auth_socket. All in all for change your root password you should run :

在上面的表格中,您可以看到您所有的 mysql 用户帐户状态以及是否mysql_native_password在插件列中看到之前为 root 帐户设置了密码auth_socket。总而言之,要更改您的 root 密码,您应该运行:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Be sure to change password to a strong password of your choosing. Then for reload your server to put your new changes into effect run this;

请务必将密码更改为您选择的强密码。然后为了重新加载您的服务器以使您的新更改生效,请运行此命令;

FLUSH PRIVILEGES;

So again check the authentication methods which has employed by your mysql , by this command:

因此,通过以下命令再次检查您的 mysql 使用的身份验证方法:

SELECT user,authentication_string,plugin,host FROM mysql.user;

and now the output is :

现在输出是:

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+

as you can see in the grant table your root account has mysql_native_password. now you can exit MYSQL shell

正如您在授予表中看到的,您的根帐户拥有mysql_native_password. 现在你可以退出 MYSQL shell

exit;

That's it.just you should restart mysql by sudo service mysql restart. Now you can login to mysql as a root account with your password easily.

就是这样。只是你应该重新启动mysql sudo service mysql restart。现在您可以使用您的密码轻松地以 root 帐户登录到 mysql。

回答by user2206324

If you would like to change the MySQL root password, in a terminal enter:

如果您想更改 MySQL 根密码,请在终端中输入:

sudo dpkg-reconfigure mysql-server-5.5

sudo dpkg-reconfigure mysql-server-5.5

The MySQL daemon will be stopped, and you will be prompted to enter a new password.

MySQL 守护进程将停止,并提示您输入新密码。

回答by lizard

This works like charm I did it for Ubuntu 16.04. Full credit to below link as I got it from there. [https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts][1]

这就像我为 Ubuntu 16.04 所做的魅力一样。当我从那里得到它时,完全归功于以下链接。[ https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts][1]

Stop MySQL

停止 MySQL

sudo service mysql stop

Make MySQL service directory. sudo mkdir /var/run/mysqld

制作 MySQL 服务目录。须藤 mkdir /var/run/mysqld

Give MySQL user permission to write to the service directory.

授予 MySQL 用户写入服务目录的权限。

sudo chown mysql: /var/run/mysqld

Start MySQL manually, without permission checks or networking.

手动启动 MySQL,无需权限检查或联网。

sudo mysqld_safe --skip-grant-tables --skip-networking &

Log in without a password.

无需密码即可登录。

mysql -uroot mysql

Update the password for the root user. make sure at atleast root account gets updated by the below query. make some selection and check the existing values if you like

更新 root 用户的密码。确保至少 root 帐户通过以下查询进行更新。如果您愿意,请进行一些选择并检查现有值

UPDATE mysql.user SET 
authentication_string=PASSWORD('YOURNEWPASSWORD'), 
plugin='mysql_native_password' WHERE User='root';
EXIT;

Turn off MySQL.

关闭 MySQL。

sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown

Start the MySQL service normally.

正常启动MySQL服务。

sudo service mysql start