10 time-saving MySQL command summaries _ MySQL

Source: Internet
Author: User
Tags mysql commands mysql manual rehash
10 time-saving MySQL command summary bitsCN.com

Although many GUI-based MySQL clients exist, such as the famous phpMyAdmin and SQLYog, I have always liked the native MySQL command line client. Indeed, I am not familiar with these command line interfaces (CLI) previously, it took us some time to get familiar with them, especially when you do not often work in an operating system with a powerful CLI environment, but after some exercises, you can use the CLI to manage users, browse your database, and execute other tasks. The pleasure is beyond the reach of other humans.

In this article, I will introduce some of the MySQL command line client skills I have accumulated at work, whether you try one or all of them, I am sure you will save a lot of time.

By the way, the MySQL command line client is applicable to all operating systems, including Windows, but because the Windows native CLI environment is daunting, it is recommended that Windows users download and install the Console, it is an alternative solution for Windows command line and provides more powerful functions, such as convenient text selection and multi-tag Windows.

1. automated login process

The correct configuration of the MySQL server requires you to provide the user name and password for identity authentication. generally, we can add the user name directly after the mysql command. for security reasons, the password will not keep up, when you press enter to execute the command, the command prompt will remind you to enter the password.

%> Mysql-u root-p Enter password: Welcome to the MySQL monitor. Commands end with; or g ....

With this small improvement, you can enter thousands of user names in a year, saving a few hours in the total logon time and creating one. my. the cnf file is stored in your home directory. for Windows, the file name is my. ini, and put it in the MySQL installation directory. in this file, add the following code. use your login information to replace the placeholder.

[Client] host = your_mysql_server user = your_username password = your_password

Be sure to correctly set the permission for this file to prevent sensitive data from being peeked.

2. automatic database failover

After logging on to the client, you need to switch to the target database. generally, we will use the following command to switch the database:

Mysql> use wjgilmore_dev;

If you want to automatically switch to the target database after logging on, you can add the following command in the file described in the previous step. Note that the location should also be in the [client] section:

Database = your_database_name

3. send commands from scripts

When designing a new database, I like to use the MySQL Workbench design mode and relationship. it is a very powerful tool that allows you to manage your mode on the GUI, then synchronize the data to the MySQL server, or export the SQL command to a file, so that you can import the data to MySQL later.

If you like handwriting code, such as creating a large number of stored procedures or executing a long connection, you can save the SQL statement as a file and then pass the file to the client for execution, such:

%> Mysql <schema. SQL

Of course, you need to specify the connection string, or use the configuration file as before.

4. vertical display results

Even in simple table mode, several columns of fields are contained. for example, the following table consists of 11 fields. when I perform a full-structure query, the input result is as follows:

Mysql> select * from accounts where username = 'wjgilmore ';
+ ---- + ----------- + ------------------ + ------------------------------
---- + ---------- + ----------- + ------------ + ----------- + --------------
-------------------- + --------------------- +
| Id | username | email | password | zip_code | latitude | longpolling | confirmed | recovery | created_on | last_login |
+ ---- + ----------- + ------------------ + -------------------------------
--- + ---------- + ----------- + ------------ + ----------- + -----------------
----------------- + --------------------- +
| 7 | wjgilmore | wj@wjgilmore.com | small | 43201 | 39.984577 |-83.018692 | 1 | 8bnnwtqlt2289q2yp81tuge82fty501h | 14:48:41 | 15:49:44 |
+ ---- + ----------- + ------------------ + ----------------------------------
+ ---------- + ----------- + ------------ + ----------- + ----------------------
------------ + --------------------- +

Obviously, we cannot accept or read this display result. use the G command to convert the ugly display result to a vertical one.

Mysql> select * from accounts where username = 'wjgilmore 'G
* *************************** 1. row ***************************
Id: 7 username: wjgilmore mail: wj@wjgilmore.com
Password: 2b877b4b825b48a9a0950dd5bd1f264d zip_code: 43201
Latitude: 39.984577 longpolling:-83.018692
Confirmed: 1 recovery: 8bnnwtqlt2289q2yp81tuge82fty501h
Created_on: 2010-09-16 14:48:41 last_login: 2010-10-27 15:49:44

It looks more comfortable.

5. enable the Tab key auto-completion function

Repeated input table names and field names are undoubtedly boring. you can pass the -- auto-rehash parameter to the mysql client, or add the following command to the my. ini file to enable the automatic completion function of the Tab key.

[Mysql] auto-rehash

6. change the prompt

More than once I want to view or modify the mode in which the notified table does not exist. in this case, I am often afraid. most of the time I log on to the wrong database, leading to the accidental deletion of tables that should not be deleted, modify the MySQL client prompt to display the database name of the current operation to avoid misoperation. to make the mysql client interactive, log on and execute the following command:

Mysql> prompt [d]> [dev_wjgilmore_com]>

You may want to maintain this effect permanently, which is simple. you only need to add the following command to your configuration file:

Prompt = [d]>

Of course, it is not limited to prompting the database name, but also displaying the current date and time, host name and user name. please read the MySQL manual for more comprehensive information.

7. use security updates to prevent disasters

As mentioned above, accidental deletion of a TABLE may lead to fear. I believe that more than one of me has a similar experience, except for the accidental execution of the DROP TABLE, even worse, the UPDATE command of the WHERE clause is ignored. for example, if you want to use the following command to modify the user name:

Mysql> UPDATE users set User = 'wjgilmore' WHERE User = 'wjgilmore-temp ';

However, when you are eager to go out for dinner, you may forget to input the WHERE clause

Mysql> UPDATE users set User = 'wjgilmore ';

Once you press enter, the consequences will be very serious. all user names in the users table will be changed to wjgilmore. to avoid such low-level errors that may cause catastrophic consequences, add the following command to the configuration file:

Safe-updates

8. use Command Documentation

Many users know the mysql client's built-in documentation. when you enter the help command, it displays a long list of commands.

Mysql> help
... List of all MySQL commands: Note that all text commands must be
First on line and end ';'? (?) Synonym
For 'help'. clear (c) Clear the current input statement.
Connect (r) Reconnect to the server. Optional arguments are db and host. delimiter (d)
Set statement delimiter ....

If you do not know how to use the DESCRIBE command, you only need to enter

Mysql> help describe;
Name: 'describe' Description: Syntax:
{DESCRIBE | DESC} tbl_name [col_name | wild] DESCRIBE provides information about the columns in a table.
It is a short cut for show columns from. These statements also display information for views. (See [help show columns] ..)...

9. use Pager

Sometimes you may want to view a certain row of data in the table, but the screen will usually display full screen data, in addition to using the LIMIT clause, enable the pager client, set your system paging utility:

Mysql> pager more PAGER set to 'more'

To disable pager, run the nopager command.

10. dump the output content to a file

You may occasionally need to output the SQL execution results to a text file. you can use the SELECT INTO OUTFILE command to achieve this purpose, or enable the tee command directly on the mysql client, and develop output files to implement the same functions, such:

Mysql> tee sales_report.txt

Summary

Whether you select some of these skills or try these 10 skills, they will save you a lot of time and energy and ease your pain. if you know other MySQL command line skills, share it in the comments!

Source: http://www.developer.com/db/10-command-line-timesavers-for-mysql-tasks.html

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.