Connect to mysql server without sudo

MysqlSudo

Mysql Problem Overview


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

Mysql Solutions


Solution 1 - Mysql

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

Solution 2 - Mysql

You need to change algorithm. Following work for me,

mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
mysql > FLUSH PRIVILEGES;

Solution 3 - Mysql

You can use the same ROOT user, or a NEW_USER and remove the SUDO privileges. Below example shows how to remove connect using ROOT, without SUDO.

Connect to MY-SQL using SUDO

sudo mysql -u root

Delete the current Root User from the User Table

DROP USER 'root'@'localhost';

Create a new ROOT user (You can create a different user if needed)

CREATE USER 'root'@'%' IDENTIFIED BY '';

Grant permissions to new User (ROOT)

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

Flush privileges, so that the Grant tables get reloaded immediately. (Why do we need to flush privileges?)

FLUSH PRIVILEGES;

Now it's all good. Just in case, check whether a new root user is created.

SELECT User,Host FROM mysql.user;

+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| root             | %         |
| debian-sys-maint | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

Exit mysql. (Press CTRL + Z). Connect to MySQL without SUDO

mysql -u root

Hope this will help!

Solution 4 - Mysql

first login to your mysql with sudo.

then use this code to change "plugin" coloumn value from "unix_socket" or "auth_socket" to "mysql_native_password" for root user.

UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin IN ('unix_socket', 'auth_socket');

FLUSH PRIVILEGES;

finally restart mysql service. that's it.

if you want more info, check this link

UPDATE:

In new versions of mysql or mariadb you can use :

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('your-password');
FLUSH PRIVILEGES;

Solution 5 - Mysql

I have solved this problem using following commands.

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'username'@'localhost';
FLUSH PRIVILEGES;

Here, username = any user name you like.

and password = any password you like.

Solution 6 - Mysql

You can use the below query:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';

This query is enough.

Solution 7 - Mysql

Login to mysql with sudo: sudo mysql -u root -p

After that Delete current root@localhost account:

~ MariaDB [(none)]> DROP USER 'root'@'localhost';
~ MariaDB [(none)]> CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
~ MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
~ MariaDB [(none)]> FLUSH PRIVILEGES;

Solution 8 - Mysql

This answer needs to be slightly adapted for mariaDB instead of mysql.

First login as root using sudo:

$ sudo mysql -uroot

Then alter the mariadb root user:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('mypassword');
FLUSH PRIVILEGES;

From now on sudo is not longer needed:

$ mysql -uroot -p

Version used: mysql Ver 15.1 Distrib 10.4.13-MariaDB, for osx10.15 (x86_64) using readline 5.1

Solution 9 - Mysql

In the comment of the question you answer you referenced, it reads

> Ok, just try to analyze all of the directories down in the path of the > socket file, they need to have o+rx and the sock file too (it's not a > good idea to make it modifiable by others). > > You can also try to remove mysql.sock and then restart mysqld, the > file should be created by the daemon with proper privileges.

This seemed to work for this question(the one you said you looked at) so it may work for you as well

Solution 10 - Mysql

The error Message:

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

means that the Server not allow the connect for this user and not that mysql cant access the socket.

try this to solve the problem:

Login in your DB

sudo mysql -u root -p

then make these modifications:

MariaDB []>use mysql;
MariaDB [mysql]>update user set plugin=' ' where User='root';
MariaDB [mysql]>flush privileges;
MariaDB [mysql]>exit

try login again without sudo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRiyafa Abdul HameedView Question on Stackoverflow
Solution 1 - MysqlRiyafa Abdul HameedView Answer on Stackoverflow
Solution 2 - MysqlHemant ThoratView Answer on Stackoverflow
Solution 3 - MysqlKeet SugathadasaView Answer on Stackoverflow
Solution 4 - MysqlMahmood ShahbazianView Answer on Stackoverflow
Solution 5 - MysqlAtequer RahmanView Answer on Stackoverflow
Solution 6 - MysqlSREEKANTH KCView Answer on Stackoverflow
Solution 7 - Mysqlkoosha yeganehView Answer on Stackoverflow
Solution 8 - Mysqluser1877106View Answer on Stackoverflow
Solution 9 - Mysqluser6335777View Answer on Stackoverflow
Solution 10 - MysqlBernd BuffenView Answer on Stackoverflow