Connect using PHP PDO to mysql database failed

Posted June 25, 2014 12.3k views

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.


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
4 answers

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 [...]
  • Many thanks alexandre but I changed this password by using mysql_secure_installation and used it for the PDO connection but I cannot connect to the MySQL Database. I used the root admin to make use of the database with its credentials.

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();

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

Many thanks all your answers.

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