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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
@paulie5457
I would speculate that the issue you had was based on the transport method being used.
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:If the database is not there you could create it with:
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