0

I moved my database to an external server because the one my webhosting provider offered is just too limited (no remote access). So I set a MariaDB up on a Vultr server (CentOS 7). everything works fine in phpmyadmin and I already imported the other db. When trying to connect from my website I get this warning tho:

Warning: mysqli_connect(): (HY000/2002): Connection timed out in blabla/assets/php/config.php on line 8 ERROR: Could not connect. Connection timed out

PHP code

define('DB_SERVER', '11.22.33.44');
define('DB_USERNAME', 'user');
define('DB_PASSWORD', 'pw');
define('DB_NAME', 'data');

$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

It worked perfectly fine with the old db so I assume Im missing someting but I can't figure it out. I tried opening the 3306 port, tried different users, allowing all IPs but nothing works.

000
  • 651
  • 1
  • 5
  • 6
  • Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Jul 24 '20 at 22:15
  • 1
    Make sure that from the server where you have the php script that should connect to the database 11.22.33.44 os firdt able to reach that server, if so try to remotely connect using the command line "mysql -uUser -pPass -h 11.22.33.44" maybe is just a mariadb misconfiguration – d3javu999 Jul 24 '20 at 22:15
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era and should not be used in new code. Additionally the procedural interface has less rigorous error checking and reporting, frustrating debugging efforts. – tadman Jul 24 '20 at 22:17
  • Do try and get out of the habit of cluttering up your code with needless things like `=== false`. Many functions are designed to return values that evaluate as logically true or false so that's redundant and in some cases can cause bugs since you may get a logically true value, but not one that’s literally `true`. – tadman Jul 24 '20 at 22:17
  • Tip: A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so errors resulting from simple mistakes made aren’t easily ignored. Without exceptions you must pay close attention to return values, many of these indicate problems you must resolve or report to the user. Exceptions allow for more sophisticated flow control as they can “bubble up” to other parts of your code where it’s more convenient to handle them. – tadman Jul 24 '20 at 22:18
  • "try to remotely connect using the command line" - I just tried it with my discord bot and it works there. – 000 Jul 24 '20 at 22:51

1 Answers1

0

Thanks for everyone who tried to help but sadly the problem is not on my end. Turned out that one.com also does not allow outgoing connections to other databases than their own.

000
  • 651
  • 1
  • 5
  • 6