Linux – Can’t connect to MariaDB

debianlinuxmariadbMySQLPHP

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

Best Answer

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.