SQLSTATE[HY000] [1698] 用户 'root'@'localhost' 的访问被拒绝 [英] SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'
问题描述
我刚刚安装了 Ubuntu 16.04 (Xenial Xerus)并在其上安装了 Web 服务器.一切正常,但我无法访问数据库.即使我创建新用户并授予所有权限,我也无法创建数据库
I just installed Ubuntu 16.04 (Xenial Xerus) and installed web server on it. Everything works well, but I cannot access database. Even if I create new user and grant all privileges, I can't create database
在 PHP 中,我收到此错误:
In PHP I'm getting this error:
SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'
当我尝试在终端中登录时,它可以工作,但在 PHP 和 phpMyAdmin 中不行.
When I try to login in terminal, it works, but in PHP and phpMyAdmin don't.
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();
}
}
推荐答案
事实证明,在 5.7 中,如果不成为 sudo'er.这意味着您不能再只运行 mysql -u root
而必须改为执行 sudo mysql -u root
.
It turns out you can't use the root
user in 5.7 anymore without becoming a sudo'er. That means you can't just run mysql -u root
anymore and have to do sudo mysql -u root
instead.
这也意味着如果您在 GUI(或任何非命令行应用程序)中使用 root
用户,它将不再起作用.要使其正常工作,您必须创建一个具有所需权限的新用户并改用该用户.
That also means that it will no longer work if you're using the root
user in a GUI (or supposedly any non-command line application). To make it work you'll have to create a new user with the required privileges and use that instead.
有关详细信息,请参阅此答案.
See this answer for more details.
这篇关于SQLSTATE[HY000] [1698] 用户 'root'@'localhost' 的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!