简体   繁体   English

用户 'root'@'localhost' 访问被拒绝(使用密码:YES)(Mysql::Error)

[英]Access denied for user 'root'@'localhost' (using password: YES) (Mysql::Error)

I tried to deploy web application on my server and I am getting this mysql database exception我试图在我的服务器上部署 Web 应用程序,但我收到了这个 mysql 数据库异常

Access denied for user 'root'@'localhost' (using password: YES) (Mysql::Error)

I tried to access the database from the command prompt using mysql -u root -p I am able to do all the database operations.我尝试使用mysql -u root -p从命令提示符访问数据库我能够执行所有数据库操作。

what is the error什么是错误

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:771)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3649)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1176)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:200)
    at com.mpigeon.DbConnection.DbConn(DbConnection.java:26)
    at com.mpigeon.CheckLoginHome.doGet(CheckLoginHome.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

You need to grant access to root from localhost.您需要从 localhost 授予对 root 的访问权限。 Check this ubuntu help检查这个ubuntu 帮助

try using root like..尝试使用root类的..

mysql -uroot

then you can check different user and host after you logged in by using然后你可以在登录后检查不同的用户和主机

select user,host,password from mysql.user;

检查您是否在密码中放置了空格。

I faced the same error after upgrading MySQL server from 5.1.73 to 5.5.45 There is another way to fix that error.将 MySQL 服务器从 5.1.73 升级到 5.5.45 后,我遇到了同样的错误还有另一种方法可以修复该错误。

In my case I was able to connect to MySQL using root password but MySQL actively refused to GRANT PRIVILEGES to any user;就我而言,我能够使用 root 密码连接到 MySQL,但 MySQL 主动拒绝向任何用户授予特权;

  1. Connect to MySQL as root以 root 身份连接到 MySQL

     mysql -u root -p

    then enter your MySQL root password;然后输入您的 MySQL 根密码;

  2. Select database;选择数据库;

     use mysql;
  3. Most probably there is only one record for root in mysql.user table allowing to connect only from localhost (that was in my case) but by the default there should be two records for root, one for localhost and another one for 127.0.0.1 ;很可能在mysql.user表中只有一个 root 记录,只允许从localhost连接(在我的情况下),但默认情况下应该有两个 root 记录,一个用于localhost ,另一个用于127.0.0.1

  4. Create additional record for root user with Host='127.0.0.1' if it's not there;如果它不存在,则为Host='127.0.0.1' root 用户创建附加记录;

     SET @s = CONCAT('INSERT INTO mysql.user SELECT ', REPLACE((SELECT GROUP_CONCAT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'user' AND TABLE_SCHEMA = 'mysql') ,"Host","'127.0.0.1'"), ' FROM mysql.user WHERE User="root"'); PREPARE stmt FROM @s; EXECUTE stmt;
  5. Additionally to that you can execute mysql_upgrade -u -p to see if everything is ok.除此之外,您可以执行mysql_upgrade -u -p以查看是否一切正常。

From my answer here , thought this might be useful:从我这里的回答来看,认为这可能有用:

I tried many steps to get this issue corrected.我尝试了很多步骤来纠正这个问题。 There are so many sources for possible solutions to this issue that is is hard to filter out the sense from the nonsense.这个问题的可能解决方案有很多来源,很难从废话中过滤掉意义。 I finally found a good solution here :我终于在这里找到了一个很好的解决方案:

Step 1: Identify the Database Version步骤 1:确定数据库版本

$ mysql --version

You'll see some output like this with MySQL:你会在 MySQL 中看到一些这样的输出:

$ mysql  Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using  EditLine wrapper

Or output like this for MariaDB:或者像这样为 MariaDB 输出:

mysql  Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1

Make note of which database and which version you're running, as you'll use them later.记下您正在运行的数据库和版本,因为稍后您将使用它们。 Next, you need to stop the database so you can access it manually.接下来,您需要停止数据库,以便您可以手动访问它。

Step 2: Stopping the Database Server步骤 2:停止数据库服务器

To change the root password, you have to shut down the database server beforehand.要更改 root 密码,您必须事先关闭数据库服务器。

You can do that for MySQL with:您可以使用以下命令为 MySQL 执行此操作:

$ sudo systemctl stop mysql

And for MariaDB with:而对于 MariaDB 来说:

$ sudo systemctl stop mariadb

Step 3: Restarting the Database Server Without Permission Checking第 3 步:在没有权限检查的情况下重启数据库服务器

If you run MySQL and MariaDB without loading information about user privileges, it will allow you to access the database command line with root privileges without providing a password.如果您在不加载有关用户权限的信息的情况下运行 MySQL 和 MariaDB,它将允许您以 root 权限访问数据库命令行,而无需提供密码。 This will allow you to gain access to the database without knowing it.这将允许您在不知情的情况下访问数据库。

To do this, you need to stop the database from loading the grant tables, which store user privilege information.为此,您需要停止数据库加载存储用户权限信息的授权表。 Because this is a bit of a security risk, you should also skip networking as well to prevent other clients from connecting.因为这有点安全风险,您还应该跳过网络以防止其他客户端连接。

Start the database without loading the grant tables or enabling networking:在不加载授权表或启用网络的情况下启动数据库:

$ sudo mysqld_safe --skip-grant-tables --skip-networking &

The ampersand at the end of this command will make this process run in the background so you can continue to use your terminal.此命令末尾的与号将使此进程在后台运行,以便您可以继续使用终端。

Now you can connect to the database as the root user, which should not ask for a password.现在您可以以 root 用户身份连接到数据库,该用户不应要求输入密码。

$ mysql -u root

You'll immediately see a database shell prompt instead.您将立即看到一个数据库 shell 提示。

MySQL Prompt MySQL 提示

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

MariaDB Prompt MariaDB 提示

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Now that you have root access, you can change the root password.现在您拥有 root 访问权限,您可以更改 root 密码。

Step 4: Changing the Root Password步骤 4:更改根密码

mysql> FLUSH PRIVILEGES;

Now we can actually change the root password.现在我们实际上可以更改root密码。

For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer , use the following command:对于MySQL 5.7.6 和更高版本以及MariaDB 10.1.20 和更高版本,请使用以下命令:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older , use:对于MySQL 5.7.5 及更早版本以及MariaDB 10.1.20 及更早版本,请使用:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

Make sure to replace new_password with your new password of choice.确保将new_password替换为您选择的新密码。

Note: If the ALTER USER command doesn't work, it's usually indicative of a bigger problem.注意:如果ALTER USER命令不起作用,通常表明存在更大的问题。 However, you can try UPDATE ... SET to reset the root password instead.但是,您可以尝试UPDATE ... SET来重置 root 密码。

[IMPORTANT] This is the specific line that fixed my particular issue: [重要] 这是解决我的特定问题的特定行:

mysql> UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHERE User = 'root' AND Host = 'localhost';

Remember to reload the grant tables after this.请记住在此之后重新加载授权表。

In either case, you should see confirmation that the command has been successfully executed.无论哪种情况,您都应该看到命令已成功执行的确认信息。

Query OK, 0 rows affected (0.00 sec)

The password has been changed, so you can now stop the manual instance of the database server and restart it as it was before.密码已更改,因此您现在可以停止数据库服务器的手动实例并像以前一样重新启动它。

Step 5: Restart the Database Server Normally第五步:正常重启数据库服务器

The tutorial goes into some further steps to restart the database, but the only piece I used was this:本教程进一步介绍了重新启动数据库的一些步骤,但我使用的唯一部分是:

For MySQL, use: $ sudo systemctl start mysql对于 MySQL,使用: $ sudo systemctl start mysql

For MariaDB, use:对于 MariaDB,请使用:

$ sudo systemctl start mariadb

Now you can confirm that the new password has been applied correctly by running:现在您可以通过运行以下命令确认新密码已正确应用:

$ mysql -u root -p

The command should now prompt for the newly assigned password.该命令现在应该提示输入新分配的密码。 Enter it, and you should gain access to the database prompt as expected.输入它,您应该可以按预期访问数据库提示。

Conclusion结论

You now have administrative access to the MySQL or MariaDB server restored.您现在已恢复对 MySQL 或 MariaDB 服务器的管理访问权限。 Make sure the new root password you choose is strong and secure and keep it in safe place.确保您选择的新 root 密码强大且安全,并将其保存在安全的地方。

This error happens if you did not set the password on install, in this case the mysql using unix-socket plugin.如果您没有在安装时设置密码,则会发生此错误,在这种情况下,mysql 使用 unix-socket 插件。

But if delete the plugin link from settings (table mysql.user) will other problem.但是如果从设置(表mysql.user)中删除插件链接会出现其他问题。 This does not fix the problem and creates another problem.这并不能解决问题,而是会产生另一个问题。 To fix the deleted link and set password ("PWD") do:要修复已删除的链接并设置密码(“PWD”),请执行以下操作:

1) Run with --skip-grant-tables as said above. 1) 如上所述使用 --skip-grant-tables 运行。

If it doesnt works then add the string skip-grant-tables in section [mysqld] of /etc/mysql/mysql.conf.d/mysqld.cnf.如果它不起作用,则在 /etc/mysql/mysql.conf.d/mysqld.cnf 的 [mysqld] 部分中添加字符串 skip-grant-tables。 Then do sudo service mysql restart.然后做 sudo service mysql restart。

2) Run mysql -u root -p, then (change "PWD"): 2)运行mysql -u root -p,然后(更改“PWD”):

update mysql.user 
    set authentication_string=PASSWORD("PWD"), plugin="mysql_native_password" 
    where User='root' and Host='localhost';    
flush privileges;

quit
then sudo service mysql restart. Check: mysql -u root -p.

Before restart remove that string from file mysqld.cnf, if you set it there.在重新启动之前从文件 mysqld.cnf 中删除该字符串,如果你在那里设置了它。

@bl79 is the author of this answer, i've just reposted it, because it does help! @bl79 是这个答案的作者,我刚刚重新发布了它,因为它确实有帮助!

My application is using Mura CMS and I faced this issue.我的应用程序正在使用 Mura CMS,我遇到了这个问题。 However the solution was the password mismatch between my mysql local server and the password in the config files.然而,解决方案是我的 mysql 本地服务器和配置文件中的密码之间的密码不匹配。 As soon as I synched them it worked.一旦我同步它们,它就起作用了。

I solved this problem by deleting the empty users creating by MySQL.我通过删除 MySQL 创建的空用户解决了这个问题。 I only have root user and my own user.我只有 root 用户和我自己的用户。 I deleted the rest.我删除了其余的。

Update user table in mysql DB.更新mysql数据库中的用户表。 And set some password where it is blank, i was using root user so i set password for root user.并在空白处设置一些密码,我使用的是 root 用户,所以我为 root 用户设置了密码。

update mysql.user set password = PASSWORD('123456') where password = '';
flush privileges;

And then again tried from ATG CIM by providing password and it worked fine.然后通过提供密码再次从 ATG CIM 尝试,它工作正常。

更新后

http://i.stack.imgur.com/3Lchp.png http://i.stack.imgur.com/3Lchp.png

I got this problem today while installing SugarCRM (a free CRM).我今天在安装 SugarCRM(一个免费的 CRM)时遇到了这个问题。

The system was not able to connect to the database using the root user.系统无法使用 root 用户连接到数据库。 I could definitively log in as root from the console... so what was the problem?我可以明确地从控制台以 root 身份登录......那么问题是什么?

I found out that in my situation, I was getting exactly the same error, but that was because the password was sent to mysql directly from the $_POST data, in other words, the < character from my password was sent to mysql as &lt;我发现在我的情况下,我得到了完全相同的错误,但那是因为密码是直接从$_POST数据发送到 mysql 的,换句话说,我密码中的<字符被发送到 mysql 作为&lt; which means the password was wrong.这意味着密码错误。

Everything else did not help a bit.其他一切都没有帮助。 The list of users in mysql were correct, including the anonymous user (which appears after the root entries.) mysql 中的用户列表是正确的,包括匿名用户(出现在根条目之后。)

I googled a lot but did not find a definite answer to my problem.我用谷歌搜索了很多,但没有找到我的问题的明确答案。 I used KeyPass to generate a strong password and could use it successfully on mysql workbench to connect but not from the command line.我使用 KeyPass 生成了一个强密码,并且可以在 mysql 工作台上成功使用它进行连接,但不能从命令行使用。 So I changed the psw to an easy one and it worked on the command line.所以我把psw改成了一个简单的,它在命令行上工作。 I have managed to create a strong password that was able to connect from the terminal.我设法创建了一个能够从终端连接的强密码。 So my advise is, try with an easy password first before trying all kind of things.所以我的建议是,在尝试各种事情之前,先尝试使用简单的密码。

I was running UTs and I started receiving error messages.我正在运行 UT,我开始收到错误消息。 I am not sure what was the problem.我不确定是什么问题。 But when I changed my encoding style in INTELLIJ to UTF8 it started working again.但是当我将 INTELLIJ 中的编码样式更改为 UTF8 时,它又开始工作了。

access denied for user 'root'@'localhost' (using password yes) hibernate用户 'root'@'localhost' 访问被拒绝(使用密码是)hibernate

this is my URL db.url=jdbc:mysql://localhost:3306/somedb?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf8这是我的 URL db.url=jdbc:mysql://localhost:3306/somedb?useUnicode=true&connectionCollat​​ion=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf8

Add a user option in msyql.在 msyql 中添加用户选项。

GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION;

and this link will be useful.这个链接会很有用。

Update the empty password in the table mysql.user of mysql更新mysql的表mysql.user中的空密码

use mysql;
select host,user,password from mysql.user;
update mysql.user set password = PASSWORD('123456') where password = '';
flush privileges;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NodeJS/mySQL - ER_ACCESS_DENIED_ERROR 用户 'root'@'localhost' 的访问被拒绝(使用密码:是) - NodeJS/mySQL - ER_ACCESS_DENIED_ERROR Access denied for user 'root'@'localhost' (using password: YES) MySQL:用户“root”@“localhost”的访问被拒绝(使用密码:YES) - MySQL: Access denied for user 'root'@'localhost' (using password: YES) Windows 上的 MySQL:用户 'root'@'localhost' 的访问被拒绝(使用密码:YES) - MySQL on Windows: Access Denied for User 'root'@'localhost' (using password: YES) xampp mysql使用密码对用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝 - xampp mysql Access denied for user 'root'@'localhost' using password: YES 错误:“对用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:是)&#39; - error: 'Access denied for user 'root'@'localhost' (using password: YES)' 错误:用户 'root'@'localhost' 的访问被拒绝(使用密码:是) - Error: Access denied for user 'root'@'localhost' (using password: YES) [root @ localhost〜]#mysql -u root -p输入密码:错误1045(28000):用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:是) - [root@localhost ~]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 无法访问 MySQL:错误 1045 (28000):用户 'root'@'localhost' 的访问被拒绝(使用密码:是) - Cannot access MySQL : ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) Mysql 错误:ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES) - Mysql error: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) rails db:migrate rails已中止! Mysql2 :: Error:用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:是) - rails db:migrate rails aborted! Mysql2::Error: Access denied for user 'root'@'localhost' (using password: YES)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM