Complete MySQL work with a script

Source: Internet
Author: User

1. Using MYSQL-E to execute MySQL SQL statements in the script

#!/bin/bash#simple MySQL Shell usagelogtime=`Date "+%y-%m-%d"' LOG=Call_sql_${logtime}.logEcho "Start Execute SQL statement at ' date '">>${log} #execute SQLStatMySQL-U root-p19930309-e"Tee/tmp/temp.loguse testdrop Tableifexists stu;create table stu (name varchar ( -), ageint) insert into Stu values ('Wangkun', A),('Amei', A),('Jack', -);Select*From stu;noteequit"Echo-E"\ n">>${log}Echo "below is output result:">>${log}Cat/tmp/temp.log >>${log}Echo "Scrtip execute successful.">>${log}exit

Execution results

  

 - at:£ belowisoutput result:+---------+------+| Name    | Age  |+---------+------+| Wangkun |    | | Amei    |    | | Jack    |    |+---------+------+Scrtip execute successful.

2. Through the pipe symbol

Select2.sql

Tee/home/hadoop_admin/mysql.loguse testdrop Tableifexists stu;create table stu (name varchar ( -), ageint) insert into Stu values ('Wangkun', A),('Amei', A),('Jack', -);Select*From stu;noteequit

  

[Email protected] mysql_shell]$ mysql-u root-p19930309 <./file'/home/hadoop_ Admin/mysql.log'name    'amei    Jack     Outfile disabled.

3. The command line calls the SQL file separately

Select2.sql

Tee/home/hadoop_admin/mysql.loguse testdrop Tableifexists stu;create table stu (name varchar ( -), ageint) insert into Stu values ('Wangkun', A),('Amei', A),('Jack', -);Select*From stu;noteequit

" Source Select2.sql "  file'/home/hadoop_admin/mysql.log'+---------+------+| Name    | Age  |+---------+------+| Wangkun |    | | Amei    |    | | Jack    |    |+---------+------+Outfile disabled.

4. The MySQL prompt in the shell script is lowered with SQL , and Oracle can do the same

select3.sh

  

#!/bin/Bashmysql-U root-p19930309 <<EOFTee/home/hadoop_admin/mysql.loguse testdrop Tableifexists stu;create table stu (name varchar ( -), ageint) insert into Stu values ('Wangkun', A),('Amei', A),('Jack', -);Select*From stu;noteequiteofexit

Status of implementation

  

[Email protected] mysql_shell]$./select3. SH  file'/home/hadoop_admin/mysql.log'name     Amei      A Jack      - Outfile disabled.

Complete MySQL work with a script

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.