Question

How do I connect to a DO managed MySQL DB from a PHP script using mysqli_connect ?

Hi,

I created a managed MySQL cluster, but I’m having problems connecting to it from my PHP script. I can’t seem to find a tutorial online yet.

How do I connect to a DO managed MySQL DB from PHP using mysqli_connect ?

Normally, I connect using syntax like:

$con = mysqli_connect("hostname", "user", "password", "database");
mysqli_set_charset($con, "utf8");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Any help is much appreciated.

Regards,

Wyatt


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello,

I’ve just tested your snippet and there are two things that I’ve noticed:

  • Make sure to append the port number to your host otherwise it would not work as the Managed databases are listening on a non-default MySQL port

  • Make sure to update the Auth type of the Database User and set it to mysql_native_password

To do that you could do is just create a new user with mysql_native_password

CREATE USER 'your_user'@your_server_ip IDENTIFIED WITH mysql_native_password BY 'your_password';

Or you could adjust this for your current user with:

ALTER USER myuser IDENTIFIED WITH mysql_native_password BY 'mypassword';

After that you should be able to connect as normal.

Hope that this helps! Regards, Bobby