How to connect to Managed MySQL Database?

Posted March 18, 2020 2.8k views
DigitalOcean Managed MySQL Database


How can I connect to DigitalOcean’s managed databases from PHP 5.6? I’m running a vps on CWP and I can’t seem to get it right.

$info = [
  "address" => "", 
  "user" => "db_user", 
  "pass" => "db_pass", 
  "db" => "db_name", 

$link = mysqli_init();
if (!$link) {
    die('mysqli_init failed');

if (!mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
    die('Setting MYSQLI_INIT_COMMAND failed');

if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
    die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');

if (!mysqli_real_connect($link, $info["address"], $info["user"], $info["pass"], $info["db"], '25060')) {
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());

echo 'Success... ' . mysqli_get_host_info($link) . "\n";


$info["user"]'s host is set to my VPS's ip address.
$info["user"]'s is granted to access $info["db"].
$info["pass"] was created with IDENTIFIED WITH mysql_native_password.

But I'm keep getting error "Connect Error (2002) Connection refused."

I'm running PHP 5.6 on CWP.
edited by bobbyiliev

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Hi @krismantotan,

There is a very good YouTube video made from one of the community managers which you can watch and help you with your issues -

Please watch it and see if it would help you out in your problems.


  • Hey, thanks for the response.
    I’ve mentioned on my post that I’ve created the database user using mysqli_native_password.
    It’s still haven’t work.

    I also did further test on different server running a PHP 7.4 to connect on the same database using mysqli, but on this server the database connects.
    php 7.4 result:

    I’m thinking maybe the problem is because of the PHP version I’m running?

    edited by MattIPv4

Hi KDSys,
I just upgrade my PHP to PHP 7.3
but error message still : Connection refused

Maybe port 25060 should be opened in hosting/client.