“连接失败:用户根”@“localhost”(使用密码:YES)的访问被拒绝。

时间:2022-01-20 19:17:12

I wrote some function used by a php webpage, in order to interact with a mysql database. When I test them on my server I get this error:

我编写了一个php页面使用的函数,以便与mysql数据库交互。当我在我的服务器上测试它们时,我得到了这个错误:

"Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" 

I am able to use them on my pc (using XAMPP) and I can navigate through the tables of the database using the command line in the server. However, the webpage fails to connect. I've checked the password but with no results. It's correct (otherwise I could not log in to mysql from the command line).

我可以在我的pc上使用它们(使用XAMPP),并且可以使用服务器中的命令行在数据库的表中导航。然而,网页连接失败。我检查了密码,但是没有结果。它是正确的(否则我无法从命令行登录到mysql)。

The call of the function is the following:

函数的调用如下:

$conn = new mysqli("localhost", "root", "password", "shop");

Do I have to set something in my server? Thanks

我需要在我的服务器上设置一些东西吗?谢谢

Edit: PHP version 5.3.3-7+squeeze1 mysql version: 5.1.49-3 both on debian

编辑:PHP版本5.3.3-7+挤压1 mysql版本:5.1.4 -3都在debian上

9 个解决方案

#1


62  

I solved in this way: I logged in with root username

我用这种方式解决:我用根用户名登录

mysql -u root -p -h localhost

I created a new user with

我创建了一个新用户

CREATE USER 'francesco'@'localhost' IDENTIFIED BY 'some_pass';

then I created the database

然后我创建了数据库

CREATE DATABASE shop;

I granted privileges for new user for this database

我为这个数据库授予新用户特权

GRANT ALL PRIVILEGES ON shop.* TO 'francesco'@'localhost';

Then I logged out root and logged in new user

然后我注销了根并登录到新用户。

quit;
mysql -u francesco -p -h localhost

I rebuilt my database using a script

我使用脚本重新构建了数据库

source shop.sql;

And that's it.. Now from php works without problems with the call

就是这样. .现在,php的工作没有调用问题

 $conn = new mysqli("localhost", "francesco", "some_pass", "shop");

Thanks to all for your time :)

感谢大家:

#2


10  

Is there a user account entry in the DB for root@localhost? In MySQL you can set different user account permissions by host. There could be several different accounts with the same name combined with the host they are connecting from. The most common are root@127.0.0.1 and root@localhost. These can have different passwords and permissions. Make sure root@localhost exist and has the settings you expect.

DB中是否有root@localhost的用户帐户条目?在MySQL中,可以根据主机设置不同的用户帐户权限。可以有几个不同的帐户,名称相同,并与它们连接的主机结合在一起。最常见的是root@127.0.0.1和root@localhost。它们可以有不同的密码和权限。确保root@localhost存在,并具有预期的设置。

I am willing to bet, based on your explanation, that this is the problem. Connecting from another PC uses a different account than root@localhost and the command line I think connects using root@127.0.0.1.

根据你的解释,我敢打赌这就是问题所在。从另一台PC连接使用与root@localhost不同的帐户,我认为使用root@127.0.0.1连接命令行。

#3


6  

From what you've said so far, it sounds like a problem where the MySQL driver in PHP5.3 has trouble connecting to the older MySQL version 4.1. Have a look on http://www.bitshop.com/Blogs/tabid/95/EntryId/67/PHP-mysqlnd-cannot-connect-to-MySQL-4-1-using-old-authentication.aspx

到目前为止,这听起来像是一个问题,PHP5.3中的MySQL驱动程序很难连接到较老的MySQL版本4.1。看看http://www.bitshop.com/blogs/tabid/95/entryid/67/php -mysqlnd-cannot- connectto mysql - 4-1-1 - use -old- authentic.aspx

There's a similar question here with some useful answers, Cannot connect to MySQL 4.1+ using old authentication

这里有一个类似的问题,有一些有用的答案,不能使用旧的身份验证连接到MySQL 4.1+

SELECT `User`, `Host`, Length(`Password`) FROM mysql.user

This will return 16 for accounts with old passwords and 41 for accounts with new passwords (and 0 for accounts with no password at all, you might want to take care of those as well). Either use the user managements tools of the MySQL front end (if there are any) or

这将返回16个有旧密码的帐户,41个有新密码的帐户(0个没有密码的帐户,您可能也想要处理这些帐户)。或者使用MySQL前端的用户管理工具(如果有的话)或者

SET PASSWORD FOR 'User'@'Host'=PASSWORD('yourpassword');
FLUSH Privileges

#4


3  

Here maybe?

I believe that the code should be:

我认为守则应是:


$connect = new mysqli("host", "root", "", "dbname");

because root does not have a password. the (using password: YES) is saying "you're using a password with this user"

因为root没有密码。(使用密码:YES)的意思是“你在用这个用户的密码”

#5


3  

I had this problem too. But this was because of another reason. My password began with character $... e.g. $MyPassword I've changed it to #MyPassword and the problem is solved.

我也有这个问题。但这是因为另一个原因。我的密码以$开头……$ password myi已更改为#MyPassword,问题解决。

#6


0  

Try initializing your variables and use them in your connection object:

尝试初始化您的变量,并在您的连接对象中使用它们:

$username ="root";
$password = "password";
$host = "localhost";
$table = "shop";
$conn = new mysqli("$host", "$username", "$password", "$table");

#7


0  

Edit your privileges on PHPmyAdmin (WAMP), Note that your password and user name has not been created. So do create or edit it, that it might work with your sql connection in your php. Hope it works

编辑您在PHPmyAdmin (WAMP)上的权限,注意您的密码和用户名尚未创建。因此,创建或编辑它,它可能与您的php中的sql连接一起工作。希望它的工作原理

#8


0  

TIP:

comment out pid and socket file, if you can't connect to server.. mysql could be connecting with incorrect pid and/or socket file, so when you try to connect to server trough command-line it "looks" at the incorrect pid/socket file...

提示:如果不能连接到服务器,请注释掉pid和套接字文件。mysql可能正在连接不正确的pid和/或套接字文件,所以当您尝试通过命令行连接服务器时,它会“查看”不正确的pid/套接字文件……

### comment out pid and socket file, if you can't connect to server..
#pid-file=/var/run/mysql/mysqld.pid #socket=/var/lib/mysql/mysql.sock

#9


0  

Most likely it is not identifying your user properly. root@localhost. Select the Mysql version your MAMP is using.

很可能它没有正确地标识用户。root@localhost。选择MAMP使用的Mysql版本。

/Applications/MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql

/ application /MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql

I actually wrote an alias on my computer so I don't have to remember how to get to the bin directory.

实际上,我在我的电脑上写了一个别名,所以我不需要记住如何到达bin目录。

alias mysqldumpMAMP='/Applications/MAMP/Library/bin/mysqldump'

别名mysqldumpMAMP = ' /应用程序/ MAMP /图书馆/ bin /,mysqldump '

Which allows me to run this:

这让我可以运行这个:

mysqldumpMAMP -uroot -p db_name > test_db_dump.sql

mysqldumpMAMP -uroot -p db_name > test_db_dump.sql

You can save an alias in your bash profile ~/.bash_profile or ~/.bash_rc

可以在bash配置文件中保存别名~/。bash_profile或~ / .bash_rc

#1


62  

I solved in this way: I logged in with root username

我用这种方式解决:我用根用户名登录

mysql -u root -p -h localhost

I created a new user with

我创建了一个新用户

CREATE USER 'francesco'@'localhost' IDENTIFIED BY 'some_pass';

then I created the database

然后我创建了数据库

CREATE DATABASE shop;

I granted privileges for new user for this database

我为这个数据库授予新用户特权

GRANT ALL PRIVILEGES ON shop.* TO 'francesco'@'localhost';

Then I logged out root and logged in new user

然后我注销了根并登录到新用户。

quit;
mysql -u francesco -p -h localhost

I rebuilt my database using a script

我使用脚本重新构建了数据库

source shop.sql;

And that's it.. Now from php works without problems with the call

就是这样. .现在,php的工作没有调用问题

 $conn = new mysqli("localhost", "francesco", "some_pass", "shop");

Thanks to all for your time :)

感谢大家:

#2


10  

Is there a user account entry in the DB for root@localhost? In MySQL you can set different user account permissions by host. There could be several different accounts with the same name combined with the host they are connecting from. The most common are root@127.0.0.1 and root@localhost. These can have different passwords and permissions. Make sure root@localhost exist and has the settings you expect.

DB中是否有root@localhost的用户帐户条目?在MySQL中,可以根据主机设置不同的用户帐户权限。可以有几个不同的帐户,名称相同,并与它们连接的主机结合在一起。最常见的是root@127.0.0.1和root@localhost。它们可以有不同的密码和权限。确保root@localhost存在,并具有预期的设置。

I am willing to bet, based on your explanation, that this is the problem. Connecting from another PC uses a different account than root@localhost and the command line I think connects using root@127.0.0.1.

根据你的解释,我敢打赌这就是问题所在。从另一台PC连接使用与root@localhost不同的帐户,我认为使用root@127.0.0.1连接命令行。

#3


6  

From what you've said so far, it sounds like a problem where the MySQL driver in PHP5.3 has trouble connecting to the older MySQL version 4.1. Have a look on http://www.bitshop.com/Blogs/tabid/95/EntryId/67/PHP-mysqlnd-cannot-connect-to-MySQL-4-1-using-old-authentication.aspx

到目前为止,这听起来像是一个问题,PHP5.3中的MySQL驱动程序很难连接到较老的MySQL版本4.1。看看http://www.bitshop.com/blogs/tabid/95/entryid/67/php -mysqlnd-cannot- connectto mysql - 4-1-1 - use -old- authentic.aspx

There's a similar question here with some useful answers, Cannot connect to MySQL 4.1+ using old authentication

这里有一个类似的问题,有一些有用的答案,不能使用旧的身份验证连接到MySQL 4.1+

SELECT `User`, `Host`, Length(`Password`) FROM mysql.user

This will return 16 for accounts with old passwords and 41 for accounts with new passwords (and 0 for accounts with no password at all, you might want to take care of those as well). Either use the user managements tools of the MySQL front end (if there are any) or

这将返回16个有旧密码的帐户,41个有新密码的帐户(0个没有密码的帐户,您可能也想要处理这些帐户)。或者使用MySQL前端的用户管理工具(如果有的话)或者

SET PASSWORD FOR 'User'@'Host'=PASSWORD('yourpassword');
FLUSH Privileges

#4


3  

Here maybe?

I believe that the code should be:

我认为守则应是:


$connect = new mysqli("host", "root", "", "dbname");

because root does not have a password. the (using password: YES) is saying "you're using a password with this user"

因为root没有密码。(使用密码:YES)的意思是“你在用这个用户的密码”

#5


3  

I had this problem too. But this was because of another reason. My password began with character $... e.g. $MyPassword I've changed it to #MyPassword and the problem is solved.

我也有这个问题。但这是因为另一个原因。我的密码以$开头……$ password myi已更改为#MyPassword,问题解决。

#6


0  

Try initializing your variables and use them in your connection object:

尝试初始化您的变量,并在您的连接对象中使用它们:

$username ="root";
$password = "password";
$host = "localhost";
$table = "shop";
$conn = new mysqli("$host", "$username", "$password", "$table");

#7


0  

Edit your privileges on PHPmyAdmin (WAMP), Note that your password and user name has not been created. So do create or edit it, that it might work with your sql connection in your php. Hope it works

编辑您在PHPmyAdmin (WAMP)上的权限,注意您的密码和用户名尚未创建。因此,创建或编辑它,它可能与您的php中的sql连接一起工作。希望它的工作原理

#8


0  

TIP:

comment out pid and socket file, if you can't connect to server.. mysql could be connecting with incorrect pid and/or socket file, so when you try to connect to server trough command-line it "looks" at the incorrect pid/socket file...

提示:如果不能连接到服务器,请注释掉pid和套接字文件。mysql可能正在连接不正确的pid和/或套接字文件,所以当您尝试通过命令行连接服务器时,它会“查看”不正确的pid/套接字文件……

### comment out pid and socket file, if you can't connect to server..
#pid-file=/var/run/mysql/mysqld.pid #socket=/var/lib/mysql/mysql.sock

#9


0  

Most likely it is not identifying your user properly. root@localhost. Select the Mysql version your MAMP is using.

很可能它没有正确地标识用户。root@localhost。选择MAMP使用的Mysql版本。

/Applications/MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql

/ application /MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql

I actually wrote an alias on my computer so I don't have to remember how to get to the bin directory.

实际上,我在我的电脑上写了一个别名,所以我不需要记住如何到达bin目录。

alias mysqldumpMAMP='/Applications/MAMP/Library/bin/mysqldump'

别名mysqldumpMAMP = ' /应用程序/ MAMP /图书馆/ bin /,mysqldump '

Which allows me to run this:

这让我可以运行这个:

mysqldumpMAMP -uroot -p db_name > test_db_dump.sql

mysqldumpMAMP -uroot -p db_name > test_db_dump.sql

You can save an alias in your bash profile ~/.bash_profile or ~/.bash_rc

可以在bash配置文件中保存别名~/。bash_profile或~ / .bash_rc