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

Vog*_*ire 147 privileges user-accounts mysql login

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

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| game_data          |
| test               |
+--------------------+
Run Code Online (Sandbox Code Playgroud)

然后我尝试创建一个新用户并注意到 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)
Run Code Online (Sandbox Code Playgroud)

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

+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
Run Code Online (Sandbox Code Playgroud)

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

我现在如何修复 MySQL?

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

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

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

Yog*_*gus 148

请按照以下步骤操作。

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

    $ mysqld --skip-grant-tables
    
    Run Code Online (Sandbox Code Playgroud)
  2. 执行这些语句。

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

如果您遇到上面的未知字段密码错误,请使用:

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

    $ /etc/init.d/mysql restart
    
    Run Code Online (Sandbox Code Playgroud)

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

$ mysql -u root -p
Run Code Online (Sandbox Code Playgroud)

输入密码: 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
Run Code Online (Sandbox Code Playgroud)

  • 显然“密码”列不再存在……它已被“身份验证字符串”取代。所以密码更新字符串现在是: UPDATE user SET authentication_string=PASSWORD('my_password') where USER='root'; (9认同)

小智 90

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

sudo mysql -u root
use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
Run Code Online (Sandbox Code Playgroud)

在 5.7 中,我发现我需要:

sudo mysql -u root
use mysql;
[mysql] update user set plugin='mysql_native_password' where User='root';
[mysql] flush privileges;
Run Code Online (Sandbox Code Playgroud)

根据文档,插件设置为空字符串时,它应该有效地默认为 mysql_native_password,但可能会被空密码哈希混淆。有关更多细微差别,您可以在此处阅读文档:https : //dev.mysql.com/doc/refman/5.7/en/native-authentication-plugin.html

  • 这为我解决了它。当我查看 mysql 守护进程的状态时,我看到了这个警告:“[警告] '用户' 条目 'root@localhost' 同时指定了密码和身份验证插件。密码将被忽略。” 将插件设置为 '' 允许我再次登录。谢谢你,@马里奥弗洛雷斯 (8认同)
  • ubuntu 18 ,它起作用了 (4认同)
  • `set plugin='mysql_native_password'` 为我做了,谢谢!供参考:https://dev.mysql.com/doc/refman/5.7/en/native-authentication-plugin.html (2认同)
  • 为什么这对于进入 2018 年的全新软件包安装仍然是一个问题? (2认同)

小智 9

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

由于版本 5.5.7 mysql 有各种身份验证插件支持 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.

  • 对于遇到此问题的其他人,在运行 mysql server 5.7.15 时,如果您不提供插件,这实际上会将您的用户锁定。可能您正在寻找的是“mysql_native_password”的“插件”。 (2认同)

小智 7

grep 'temporary password' /var/log/mysqld.log
Sort date (newest date)
Run Code Online (Sandbox Code Playgroud)

你可能会看到这样的事情;

[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
Run Code Online (Sandbox Code Playgroud)

保护 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:
Run Code Online (Sandbox Code Playgroud)

如果你看到它说

... 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)
Run Code Online (Sandbox Code Playgroud)

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