Report this

What is the reason for this report?

Connect using PHP PDO to mysql database failed

Posted on 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.

Thanks



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!

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.

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 [...]

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);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} 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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.