MySQL:无法访问 root 帐户

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

MySQL: can't access root account

mysqlmysql-error-1045

提问by kenorb

I'm running MySQL 5.x on my local Windows box and, using MySQL administrator, I can't connect to the databases I created using the root account. The error I get is:

我在本地 Windows 机器上运行 MySQL 5.x,并且使用 MySQL 管理员,我无法连接到我使用 root 帐户创建的数据库。我得到的错误是:

MySQL Error number 1045 Access denied for user 'root'@'localhost' (using password: YES)

MySQL 错误号 1045 用户“root”@“localhost”的访问被拒绝(使用密码:是)

I can't recall changing root account credentials, but I know I tried to give other computers on the LAN access to the db by adding their IPs. One thing I did for one of the IPs was to specify access to the account 'root'instead of root, i.e. I surrounded root with single quotation chars. All using MySQL administrator. Could this be the reason why i can't login using root?

我不记得更改了 root 帐户凭据,但我知道我试图通过添加 IP 来让 LAN 上的其他计算机访问数据库。我为其中一个 IP 所做的一件事是指定对帐户的访问'root'而不是root,即我用单引号字符将 root 括起来。全部使用 MySQL 管理员。这可能是我无法使用root登录的原因吗?

Also, is there a way to create a new or reset the root account? As previously mentioned, I have full access to my box.

另外,有没有办法创建新的或重置 root 帐户?如前所述,我可以完全访问我的盒子。

See these questions

看到这些问题

回答by kenorb

You can use the init files. Check the MySQL official documentation on How to Reset the Root Password(including comments for alternative solutions).

您可以使用 init 文件。查看关于如何重置 Root 密码的 MySQL 官方文档(包括对替代解决方案的评论)。

So basically using init files, you can add any SQL queries that you need for fixing your access (such as GRAND, CREATE, FLUSH PRIVILEGES, etc.) into init file (any file).

所以基本上在使用init文件,你可以添加你需要的(如修理你访问的任何SQL查询GRANDCREATEFLUSH PRIVILEGES,等)进入init文件(任何文件)。

Here is my example of recovering root account:

这是我恢复 root 帐户的示例:

echo "CREATE USER 'root'@'localhost' IDENTIFIED BY 'root';" > your_init_file.sql
echo "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;" >> your_init_file.sql 
echo "FLUSH PRIVILEGES;" >> your_init_file.sql

and after you've created your file, you can run:

创建文件后,您可以运行:

killall mysqld
mysqld_safe --init-file=$PWD/your_init_file.sql

then to check if this worked, press Ctrl+Zand type: bgto run the process from the foreground into the background, then verify your access by:

然后检查这是否有效,按Ctrl+Z并输入:bg从前台运行进程到后台,然后通过以下方式验证您的访问:

mysql -u root -proot
mysql> show grants;
+-------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                   |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |

See also:

也可以看看:

回答by SimonVonDerGoltz

This worked for me:

这对我有用:

https://blog.dotkam.com/2007/04/10/mysql-reset-lost-root-password/

https://blog.dotkam.com/2007/04/10/mysql-reset-lost-root-password/

Step 1: Stop MySQL daemon if it is currently running

步骤 1:如果 MySQL 守护进程正在运行,则停止它

  ps -ef | grep mysql      - checks if mysql/mysqld is one of the running processes.

  pkill mysqld             - kills the daemon, if it is running.

Step 2: Run MySQL safe daemon with skipping grant tables

第 2 步:运行 MySQL 安全守护进程并跳过授权表

  mysqld_safe --skip-grant-tables &

  mysql -u root mysql

Step 3: Login to MySQL as root with no password

第 3 步:以 root 身份登录 MySQL,无需密码

  mysql -u root mysql

Step 4: Run UPDATE query to reset the root password

第 4 步:运行 UPDATE 查询以重置 root 密码

  UPDATE user SET password=PASSWORD("value=42") WHERE user="root";
  FLUSH PRIVILEGES;

In MySQL 5.7, the 'password' field was removed, now the field name is 'authentication_string':

在 MySQL 5.7 中,'password' 字段被删除,现在字段名称是 'authentication_string':

  UPDATE user SET authentication_string=PASSWORD("42") WHERE 
  user="root";
  FLUSH PRIVILEGES;

Step 5: Stop MySQL safe daemon

第 5 步:停止 MySQL 安全守护进程

Step 6: Start MySQL daemon

第 6 步:启动 MySQL 守护进程

回答by Dave Webb

There is a section in the MySQL manual on how to reset the root passwordwhich will solve your problem.

MySQL 手册中有一节介绍了如何重置 root 密码,这将解决您的问题。

回答by Shumin Guo

I got the same problem when accessing mysql with root. The problem I found is that some database files does not have permission by the mysql user, which is the user that started the mysql server daemon.

使用 root 访问 mysql 时我遇到了同样的问题。我发现的问题是有些数据库文件没有mysql用户的权限,也就是启动mysql服务器守护进程的用户。

We can check this with ls -l /var/lib/mysqlcommand, if the mysql user does not have permission of reading or writing on some files or directories, that might cause problem. We can change the owner or mode of those files or directories with chown/chmodcommands.

我们可以用ls -l /var/lib/mysql命令来检查,如果mysql用户没有对某些文件或目录的读写权限,可能会导致问题。我们可以使用chown/chmod命令更改这些文件或目录的所有者或模式。

After these changes, restart the mysqld daemon and login with root with command:

进行这些更改后,重新启动 mysqld 守护进程并使用 root 用户登录并使用以下命令:

mysql -u root

Then change passwords or create other users for logging into mysql.

然后更改密码或创建其他用户登录mysql。

HTH

HTH