Last updated on August 28, 2020 by Dan Nanni
In order to log in to a MySQL server, you can run
mysql command along with your login credentials and server's IP address as arguments. For example:
$ mysql -u $MYSQL_ROOT -p $MYSQL_PASS -h 192.168.10.1
However, besides the inconvenience of typing extra arguments, using plain-text login credentials in a command line like above is really not a secure way to access a MySQL server. In a multi-user Linux environment, what you type in command line can easily be revealed to others who happen to run
ps on the same host at the same time.
MySQL offers a way for you to log in to MySQL server without password, by using an external MySQL configuration file. In Linux, there are two different kinds of MySQL configuration files: (1)
/etc/my.cnf and (2)
~/.my.conf. While any system-wide MySQL configuration is defined in
/etc/my.cnf, any user-specific MySQL configuration is stored in
~/.my.cnf. You can leverage
~/.my.cnf to define your MySQL login credential in the file.
$ vi ~/.my.cnf
[client] user=alice password=alice_passwd host=192.168.10.1
Make sure to have the configuration file readable to you only.
$ chmod 0600 ~/.my.cnf
~/.my.cnf is created, simply typing
mysql command will let you log in to
alice, and you no longer need to provide login password separately.
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.