2013-06-03 07:32:18 +0000 2013-06-03 07:32:18 +0000
130
130

mysql如何修复用户'root'@'localhost'

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

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

然后我试图创建一个新的用户,发现PRIVILEGES有问题。

我现在如何修复MySQL?

我找不到数据库'mysql',无法创建数据库,无法创建用户,任何尝试做的事情都会得到一个错误。用户'root’@‘localhost’(使用密码:YES)被拒绝访问。

我应该用MacPorts重新安装MySQL吗?如果重新安装,我将失去数据库$ mysql -u root -p,对吗?

答案 (6)

138
138
138
2013-06-03 07:35:27 +0000

1.按照以下步骤操作。用--skip-grant-tables选项(安全设置)启动MySQL服务器实例或守护进程。执行这些语句。

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

update user set authentication_string=password('my_password') where user='root';
  1. 最后,在没有--skip-grant-tables选项的情况下重启实例/daemon。
$ mysql -u root -p

输入密码:my_password

修复MySQL “Unable to lock 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
74
74
74
2016-09-28 15:01:12 +0000

以上这些对我来说都没有帮助。我发现我需要清除插件方法。在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,但可能会被空的password hash混淆。更多的细微差别,你可以阅读这里的文档。 https://dev.mysql.com/doc/refman/5.7/en/native-authentication-plugin.html

8
8
8
2016-04-28 21:27:58 +0000

另外,要确保在表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.

8
8
8
2016-01-22 23:08:02 +0000
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
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)

&001

如果你看到它写着

&001

&001

(https://www.youtube.com/watch?v=L2m3BM28Mz0),看最后10分钟的[这个视频]&003,它教你如何做。

3
3
3
2017-11-10 20:13:46 +0000

在我的案例中,我遇到了一个数据库损坏的问题,在Debian上重启mysql后,根登录没有密码。解决方案是这样的:

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

有些其他的回答也提到了原生的/password插件,但这是你可以做到的,不需要复杂的修改。这就是它的意义所在。

3
3
3
2014-02-21 17:05:47 +0000

试试:

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