Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySql (1045) Access Denied For User

Tags:

php

mysql

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?

like image 332
Baknik Avatar asked Oct 02 '22 17:10

Baknik


1 Answers

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());
}
like image 142
TrojanHorse Avatar answered Oct 09 '22 14:10

TrojanHorse