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

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
  • 117,122
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
  • 38,887
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