Using Shell to quickly log on to the MySQL database without Password
Background
When we want to log on to the mysql database through MySQL-client in Shell, we always need to enter the password again and again in great trouble.
In addition, if your root password is highly random (the LastPass method is good), it will be very costly to log on to the MySQL database once.
We usually log on to the database as follows:
root@imlonghao:~# mysql -uroot -pEnter password:
Is there a way to log on to the database securely and easily?
Method
The answer is yes, and MySQL has already helped us think about this problem!
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_passuser=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=mysqlrootpassword123321user=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--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.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.