mysql如何修复用户'root'@'localhost'拒绝访问


123

在搞砸之前,请使用登录$ mysql -u root -p并显示数据库:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| game_data          |
| test               |
+--------------------+

然后,我尝试创建一个新用户,并发现PRIVILEGES出了点问题。

因此,我删除了新用户,并且我想我意外删除了“ root”和“ Admin”。

然后,我尝试再次创建“ root”,但是在授予所有特权时出现“访问被拒绝”错误。

mysql> CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'password' with grant option;
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

如果我使用再次登录到MySQL $ mysql -u root -p并显示数据库,

+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+

所有其他数据库都消失了。

现在如何修复MySQL?

我找不到数据库“ mysql”,无法创建数据库,无法创建用户,我尝试做的任何事情都会出错。

错误1045(28000):用户'root'@'localhost'的访问被拒绝(使用密码:是)。

我应该使用MacPorts重新安装MySQL吗?如果重新安装,我将丢失数据库game_data,对吗?


尝试使用与“ root” @“ localhost”不同的“ root” @“ 127.0.0.1”访问服务器。然后发出命令以创建用户'root'@'localhost'并为其授予所有特权。
吉米2013年

另请参见如何让MySQL授权root用户?在超级用户上。它试图避免重设密码。
jww '16

-p和密码之间只有一个空格。我知道它很傻,但可能会帮助某人。
Vinay Wadhwa

Answers:


132

请按照以下步骤操作。

  1. 使用--skip-grant-tables选项(安全性设置)启动MySQL服务器实例或守护程序。

    $ mysqld --skip-grant-tables
    
  2. 执行这些语句。

    $ mysql -u root mysql
    $mysql> UPDATE user SET Password=PASSWORD('my_password') where USER='root';
    $mysql> FLUSH PRIVILEGES;
    

如果您遇到上述未知字段“密码错误”,请使用:

update user set authentication_string=password('my_password') where user='root';
  1. 最后,在没有--skip-grant-tables选项的情况下重新启动实例/守护程序。

    $ /etc/init.d/mysql restart
    

现在,您应该可以使用新密码进行连接了。

$ mysql -u root -p

输入密码: my_password

修复了MySQL“无法锁定ibdata1”错误

sudo mv /usr/local/mysql/data/ibdata1 /usr/local/mysql/data/ibdata1.bak
sudo mv /usr/local/mysql/data/ib_logfile0 /usr/local/mysql/data/ib_logfile0.bak
sudo mv /usr/local/mysql/data/ib_logfile1 /usr/local/mysql/data/ib_logfile1.bak
sudo cp -a /usr/local/mysql/data/ibdata1.bak /usr/local/mysql/data/ibdata1
sudo cp -a /usr/local/mysql/data/ib_logfile0.bak /usr/local/mysql/data/ib_logfile0
sudo cp -a /usr/local/mysql/data/ib_logfile1.bak /usr/local/mysql/data/ib_logfile1
sudo /etc/init.d/mysql restart

1
“ $ mysqld --skip-grant-tables”和“ InnoDB:无法锁定./ibdata1,错误:35 InnoDB:检查您是否还没有另一个mysqld进程InnoDB:使用相同的InnoDB数据或日志文件。”

请参阅上方的我的更新
Yogus

我发现在发出第一个命令之前停止mysql可以停止发生锁定错误-服务mysql停止
Frank Tzanabetis 2015年

7
显然,“密码”列已不复存在...已由“ authentication_string”替换。因此,密码更新字符串现在为:UPDATE用户SET authentication_string = PASSWORD('my_password')其中USER ='root';
jdmcnair 2015年

1
谢谢!现在我很好奇:是什么原因引起的?在Windows上?
Ilia Andrienko '16

64

以上都不对我有帮助。我发现我需要清除插件方法。在5.6中,我可以这样做:

sudo mysql -u root
use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;

在5.7中,我发现需要:

sudo mysql -u root
use mysql;
[mysql] update user set plugin='mysql_native_password' where User='root';
[mysql] flush privileges;

根据文档,在将插件设置为空字符串的情况下,它实际上应该默认为mysql_native_password,但可能会因空密码哈希而感到困惑。有关更多细微差别,您可以在此处阅读文档:https : //dev.mysql.com/doc/refman/5.7/en/native-authentication-plugin.html


5
这为我解决了。当我查看mysql守护程序的状态时,看到以下警告:“ [警告]'用户'条目'root @ localhost'同时指定了密码和身份验证插件。该密码将被忽略。” 将插件设置为“”使我可以再次登录。谢谢@Mario Flores
SpacePope 2016年

我的插件是空白的。有趣的是它自动变为空白。请注意,我说的是我使用dist升级的个人计算机上的安装,因此这可能是root用户中断的原因。
金的朋友,2016年

2
set plugin='mysql_native_password'为我做到了,谢谢!供参考:dev.mysql.com/doc/refman/5.7/en/...
公园。

为什么对于2018年开始安装的软件包仍然存在问题?
mckenzm

1
ubuntu 18,它起作用了
Rahal Kanishka

8

另外,请确保表中的所需记录user具有空plugin字段(例如,可以有"unix_socket")。

从5.5.7版开始,mysql具有各种auth插件支持 https://dev.mysql.com/doc/refman/5.6/en/authentication-plugins.html

因此,如果您有非空plugin字段,则密码将被忽略,并且在mysql错误日志中会出现警告(对我来说是/var/log/mysql/error.log):

[Warning] 'user' entry 'root@localhost' has both a password and an authentication plugin specified. The password will be ignored.


1
对于运行此程序的其他人,在运行mysql服务器5.7.15时,如果您不提供插件,则实际上会将您的用户锁定在外。是一种可能就是你正在寻找pluginmysql_native_password
乔纳森·坎特雷尔16/09/28

8
grep 'temporary password' /var/log/mysqld.log
Sort date (newest date)

您可能会看到类似的内容;

[root@SERVER ~]# grep 'temporary password' /var/log/mysqld.log
2016-01-16T18:07:29.688164Z 1 [Note] A temporary password is generated for root@localhost: O,k5.marHfFu
2016-01-22T13:14:17.974391Z 1 [Note] A temporary password is generated for root@localhost: b5nvIu!jh6ql
2016-01-22T15:35:48.496812Z 1 [Note] A temporary password is generated for root@localhost: (B*=T!uWJ7ws
2016-01-22T15:52:21.088610Z 1 [Note] A temporary password is generated for root@localhost: %tJXK7sytMJV
2016-01-22T16:24:41.384205Z 1 [Note] A temporary password is generated for root@localhost: lslQDvgwr3/S
2016-01-22T22:11:24.772275Z 1 [Note] A temporary password is generated for root@localhost: S4u+J,Rce_0t
[root@SERVER ~]# mysql_secure_installation

保护MySQL服务器部署。

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password:

如果你看到它说

... Failed! Error: Your password does not satisfy the current policy requirements
That means your password needs to have a character such as ! . # - etc...
mix characters well, upper case, lower case, ! . , # etc...

New password: 

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done! 
[root@SERVER ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.10 MySQL Community Server (GPL)

观看此视频的最后10分钟,它教您如何操作。


尽管这可以回答问题,但是您应该对链接的内容进行更详细的描述,并说明它与问题的关系。这将有助于确保在链接页面被删除或脱机的情况下,此答案仍然有用。有关更多信息,请参见此Meta Stack Exchange帖子
bwDraco '16

这是我看到的用例,我正在使用Ansible在AWS的RHEL7上安装mysql,并且在重新配置并重新启动数据库后得到了相同的行为。您是否知道为什么首先发生这种情况,阻止了Ansible访问本地mysqld并对其进行配置。这只是出于测试目的。
einarc '16

显然,这取决于你如何初始化服务器,看看在步骤4的位置:dev.mysql.com/doc/refman/5.7/en/...
einarc

3

试试吧:

mysql --no-defaults --force --user=root --host=localhost --database=mysql 
UPDATE user SET Password=PASSWORD('NEWPASSWORD') where USER='root';
FLUSH PRIVILEGES;

6
您能描述一下这些--no-defaults --force开关的作用吗?否则,它是上述已接受答案的VLQ副本。
2014年

@CanadianLuke没有默认值会跳过任何本应包含的配置文件,即使发出的任何SQL命令失败,也会强制继续操作。如有疑问,请查阅手册
deucalion 2015年

3
需要将其编辑为答案
Canadian Luke

2
我知道了ERROR 1054 (42S22): Unknown column 'Password' in 'field list'
乔纳森


3

就我而言,我的数据库损坏了,在Debian上重启mysql后,root登录名没有密码。解决方案是这样的:

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

其他一些答案也提到了native_password插件,但这是您无需复杂摆弄就可以做到的方式。这就是要更改它的方式。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.