5
votes

I've tried to search for an existing answer to this problem, but the answers I find have not worked thus far.

I've been attempting to use PHP to connect to a MySql database. My web host uses cPanel on Linux. The code I'm using to do this seems standard enough:

$mysqli = new mysqli("localhost", "cPanelUsername_dbUsername", "dbPassword", "cPanelUsername_dbName");

I've been getting the following error:

Failed to connect to MySQL: (1045) Access denied for user 'cPanelUsername_dbUsername'@'localhost' (using password: YES)Access denied for user 'cPanelUsername'@'localhost' (using password: NO) 
  • "localhost" is the host server where the MySql server is located (it seems like this works)
  • "cPanelUsername" is my cpanel username
  • "dbUsername" is the database user, which I added to the database with all permissions granted
  • "dbPassword" is the database password for dbUsername
  • "dbName" is the database name

I ended up adding my cPanel username before the dbName and dbUsername after searching for answers to this issue elsewhere.

It looks like I have everything set up correctly but it's not connecting (with the error above). I don't have any direct control over the server that I wouldn't have to ask my web host about, which may take a few days to get sorted out. Do I have something wrong with my connection code?

4
access denied means that the account you're trying to log into doesn't exist or doesn't have the rights to log in. - Marc B
If your database user is called dbUsername, why are you entering cPanelUsername_dbUsername? Or is that a quirck of your hosting provider? - Wrikken
It seems to be a quirk of cPanel, at least from what I've read. - Baknik

4 Answers

3
votes

First check the database that you gave the proper user access to your database, which is given from Add User to databases from Mysql database section in cpanel.

after that check it again,

first try normal connection code in php,

$con = mysql_connect("localhost","cpanel_username","cpanel_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
1
votes

In cPanel, make sure that:

  1. The database user cPanelUsername_dbName exists, with the password dbPassword
  2. The database you want to use exists.
  3. The user cPanelUsername_dbName is allowed to access the database.
  4. The user cPanelUsername_dbName is allowed to access the database from localhost, 127.0.0.1, and the IP address of your server.

Your MySQL connections may use 127.0.0.1 or the IP address of your server, and MySQL will reject the connection if access isn't granted for the specific IP address used.

0
votes

check the database name spelling at your phpMyAdmin. Usually the name is in format user_dbname.

For example:

cpanel username: jack,  
database created: student

In your php script, the dbname should be jack_student

0
votes

This worked for me:

  • Depending on what MySQL version you have, make sure you have matching password and hostname on your PHP file, config.inc.php file.

If you need to change the password for MySQL 5.7.6 and later:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL 5.7.5 and earlier:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
  • If you are trying to access your localhost server from a different machine i.e. (emulator, another computer), make sure you are using the actual IP address of the the localhost, DO NOT USE localhost as the hostname. Because this is like telling the machine to connect to itself - but the server is on a different IP address.