How to allow a Linux user access to MySQL without password.

A Linux user may need to frequently login to MySQL of the server. If he logs in as the same MySQL user each time, there is a way to save these details and login to mysql MySQL without password every single time. We can do this by creating a file named .my.cnf in the home directory of the user. The username and password saved for this.

Following displays an example .my.cnf file for user k to enter MySQL without password.

[k@e21 ~]$ pwd
/home/k
[k@e21 ~]$ cat .my.cnf
[client]
user=root
password='Testpassword1'
[k@e21 ~]$ 

You must also ensure that the file is readable and writable only to the Linux user.

[k@e21 ~]$ ll .my.cnf 
-rw-------. 1 k k 44 May 19 10:11 .my.cnf
allow a Linux user access to MySQL without password. using .my.cnf file

Please note that the password here shown as an example (Testpassword1) is just for an example. This is a very weak password and shouldn’t be used as password in real case scenarios. Please also be aware that saving passwords in text file is not the perfect idea. It is possible that a hacker can get into the Linux user account, but cannot get into mysql, if you don’t have MySQL password in a file in text format.

Add a Comment

Your email address will not be published. Required fields are marked *