“连接失败:拒绝用户'root'@'localhost'的访问(使用密码:是)"从PHP功能

问题描述:

我写了一些php网页使用的函数,以便与mysql数据库进行交互.当我在服务器上测试它们时,出现此错误:

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:

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

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

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).

该函数的调用如下:

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

我是否必须在服务器中进行设置?谢谢

Do I have to set something in my server? Thanks

PHP版本5.3.3-7 + squeeze1 mysql版本:5.1.49-3 两者都在debian上

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

我以这种方式解决了: 我以root用户名登录

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

mysql -u root -p -h localhost

我创建了一个新用户

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

然后我创建了数据库

CREATE DATABASE shop;

我为此数据库的新用户授予了特权

I granted privileges for new user for this database

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

然后我注销root并登录新用户

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;

就是这样..现在,php可以正常工作了,而调用没有问题

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

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

感谢您的宝贵时间:)