即使我已经实施了搜索量最大的解决方案,该问题仍被标记为重复。我使用以下代码行修改了MySql用户以使用本机密码(这不能解决我的连接问题):
ALTER USER user_insert_ IDENTIFIED WITH mysql_native_password BY 'password123';
此代码是此处引用的解决方案:phpMyAdmin on MySQL 8.0(也将此线程标记为重复的解决方案)
我正在使用MySQL 8.0和php 7.3.0
我已授予'user_insert_'@'127.0.0.1'的完全权限,并且能够从pymysql和MySQL Workbench正常运行以下SELECT语句,但不能通过php中的PDO:
"SELECT * FROM worker_status"
我收到的错误是:
致命错误:未捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1142对用户'user_insert _'@'localhost'的表'worker_status'在C:\ xampp \ htdocs \ hud \ test_api_2 \中的SELECT命令被拒绝objects \ activity.php:33
我希望收到一个单行响应。
这是相关代码:
class Database{
private $host = "127.0.0.1";
private $db_name = "hud";
private $username = "user_insert_";
private $password = "password123";
private $charset = 'utf8mb4';
private $dsn = "";
public $conn;
public function getConnection(){
$this->conn = null;
$this->dsn = "mysql:host=$this->host;dbname=$this->db_name;charset=$this->charset";
print($this->dsn);
$this->options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$this->conn = new PDO($this->dsn, $this->username, $this->password, $this->options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
return $this->conn;
}
public function __construct($db){
$this->conn = $db;
}
function read(){
$query = "SELECT * FROM worker_status";
$stmt = $this->conn->prepare($query);
$stmt->execute();
$result = $stmt->fetch();
return $stmt;
}
此外,当尝试在“”处访问phpmyadmin时,我收到一条red错误消息,指出未对浏览器访问启用caching_sha2_password。
我已检查:
在我看来,这就像一个香草的盒子。为什么我只用php PDO收到1142 SELECT权限错误?