Connect using PHP PDO to mysql database failed

  • Posted June 25, 2014

I’m using a droplet with Ubuntu 12.04 (32bits) and after installing LAMP into this droplet I cannot access from a php file to my database using the admin user from the site.

I updated my host, username and password to connect but I don’t know If I forgot any command or related tip.

The connection always return not successfull.

I great any notice about this error.



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

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.

These a serie of eight videos deeply explaining how to connect to the database using PDO method. Enjoy watching them.

Many thanks both of you. I didn’t execute the sudo mysql_install_db command into my droplet.

Many thanks all your answers.

How are you trying to connect to the database? Are you catching the error message? That might give you a better idea of what the problem is. Try doing something like:

try {
    $conn = new PDO('mysql:host=localhost;port=1234;dbname=$dbname', $username, $password);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();

When you log in in your server, in the welcome message you can find your mysql password. This password isn’t the same as your ssh user.

Thank you for using DigitalOcean's LAMP Application.
Your MySQL root user's password is password here
You are encouraged to run mysql_secure_installation to ready your server for production [...]