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