Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to mysql server without sudo

Tags:

mysql

sudo

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

like image 713
Riyafa Abdul Hameed Avatar asked May 15 '16 15:05

Riyafa Abdul Hameed


People also ask

Does MySQL need root?

Normally while installing MySQL/MariaDB database server on Linux, it's recommended to set a MySQL root user password to secure it, and this password is required to access the database server with root user privileges.


2 Answers

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 
like image 144
Riyafa Abdul Hameed Avatar answered Sep 28 '22 06:09

Riyafa Abdul Hameed


You need to change algorithm. Following work for me,

mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ''; mysql > FLUSH PRIVILEGES; 
like image 25
Hemant Thorat Avatar answered Sep 28 '22 04:09

Hemant Thorat