PHP MySql (1045) Access Denied For User

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?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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,, and the IP address of your server.

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

Method 3

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

Method 4

This worked for me:

  • Depending on what MySQL version you have, make sure you have matching password and hostname on your PHP file, 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.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x