SQLSTATE[HY000] [1698] 用户“root”@“localhost” 的访问被拒绝
我刚刚安装了Ubuntu 16.04(Xenial Xerus)并在其上安装了Web服务器。一切正常,但我无法访问数据库。即使我创建新用户并授予所有权限,我也无法创建数据库
在PHP中,我收到此错误:
SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'
当我尝试在终端中登录时,它可以工作,但在PHP和phpMyAdmin中不起作用。
PHP 代码:
protected $host = '127.0.0.1';
protected $db = 'dbname';
protected $name = 'root';
protected $pass = 'root';
protected $conn;
private static $settings = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
);
public function __construct() {
try {
$this->conn = new PDO("mysql:host=$this->host;dbname=$this->db", $this->name, $this->pass, self::$settings);
} catch (PDOException $e) {
echo $e->getMessage();
}
}