0

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

-MariaDB

-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:

<?php
$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

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

3 Answers3

1

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;
exit;
}
user9517
  • 114,104
  • 20
  • 206
  • 289
  • 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 02 '17 at 22:24
  • So now you have more to work with - get googling. – user9517 Dec 02 '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 02 '17 at 22:59
  • https://serverfault.com/questions/725262/what-causes-the-connection-refused-message – user9517 Dec 03 '17 at 06:34
1

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 127.0.0.1 or localhost.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
0

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

tristan625
  • 43
  • 2