Connect to mysql server without sudo

Riyafa Abdul Hameed picture Riyafa Abdul Hameed · May 15, 2016 · Viewed 72k times · Source

The command:

mysql -u root -p

gives the error:

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

But running sudo privileges, works:

sudo mysql -u root -p

Is it possible to get rid of the sudo requirement because it prevents me from opening the database in intellij? I tried the following as in the answer to this question Connect to local MySQL server without sudo:

sudo chmod -R 755 /var/lib/mysql/

which did not help. The above question has a different error thrown

Answer

Riyafa Abdul Hameed picture Riyafa Abdul Hameed · May 15, 2016

Only the root user needs sudo requirement to login to mysql. I resolved this by creating a new user and granting access to the required databases:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';

now newuser can login without sudo requirement:

mysql -u newuser -p