Can't connect to DB server: Access denied for user ''@'localhost' (using password: NO)

Operating System & Version
Centos 7
cPanel & WHM Version
11.92.0

ctrlaltdel21

Member
Jan 6, 2021
23
1
3
Ontario, Canada
cPanel Access Level
Root Administrator
Hi,

We have an inhouse MySQL database that is only accessible inhouse - you cannot access it remotely. There is no user/password required to access this database - you simply go to the IP address in your browser and everything is there.

We would like to move it online so we can access it remotely from anywhere.

I set up a cPanel account and imported this database so it's accessible via a domain name. When I go to the domain name in my browser, here is exactly what shows on my browser screen:

Could not connect to DB server:Access denied for user ''@'localhost' (using password: NO)

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


Here are the first few lines of the config.php file:

<?php
ini_set('display_errors', 'On');
error_reporting (E_ERROR | E_PARSE);
$config["db"] = "cpanelusername_databasename";
$config["user"] = "cpanelusername_databaseusername";
$config["password"] = "databasepassword";
$config["host"] = "localhost";
$config['baseurl'] = "https://ourdomain.com";
$crypt = "companyname";

Here is the code of the connect.php file:

<?php
$db = mysql_connect($config["host"], $config["user"], $config["password"]);
$sqlnames = "SET NAMES utf8;";
mysql_query($sqlnames);
if(!$db) echo "<p>Could not connect to DB server:" . mysql_error() . "</p>\n";
mysql_select_db($config["db"], $db) or die (mysql_error());
$config['site_db']=$db;
?>

Is there anything here that is incorrect? I can't find any other files that would connect the database to the webserver.

Thank you in advance for your assistance.
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,569
2,612
363
cPanel Access Level
Root Administrator
Hey there! I don't claim to be a PHP wizard, but I've looked at many connection strings and that seems fine to me.

I'd start by eliminating that from the equation completely and test the user access on the command line with this:

Code:
mysql -u username -p
That will prompt you for the password for that user you've created. If that works, it's something with the site's code or connection. If that doesn't work, the issue is with the access for the user to the database.

You can make sure the user has full access to the database through the cPanel >> MySQL Database area near the bottom of the page by just trying to re-grant access. It won't hut anything to re-save the values on that page.
 
  • Like
Reactions: ctrlaltdel21

keat63

Well-Known Member
Nov 20, 2014
1,963
267
113
cPanel Access Level
Root Administrator
When you created the database on your cpanel, did you then create a user and assign access rights to that database.
And grant all privileges.

Is the software which access the database hosted on the same server, or is this on a local PC.
I found this.


To connect from a script in your cPanel account to a database also in your cPanel account you need to just
  1. Create Database
  2. Create User for it
  3. Give necessary grants to the User

To connect locally, from your own computer to a cPanel hosted database you need to:
  1. Have a Database and an User assigned to it.
  2. Allow your local computer's IP in cPanel's Remote MySQL tool
The latter part might be tricky, if you have many remote users, or dynamically assigned IP's
 
Last edited:

ffeingol

Well-Known Member
PartnerNOC
Nov 9, 2001
944
423
363
cPanel Access Level
DataCenter Provider
The error messages is "interesting"

''@'localhost' (using password: NO)
Based on that error, it's not getting a username (it's '' which it should be 'something') and it is showing that no password was provided. Are you sure that your config file is getting properly included? Path's on to the included files may be very different than what you had on your local server.
 

kodeslogic

Well-Known Member
PartnerNOC
Apr 26, 2020
576
266
138
IN
cPanel Access Level
Root Administrator
If you're able to login to MySQL from the command prompt by mysql -u username -p, here the username and password must be the same as in your database config file.

Next step you should check that
- in the cPanel interface make sure you have a database and database user and they are correctly mapped with required privileges
- the database config file is correctly included for your website code as suggested by @ffeingol
 
  • Like
Reactions: ctrlaltdel21

ctrlaltdel21

Member
Jan 6, 2021
23
1
3
Ontario, Canada
cPanel Access Level
Root Administrator
The error messages is "interesting"

Based on that error, it's not getting a username (it's '' which it should be 'something') and it is showing that no password was provided. Are you sure that your config file is getting properly included? Path's on to the included files may be very different than what you had on your local server.
I wondered the exact same thing because whenever I found this error online by others having the same issue, they all had a username where I had the " only.

Oddly, after reading @cPRex 's response, I reset my password on the database, the error disappeared and our site displayed. Based on the error I was receiving, it doesn't make sense to me that's all it was because the error didn't show my username and said I wasn't using a password. Very strange.

Thank you for your response.
 

ctrlaltdel21

Member
Jan 6, 2021
23
1
3
Ontario, Canada
cPanel Access Level
Root Administrator
To connect locally, from your own computer to a cPanel hosted database you need to:
  1. Have a Database and an User assigned to it.
  2. Allow your local computer's IP in cPanel's Remote MySQL tool
The latter part might be tricky, if you have many remote users, or dynamically assigned IP's
Thank you for posting this - this is very good to know.
 

kodeslogic

Well-Known Member
PartnerNOC
Apr 26, 2020
576
266
138
IN
cPanel Access Level
Root Administrator
Good to hear that it is resolved. :)