遇到一个错误,指出我的用户不知道该数据库,即使该数据库存在并且拼写正确。我已经授予他们所有关于db的权限,我可以通过服务器中的mysql命令行访问它,也可以通过mysql workbench访问它,但是当尝试使用api中的pdo访问它时。我也无法与用户创建新的架构。
我从mysql.user中检查了User,Host,它显示该用户具有%和localhost。我在mysqld.cnf中注释掉了bind-address并像这样创建了我的用户。
CREATE USER 'user'@'localhost' IDENTIFIED BY 'myPassword';
CREATE USER 'user'@'%' IDENTIFIED BY 'myPassword';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
我的pdo连接
<?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;
}
}
?>