In shell development, many times we need to operate MySQL database (for example: query data, export data, etc.), but we can not enter the MySQL command line environment, we need to simulate the environment of MySQL in the shell environment, the use of MySQL-related commands, This paper summarizes several methods of shell operation of MySQL for your reference.
Scenario 1
- Mysql-uuser-ppasswd-e"Insert logtable values (...)"
Advantages: Simple statement
Disadvantages: Supported SQL is relatively simple
Scenario 2Prepare an SQL script with the name Update.sql, for example:
- CREATE TABLE ' user ' (
- ' ID ' varchar ($) not NULL COMMENT ' primary key ',
- ' username ' varchar (notNULL COMMENT ' username '),
- ' Password ' varchar (notNULL COMMENT ' user password '),
- ' CreateDate ' date not NULL COMMENT ' creation time ',
- ' Age ' int (one) not NULL COMMENT ' ages ',
- PRIMARY KEY (' id ')
- ) Engine=myisam DEFAULT Charset=utf8 comment=' user Information table ';
- DROP TABLE IF EXISTS ' Visit_log ';
- CREATE TABLE ' Visit_log ' (
- ' ID ' varchar (character) set UTF8 not NULL,
- ' Type ' int (one) is not NULL,
- ' Content ' text character set UTF8 not NULL,
- ' CreateDate ' date not NULL,
- PRIMARY KEY (' id ')
- ) Engine=myisam DEFAULT charset=latin1 comment=' access log ';
Create a new update_mysql.sh with the following content:
[Python]View Plaincopy
- Use CHBDB;
- SOURCE Update.sql
Then execute the following command:
[Python]View Plaincopy
- Cat update_mysql.sh | MySQL--user=root-ppassword
Advantages: Support for complex SQL scripts
Disadvantages:1> requires two files: Update.sql and update_mysql.sh2> Once there is an error in the middle, the script will not execute, for example: if the first table already exists, the following exception is reported: Error 1050 (42S01) at line 1 in File: ' Update.sql ': Table ' user ' already exists then the script exits and the second table cannot be created.
Scenario 3Create a new shell script with the following format:
- #!/bin/bash
- mysql-u*-h*-p* <<eof
- Your SQL script.
- Eof
For example:
- #!/bin/bash
- Mysql-uroot-ppassword <<eof
- Use CHBDB;
- CREATE TABLE User (
- ID varchar ($) not NULL COMMENT ' primary key ',
- Username varchar () not NULL COMMENT ' user name ',
- Password varchar () not NULL COMMENT ' user password ',
- CreateDate date not NULL COMMENT ' creation time ',
- Age int (one) not NULL COMMENT ' ages ',
- PRIMARY KEY (' id ')
- ) Engine=myisam DEFAULT Charset=utf8 comment=' user Information table ';
Advantages:1> support for complex SQL scripts 2> no additional files required
Disadvantages:1> table name, field cannot use single quotation marks, need to modify the original SQL statement 2> if there is an error in the middle, then the script will not execute, for example: if the first table already exists, the following exception will be reported: Error 1050 (42S01) at line 1 in file: ' Update.sql ': Table ' user ' already exists then the script exits and the second table cannot be created.
Scenario 4Prepare an SQL script, such as Update.sql, and then execute the following command:
[Python]View Plaincopy
- Mysql-uroot-ppassword < Update.sql
Pros: Support for complex SQL scripts
Disadvantages:1> Once there is an error in the middle, the script will not execute, for example: if the first table already exists, the following exception will be reported: Error 1050 (42S01) at line 1 in file: ' Update.sql ': Table ' user ' already exi STS then the script exits, and the second table cannot be created.
Execute MySQL command under shell