This article mainly introduces you to the method of realizing password-free quick login MySQL database under Shell. The step-by-step introduction in the article is very detailed through sample code, which has certain reference and learning value for everyone. Friends who need it, please follow the editor to take a look.
Background
When we want to log in to the MySQL database through mysql-client under Shell, we always need to be very troublesome Enter your password again and again.
Moreover, if your root password is highly random (LastPass is good), then the cost of logging into the MySQL database once will be very high.
Usually we log in like this when we log in to the database, as follows
root@imlonghao:~# mysql -uroot -p Enter password:
So, is there a way to be safe , and can log in to the database simply and conveniently?
Method
Of course 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 in
In~/ Create a new .my.cnf file in the directory. Of course, if you already have this file, just modify it directly!
I personally like to use the vim method, so we can do this
vim ~/.my.cnf
and then write the following information in the file
[client] password=your_pass user=your_user
Note:Change your_pass and your_user to the password and username of the user you want to log in
The following is an example:
[client] password=mysqlrootpassword123321 user=root
If you already have the .my.cnf file, just write the information in the [client] field!
Note: Since your password is written in plain text in the .my.cnf file, please pay attention to setting the file permissions of this file
root@imlonghao:~# chmod 400 ~/.my.cnf
After saving, we can directly use the mysql command to log in to the MySQL database!
Note: If you need to specify a settings file instead of using the default ~/.my.cnf, you need to use --defaults- file=file_name
parameters. Example:
root@imlonghao:~# mysql --defaults-file=/home/imlonghao/mysql-opts
Using environmentVariables MYSQL_PWD Quick login
MySQL priority The parameters in the environment variable will be used as the running parameters
root@imlonghao:~# export MYSQL_PWD=your_pass
After setting, you do not need to enter the password again when logging in to mysql again.
However, it should be noted that if you exit the current Shell, this environment variable will disappear.
What needs more attention is that the commands you enter in the Shell will be automatically saved, and history can see the commands you entered.
Summarize
The above is the detailed content of Sharing on how to quickly log in to MySQL database without password under Shell. For more information, please follow other related articles on the PHP Chinese website!