本文介绍了SQLSTATE [HY000] [1698]用户'root'@'localhost'的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了Ubuntu 16.04,并在其上安装了Web服务器.一切正常,但是我无法访问数据库.即使创建新用户并授予所有特权,我也无法创建数据库在PHP中,我收到此错误:

I just installed Ubuntu 16.04 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 databaseIn 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();
    }
}

推荐答案

原来,您不能在sudoer中使用5.7中的root用户.这意味着您不能再运行mysql -u root而必须执行sudo mysql -u root.

Turns out you can't use the root user in 5.7 anymore without becoming a sudoer. 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'的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:38