Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'

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

In PHP I'm getting this error:

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' 

When I try to login in terminal, it works, but in PHP and phpMyAdmin don't.

PHP Code:

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();     } } 
like image 786
lamka02sk Avatar asked Apr 26 '16 11:04

lamka02sk


1 Answers

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.

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.

like image 50
Andris Avatar answered Sep 21 '22 12:09

Andris