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;
}
}
?>
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!
Accepted Answer
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.