How to quickly log on to the MySQL database without a password in Shell
Method
The answer is yes, and MySQL has already helped us think about this problem! How to quickly log on to the MySQL database without a password in Shell
Reference link: End-User Guidelines for Password Security
Use. my. cnf to quickly log on
In ~ /Directory to create a new. my. cnf file. Of course, if you already have this file, simply modify it!
I personally like to use the vim algorithm, so we can do this.
Vim ~ /. My. cnf
Then write the following information into the file.
[Client]
Password = your_pass
User = your_user
Note: Modify your_pass and your_user the password and user name of the user you want to log on
The following is an example:
[Client]
Password = mysqlrootpassword123321
User = root
If you already have the. my. cnf file, write the information in the [client] field!
Note: Because your password is clearly written in the. my. cnf file, you must set the File Permission for this file.
Root @ imlonghao :~ # Chmod 400 ~ /. My. cnf
After saving, we can directly log on to the mysql database using the MySQL command!
Note: If you need to specify a setting file without using the default ~ /. My. cnf, you need to use the -- defaults-file = file_name parameter. Example:
Root @ imlonghao :~ # Mysql -- defaults-file =/home/imlonghao/mysql-opts
Use the environment variable MYSQL_PWD to quickly log on
MySQL uses parameters in environment variables as running parameters first.
Root @ imlonghao :~ # Export MYSQL_PWD = your_pass
After setting, you do not need to enter the password again when logging on to mysql again.
However, if you exit the current Shell, the environment variable will disappear.
Note that the commands you input in Shell will be automatically saved, and the commands you entered will be displayed in history.