How to quickly log in to MySQL database without password under Shell

How to quickly log in to MySQL database without password under Shell

background

When we want to log in to the MySQL database through mysql-client in Shell, we always need to enter the password again and again, which is very troublesome.

Moreover, if your root password is highly random (LastPass is a good choice), then the cost of logging into the MySQL database once will be very high.

Usually we log in to the database like this, as follows

root@imlonghao:~#mysql -uroot -p
Enter password:

So, is there a way to log in to the database safely, simply and conveniently?

method

Of course the answer is yes, and MySQL has already thought about this problem for us!

Reference link: End-User Guidelines for Password Security

Use .my.cnf to log in quickly

Create a .my.cnf file in the ~/ directory. Of course, if you already have this file, just modify it!

I personally like to use vim, so we can do this

vim ~/.my.cnf

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.

Here is an example:

[client]
password=mysqlrootpassword123321
user=root

If you already have a .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, you should pay attention to setting the file permissions for 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 other than the default ~/.my.cnf, you need to use --defaults-file=file_name parameter. example:

root@imlonghao:~# mysql --defaults-file=/home/imlonghao/mysql-opts

Use environment variable MYSQL_PWD to log in quickly

MySQL will first use the parameters in the environment variables as running parameters

root@imlonghao:~# export MYSQL_PWD=your_pass

After setting, you don't need to enter the password again when you log in to MySQL again.

However, it should be noted that if you exit the current Shell, this environment variable will disappear.

What is more important to note is that the commands you enter in the Shell will be automatically saved, and you can see the commands you have entered in history.

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • HBASE commonly used shell commands, add, delete, modify and query methods
  • Shell script to implement mysql scheduled backup, deletion and recovery functions
  • How to simply process MySQL query results in shell
  • Write a mysql data backup script using shell
  • Automatic backup of MySQL database using shell script
  • Use shell scripts to add, delete, modify, and check mysql and configure my.cnf

<<:  Solution to multiple 302 responses in nginx proxy (nginx Follow 302)

>>:  Develop a vue component that encapsulates iframe

Recommend

Realize map aggregation and scattering effects based on vue+openlayer

Table of contents Preface: Result: 1. Polymerizat...

A brief discussion on the solution of Tomcat garbled code and port occupation

Tomcat server is a free and open source Web appli...

W3C Tutorial (8): W3C XML Schema Activities

XML Schema is an XML-based alternative to DTD. XM...

How to create your own image using Dockerfile

1. Create an empty directory $ cd /home/xm6f/dev ...

Detailed tutorial on installation and configuration of MySql 5.7.17 winx64

1. Download the software 1. Go to the MySQL offic...

Vue uses canvas handwriting input to recognize Chinese

Effect picture: Preface: Recently, I was working ...

MySQL slave library Seconds_Behind_Master delay summary

Table of contents MySQL slave library Seconds_Beh...

Pure CSS implementation of radio and checkbox effect example

radio-and-checkbox Pure CSS to achieve radio and ...

Table Tag (table) In-depth

<br />Table is a tag that has been used by e...

How to use Vue's idea to encapsulate a Storage

Table of contents background Function Purpose Ide...

Summary of the pitfalls of using primary keys and rowids in MySQL

Preface We may have heard of the concept of rowid...

Linux firewall status check method example

How to check the status of Linux firewall 1. Basi...

Detailed process of building nfs server using Docker's NFS-Ganesha image

Table of contents 1. Introduction to NFS-Ganesha ...