MySQL 使用命令行更改mysql用户密码

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

Change mysql user password using command line

mysqlcommand-lineputty

提问by user3310572

I'm trying to update the password for a database user using the command line, and it's not working for me. This is the code I'm using:

我正在尝试使用命令行更新数据库用户的密码,但它对我不起作用。这是我正在使用的代码:

mysql> UPDATE user SET password=PASSWORD($w0rdf1sh) WHERE user='tate256';

Could someone tell me what's wrong with this code.

有人能告诉我这段代码有什么问题吗?

回答by hellboy

In your code, try enclosing password inside single quote. Alternatively, as per the documentationof mysql, following should work -

在您的代码中,尝试将密码括在单引号内。或者,根据mysql的文档,以下应该有效 -

SET PASSWORD FOR 'jeffrey'@'localhost' = PASSWORD('cleartext password');

FLUSH PRIVILEGES;

The last line is important or else your password change won't take effect unfortunately.

最后一行很重要,否则很遗憾您的密码更改不会生效。

EDIT:

编辑:

I ran a test in my local and it worked -

我在本地进行了测试,结果奏效了 -

mysql>  set password for 'test' = PASSWORD('$w0rdf1sh');
Query OK, 0 rows affected (0.00 sec)

Mine is version 5. You can use following command to determine your version -

我的是第 5 版。您可以使用以下命令来确定您的版本 -

SHOW VARIABLES LIKE "%version%";

回答by Govind Rai

As of MySQL 5.7.6, use ALTER USER

从 MySQL 5.7.6 开始,使用 ALTER USER

Example:

例子:

ALTER USER 'username' IDENTIFIED BY 'password';

Because:

因为:

  • SET PASSWORD ... = PASSWORD('auth_string')syntax is deprecated as of MySQL 5.7.6 and will be removed in a future MySQL release.

  • SET PASSWORD ... = 'auth_string'syntax is not deprecated, but ALTER USERis now the preferred statement for assigning passwords.

  • SET PASSWORD ... = PASSWORD('auth_string')从 MySQL 5.7.6 开始不推荐使用语法,并将在未来的 MySQL 版本中删除。

  • SET PASSWORD ... = 'auth_string'语法没有被弃用,但ALTER USER现在是分配密码的首选语句。

回答by vijay kumar

Note: u should login as root user

注意:您应该以root用户身份登录

 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your password');

回答by Robert Anthony S. Tribiana

this is the updated answer for WAMP v3.0.6

这是 WAMP v3.0.6 的更新答案

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

FLUSH PRIVILEGES;

回答by David Silva Smith

Before MySQL 5.7.6 this works from the command line:

在 MySQL 5.7.6 之前,这可以从命令行运行:

mysql -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('$w0rdf1sh');"

I don't have a mysql install to test on but I think in your case it would be

我没有要测试的 mysql 安装,但我认为在你的情况下它会是

mysql -e "UPDATE mysql.user SET Password=PASSWORD('$w0rdf1sh') WHERE User='tate256';"

回答by Napolean

In windows 10, just exit out of current login and run this on command line

在 Windows 10 中,只需退出当前登录并在命令行上运行它

--> mysqladmin -u root password “newpassword”

--> mysqladmin -u root password “newpassword”

where instead of root could be any user.

代替 root 的地方可以是任何用户。

回答by mykoman

As of MySQL 8.0.18 This works fine for me

从 MySQL 8.0.18 开始,这对我来说很好用

mysql> SET PASSWORD FOR 'user'@'localhost' = 'userpassword';

回答by Kirti Nikam

This works for me. Got solution from MYSQL webpage

这对我有用。从MYSQL 网页得到解决方案

In MySQL run below queries:

在 MySQL 中运行以下查询:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'New_Password';

回答by user353gre3

Your login root should be /usr/local/directadmin/conf/mysql.conf. Then try following

您的登录根目录应该是/usr/local/directadmin/conf/mysql.conf. 然后尝试以下

UPDATE mysql.user SET password=PASSWORD('$w0rdf1sh') WHERE user='tate256' AND Host='10.10.2.30';
FLUSH PRIVILEGES;

Host is your mysql host.

主机是您的 mysql 主机。