Allow linux root user mysql root access without password

On cPanel when I am logged in as root and type "mysql" without hostname and password it gives me direct access to mysql root user.

I would like to do this for one of my non-cpanel server where the linux root user gets password less logon to mysql root user in the same way as it does on cPanel.

Is this possible ?


Solution 1:

The easiest way to do this is to use a client section of the ~/.my.cnf file, and add the credentials there.

[client]
user=root
password=somepassword
...

it's a good idea to make that file readable only by root too.

Solution 2:

For mysql 5.7+, if you set empty password for the initial setup, mysql will automatically use auth_socket as a strategy. An attempt to update password without change the strategy will have no result. You can always able to login without using password if your user is root.

Solution Run following command to change the authentication strategy and set the password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ''

reference: https://www.percona.com/blog/2016/03/16/change-user-password-in-mysql-5-7-with-plugin-auth_socket/

Solution 3:

As of MySQL 5.6.6, you can use the mysql_config_editor to create an encrypted file that will log you in automatically:

mysql_config_editor set --login-path=client --host=localhost --user=root --password

Then enter the password when prompted.

Note: if your password has '#' in it, and possibly other characters, use single quotes when entering the password.