Execute MySQL command under shell

Source: Internet
Author: User
Tags mysql in mysql command line

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
    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:
  1. CREATE TABLE ' user ' (
  2. ' ID ' varchar ($) not NULL COMMENT ' primary key ',
  3. ' username ' varchar (notNULL COMMENT ' username '),
  4. ' Password ' varchar (notNULL COMMENT ' user password '),
  5. ' CreateDate ' date not NULL COMMENT ' creation time ',
  6. ' Age ' int (one) not NULL COMMENT ' ages ',
  7. PRIMARY KEY (' id ')
  8. ) Engine=myisam DEFAULT Charset=utf8 comment=' user Information table ';
  9. DROP TABLE IF EXISTS ' Visit_log ';
  10. CREATE TABLE ' Visit_log ' (
  11. ' ID ' varchar (character) set UTF8 not NULL,
  12. ' Type ' int (one) is not NULL,
  13. ' Content ' text character set UTF8 not NULL,
  14. ' CreateDate ' date not NULL,
  15. PRIMARY KEY (' id ')
  16. ) Engine=myisam DEFAULT charset=latin1 comment=' access log ';
Create a new update_mysql.sh with the following content: [Python]View Plaincopy
    1. Use CHBDB;
    2. SOURCE Update.sql
Then execute the following command: [Python]View Plaincopy
    1. 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:  
    1. #!/bin/bash
    2. mysql-u*-h*-p* <<eof
    3. Your SQL script.
    4. Eof
For example:  
  1. #!/bin/bash
  2. Mysql-uroot-ppassword <<eof
  3. Use CHBDB;
  4. CREATE TABLE User (
  5. ID varchar ($) not NULL COMMENT ' primary key ',
  6. Username varchar () not NULL COMMENT ' user name ',
  7. Password varchar () not NULL COMMENT ' user password ',
  8. CreateDate date not NULL COMMENT ' creation time ',
  9. Age int (one) not NULL COMMENT ' ages ',
  10. PRIMARY KEY (' id ')
  11. ) 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
    1. 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

Related Article

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.