#!/bin/bash
#mysql. Sh
Mysql= ' Mysql-uroot-ptogest '
Sql= ' show databases;use togest_sy_department;show tables; '
$mysql-E "$sql"
Mysql-uroot-ptogest--tee= "Mysql.log"
Shell Operation MySQL
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
[Python] View plaincopy
- MYSQL-UUSER-PPASSWD-E "Insert logtable values (...)"
Pros: Simple statement
Cons: Supported SQL is relatively simple
Scenario 2
Prepare an SQL script with the name Update.sql, for example:
[Python] View plaincopy
- 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 ';
- DROP TABLE IF EXISTS
visit_log
;
- CREATE TABLE
visit_log
(
-
ID
varchar (character) set UTF8 not NULL,
- Code>type Int (one) not NULL,
-
content
text character set UTF8 not null,
-
Crea Tedate
date not NULL,
- PRIMARY KEY (
ID
)
-
) Engine=myisam DEFAULT charset=latin1 Comment= ' Access log ';
Create a new update_mysql.sh with the following contents:
[Python] View plaincopy
- Use CHBDB;
- SOURCE Update.sql
Then execute the following command:
[Python] View plaincopy
- Cat update_mysql.sh | MySQL--user=root-ppassword
Pros: Support for complex SQL scripts
Disadvantages:
1> need two files: Update.sql and update_mysql.sh
2> 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 3
Create a new shell script with the following format:
[Python] View plaincopy
- #!/bin/bash
- Mysql-u- h -p* <<eof
- Your SQL script.
- Eof
For example:
[Python] View plaincopy
- #!/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> 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 4
Prepare 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 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.
大家知道在mysql命令行中使用source命令,即使中间出错,后续脚本也会继续执行,但是如上几种方式,均无法解决该问题,如果大家有好的建议,请回复,谢谢。
For more articles, please follow: http://www.ilovehai.com
Shell Operation MySQL