I've a problem that I can't figure it out by myself.

I'm trying to host a website on my VPS server:

-Debian 9


-PHP 7.0.19-1

My website is attempting to connect to my database (which is accessible through phpmyadmin) but with no success.

PHP code:

$cfg['db_server'] = 'ipaddress'; 
$cfg['db_user'] = 'user';
$cfg['db_pass'] = 'password'; 
$cfg['db_name'] = 'newdb';

$conn = @mysqli_connect ($cfg['db_server'], $cfg['db_user'], $cfg['db_pass']);
$select = @mysqli_select_db ($cfg['db_name'], $conn);

if (!$conn) {
    die ('<p class="error">Can't connect to server</p>');
if (!$select) {
    die ('<p class="error">Can't select db.</p>');

On my website I see "Can't connect to server" error.

Could you kindly help me to troubleshoot this problem? I don't have much experience with linux systems, so I don't how to find a solution for this. If you need more information just, I will send it ASAP.

Thank you in advance

  • 1
    $cfg['db_server'] = 'ipaddress'; You are obfuscating things that you shouldn't. What you have here as the ipaddress? – Esa Jokinen Dec 3 '17 at 1:47

Connection refused means that either your MySQL server is not running, or is not bound to the interface where your PHP application is trying to connect to.

You need to change the ipaddress to the address of the interface which MySQL listens to. Most often this is or localhost.

  • thanks all for help! :) changing IP address to localhost did the trick – Rasiel Dec 3 '17 at 17:07

I would suggest reading the documentation and the using this piece of core (lifted (almost) from said documentation which should, tell you what the problem is in more detail

if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
  • I've added your code to my website and I got this error now. Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: Connection refused – Rasiel Dec 2 '17 at 22:24
  • So now you have more to work with - get googling. – user9517 Dec 2 '17 at 22:25
  • Eh.. I have spent 30 mins on googling but still I can't handle this problem. Do you have more ideas how to track down this issue source? – Rasiel Dec 2 '17 at 22:59

if the db server is the same as the webserver then try setting $cfg['db_server'] = 'ipaddress' to '', if this works and using ip does not then could be something related to permissions on the db end or the server-firewall

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.