MySQL 根密码更改

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

MySQL root password change

mysqlrootmysqladmin

提问by nick

I have been trying to reset my MySQL root password. I have run the mysqld_safe --skip-grant-tables, updated the root password, and checked the user table to make sure it is there. Once restarting the mysql daemon I tried logging in with the new root password that I just set and still get Access denied for user 'root' errors. I have also tried completely removing and reinstalling mysql (including removing the my.cnf file) and still no luck. Does anyone have any suggestions on what I can do next?

我一直在尝试重置我的 MySQL 根密码。我已经运行了 mysqld_safe --skip-grant-tables,更新了 root 密码,并检查了用户表以确保它在那里。重新启动 mysql 守护程序后,我尝试使用刚刚设置的新 root 密码登录,但仍然因用户“root”错误而拒绝访问。我也尝试过完全删除并重新安装 mysql(包括删除 my.cnf 文件),但仍然没有运气。有人对我下一步可以做什么有任何建议吗?

Thanks in advance

提前致谢

采纳答案by nick

Found it! I forgot to hash the password when I changed it. I used this query to solve my problem:

找到了!我更改密码时忘记对密码进行哈希处理。我用这个查询来解决我的问题:

update user set password=PASSWORD('NEW PASSWORD') where user='root';

update user set password=PASSWORD('NEW PASSWORD') where user='root';

I forgot the PASSWORD('NEW PASSWORD')and just put in the new password in plain text

我忘记了PASSWORD('NEW PASSWORD'),只是以纯文本形式输入新密码

回答by kta

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

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

回答by VSB

hava a look at this from MySQL Reference manual:

请看一下 MySQL 参考手册中的内容:

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

Look at this page for more information: Resetting the Root Password: Unix Systems

查看此页面以获取更多信息:重置根密码:Unix 系统

回答by Robert Anthony S. Tribiana

This is the updated answer for WAMP v3.0.6 and up

这是 WAMP v3.0.6 及更高版本的更新答案

> UPDATE mysql.user 
> SET authentication_string=PASSWORD('MyNewPass') 
> WHERE user='root';

> FLUSH PRIVILEGES;

In MySQL version 5.7.x there is no more password field in the mysql table. It was replaced with authentication_string. (This is for the terminal/CLI)

在 MySQL 5.7.x 版中,mysql 表中不再有密码字段。它被 authentication_string 取代。(这是用于终端/CLI)

UPDATE mysql.user SET authentication_string=PASSWORD('MyNewPass') WHERE user='root';

FLUSH PRIVILEGES;

(This if for PHPMyAdmin or any Mysql GUI)

(这对于 PHPMyAdmin 或任何 Mysql GUI)

回答by iamfaith

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

You can find Resetting the Root Passwordin the MySQL documentation.

您可以在 MySQL 文档中找到重置根密码

回答by Lokesh G

Please follow the below steps.

请按照以下步骤操作。

step1. stop mysql

第1步。停止mysql

step2. sudo mysqld_safe --skip-grant-tables

第2步。 sudo mysqld_safe --skip-grant-tables

step3.mysql -u root

第三步。mysql -u root

step4.use mysql;

第四步。use mysql;

step5.show tables;

第五步。show tables;

step6.describe user;

第六步。describe user;

step7.update user set authentication_string=password('1111') where user='root';

第七步。update user set authentication_string=password('1111') where user='root';

login with password 1111

使用密码 1111 登录

回答by Bamboomy

I searched around as well and probably some answers do fit for some situations,

我也四处搜索,可能有些答案适合某些情况,

my situation is Mysql 5.7 on a Ubuntu 18.04.2 LTS system:

我的情况是 Ubuntu 18.04.2 LTS 系统上的 Mysql 5.7:

(get root privileges)

(获得root权限)

$ sudo bash

(set up password for root db user + implement security in steps)

(为root db用户设置密码+分步实现安全)

# mysql_secure_installation

(give access to the root user via password in stead of socket)

(通过密码而不是套接字来访问 root 用户)

(+ edit: apparently you need to set the password again?)

(+ 编辑:显然您需要再次设置密码?)

(don't set it to 'mySecretPassword'!!!)

(不要将其设置为“mySecretPassword”!!!)

# mysql -u root

mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> set password for 'root'@'localhost' = PASSWORD('mySecretPassword'); 
mysql> FLUSH PRIVILEGES;
mysql> exit;

# service mysql restart

Many thanks to zetacu(and erich) for this excellent answer(after searching a couple of hours...)

非常感谢zetacu(和erich)提供了这个出色的答案(搜索了几个小时后......)

Enjoy :-D

享受:-D

S.

S。

回答by Carlito

On MySQL 8.0.4+

在 MySQL 8.0.4+ 上

To update current root user:

更新当前 root 用户:

select current_user();
set password = 'new_password';

To update other user:

更新其他用户:

set password for 'otherUser'@'localhost' = 'new_password';

To set password policy before updating password:

在更新密码前设置密码策略:

set global validate_password.policy = 0;
set password = 'new_password';
set password for 'otherUser'@'localhost' = 'new_password';

Other / better way to update root password:

更新 root 密码的其他/更好的方法:

mysql_secure_installation

Want to stick with 5.x authentication so you can still use legacy apps?

想要坚持使用 5.x 身份验证,以便您仍然可以使用旧版应用程序吗?

On my.cnf

my.cnf

default_authentication_plugin = mysql_native_password

To update root:

要更新根:

set global validate_password.policy = 0;
alter user 'root'@'localhost' identified with mysql_native_password by 'new_password';

回答by LV98

This worked for me -

这对我有用-

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

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

https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html

https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html

回答by tk_

You have to reset the password! steps for mac osx(tested and working) and ubuntu

您必须重置密码!mac osx(测试和工作)和 ubuntu 的步骤

Stop MySQL

停止 MySQL

$ 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';

Start MySQL

启动 MySQL

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

your new password is 'password'.

您的新密码是“密码”。