MySQL without password
The common form to log in to MySQL server, is running a mysql command with your login credentials and server’s IP address as arguments. For example:
mysql -u $MYSQL_ROOT -p$MYSQL_PASS
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.
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:
- /etc/my.cnf and
- ~/.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.
vim ~/.my.cnf
We put our MySQL user in the configuration file:
[client]
user=root
password=$PASSWORD_ROOT
Make sure to have the configuration file readable to you only.
chmod 0600 ~/.my.cnf
Once ~/.my.cnf is created, simply typing mysql command will let you log in to the MySQL server as root, and you no longer need to provide login password separately.
mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14787
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>