Home > Database > Mysql Tutorial > Sharing on how to quickly log in to MySQL database without password under Shell

Sharing on how to quickly log in to MySQL database without password under Shell

黄舟
Release: 2017-06-18 10:35:47
Original
1510 people have browsed it

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:
Copy after login

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
Copy after login

and then write the following information in the file


[client]
password=your_pass
user=your_user
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template