Question

PDO Unknown database

Getting an error stating that the database is unknown for my user even though it exists and is spelled correctly. I have granted them all permissions on the db, I can access it through the mysql command line in my server and I can access it through mysql workbench but when trying to access it with pdo in my api.

I checked the User, Host from mysql.user and it shows that the user has % and localhost. I commented out bind-address in mysqld.cnf and created my user like so.

CREATE USER 'user'@'localhost' IDENTIFIED BY 'myPassword';
CREATE USER 'user'@'%' IDENTIFIED BY 'myPassword';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

My PDO Connection

<?php
class Database{
    private $host = "127.0.0.1";
    private $db_name = "casinoCheckList";
    private $username = "user";
    private $password = "myPassword";
    public $conn;

    public function getConnection(){

        $this->conn = null;
        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->exec("set names utf8");
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>

Show comments

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.

Andrew Moore
DigitalOcean Employee
DigitalOcean Employee badge
November 25, 2019
Accepted Answer

@paulie5457

I would speculate that the issue you had was based on the transport method being used.

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' WITH GRANT OPTION 
GRANT ALL PRIVILEGES ON casinoChecklist.* TO 'user'@'localhost'

Notice your use of ‘localhost’. What we tend to experience is the client connection using the unix socket and avoiding the TCP/IP routing. Your PHP script uses the loopback address and it might have manifested in the error you experienced.

If you add a user where the host is wild carded or explicitly using the lo interface (127.0.0.1) you may not have experienced this issue.

Something to watch if you’re troubleshooting connection challenges in the future.

BR

Bobby Iliev
Site Moderator
Site Moderator badge
November 25, 2019

Hello,

What I could suggest is checking if the casinoCheckList database actually exists:

SHOW DATABASES;

If the database is not there you could create it with:

CREATE DATABASE casinoCheckList;

If this is not the case I could suggest sharing the exact error that you are getting here so I could try to advise you further.

Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel