Connecting to MySQL without entering a password

Suppose you need to make the user root can connect to MySQL from localhost without entering a password.

To do this, create the MySQL configuration file in the home directory of the user you are connecting from (in the nano text editor, press CTRL + X to exit, y / n to save or cancel changes):

sudo nano ~/.my.cnf

For security purposes, we will configure access to the .my.cnf file only for the root user of the system.

sudo chown root:root ~/.my.cnf
sudo chmod 400 ~/.my.cnf

And add the client section, specifying the user’s login and password in it, as well as several other parameters:

[client]
host=localhost
user=root
password=PASSWORD
socket=/var/run/mysqld/mysqld.sock

Let’s try to connect, immediately the MySQL console should appear (without requesting a password):

mysql

You can also write the path to the file with login and password, for example:

mysql --defaults-extra-file=/etc/zabbix/.mysql.cnf -e "QUERY;"

Specifying a password on the command line is not recommended, and accordingly, if you specify, a warning will be displayed:

mysql: [Warning] Using a password on the command line interface can be insecure.

Did my article help you? How about buying me a cup of coffee as an encouragement? Buy me a coffe.

Leave a comment

Leave a Reply