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

Share MySql8.0.19 installation pit record

The previous article introduced the installation ...

Vue custom components use event modifiers to step on the pit record

Preface Today, when I was using a self-written co...

Element-ui's built-in two remote search (fuzzy query) usage explanation

Problem Description There is a type of query call...

Ubuntu 18.04 obtains root permissions and logs in as root user

Written in advance: In the following steps, you n...

Detailed explanation of the use of MySQL DML statements

Preface: In the previous article, we mainly intro...

Front-end development must learn to understand HTML tags every day (1)

2.1 Semanticization makes your web pages better u...

How to use nodejs to write a data table entity class generation tool for C#

Although Microsoft provides T4 templates, I find ...

MySQL master-slave replication principle and points to note

Written in front I have been writing a special to...

Linux kernel device driver virtual file system notes

/******************** * Virtual File System VFS *...

How to analyze SQL execution plan in MySQL through EXPLAIN

Preface In MySQL, we can use the EXPLAIN command ...

A brief discussion on the corresponding versions of node node-sass sass-loader

Table of contents The node version does not corre...

Detailed explanation of MySQL database paradigm

Preface: I have often heard about database paradi...