Question

PDO Unknown database

Posted November 24, 2019 662 views
MySQL

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;
    }
}
?>

1 comment

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.

×
2 answers

@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

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

  • When signed in as the user I see the database in show databases. When signed in as root it also appears. And I was getting this error SQLSTATE[HY000] [1049] Unknown database but now everything appears to be working. I am not sure what happened, I let it sit for a few hours so maybe it was a time sensitive thing but I don’t believe that could be the cause since I’ve set up apis in the past using the same methods and didn’t run into this issue.

Submit an Answer