“Connect failed: Access denied for user 'root'@'localhost' (using password: YES)” from php function

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

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

The call of the function is the following:

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

Have I to set something in my server? Thanks

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

Answers


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

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

Thanks to all for your time :)


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.

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.


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

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

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

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

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");

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.


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


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"


It is not an error, it is your fault that, you write wrong code in connection file. you simple write as

$con = mysql_connect($host,$user,$password);

if your write worng code for example

$con = mysql_connect($host,$password,$user);

then it show the

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

so keep enjoy....


Need Your Help

Django + Pydev/Eclipse + Google App Engine - possible?

django eclipse google-app-engine linker pydev

Has anyone been able to get Google App Engine/Django working in Pydev/Eclipse? I tried this but had difficulty getting Pydev to recognize all of the externally linked folders (django plugins) that ...

Heroku, apparent silent failure of sucker_punch

heroku delayed-execution

My app runs on Heroku with unicorn and uses sucker_punch to send a small quantity of emails in the background without slowing the web UI. This has been working pretty well for a few weeks.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.