用脚本完成mysql工作
2016-04-24 10:54
459 查看
1. 用mysql -e在脚本中执行mysql的sql语句
执行结果
2.通过管道符号
select2.sql
3.命令行单独调用sql文件
select2.sql
4. shell脚本中MySQL提示符下调用SQL , oracle 也可以这样干
select3.sh
执行情况
#!/bin/bash #simple mysql shell usage logtime=`date "+%Y-%m-%d"` LOG=call_sql_${logtime}.log echo "Start execute sql statement at `date`" >>${LOG} #execute sql stat mysql -u root -p19930309 -e " tee /tmp/temp.log use test drop table if exists stu; create table stu(name varchar(20),age int); insert into stu values('wangkun',12),('amei',12),('Jack',14); select * from stu; notee quit " echo -e "\n" >> ${LOG} echo "below is output result :" >> ${LOG} cat /tmp/temp.log >> ${LOG} echo "scrtip execute successful." >> ${LOG} exit
执行结果
Start execute sql statement at Mon Apr 18 23:23:01 CST 2016 below is output result : +---------+------+ | name | age | +---------+------+ | wangkun | 12 | | amei | 12 | | Jack | 14 | +---------+------+ scrtip execute successful.
2.通过管道符号
select2.sql
tee /home/hadoop_admin/mysql.log use test drop table if exists stu; create table stu(name varchar(20),age int); insert into stu values('wangkun',12),('amei',12),('Jack',14); select * from stu; notee quit
[hadoop_admin@master mysql_shell]$ mysql -u root -p19930309 < ./select2.sql Warning: Using a password on the command line interface can be insecure. Logging to file '/home/hadoop_admin/mysql.log' name age wangkun 12 amei 12 Jack 14 Outfile disabled.
3.命令行单独调用sql文件
select2.sql
tee /home/hadoop_admin/mysql.log use test drop table if exists stu; create table stu(name varchar(20),age int); insert into stu values('wangkun',12),('amei',12),('Jack',14); select * from stu; notee quit
[hadoop_admin@master mysql_shell]$ mysql -u root -p19930309 -e "source select2.sql" Warning: Using a password on the command line interface can be insecure. Logging to file '/home/hadoop_admin/mysql.log' +---------+------+ | name | age | +---------+------+ | wangkun | 12 | | amei | 12 | | Jack | 14 | +---------+------+ Outfile disabled.
4. shell脚本中MySQL提示符下调用SQL , oracle 也可以这样干
select3.sh
#!/bin/bash
mysql -u root -p19930309 <<EOF
tee /home/hadoop_admin/mysql.log use test drop table if exists stu; create table stu(name varchar(20),age int); insert into stu values('wangkun',12),('amei',12),('Jack',14); select * from stu; notee quit
EOF
exit
执行情况
[hadoop_admin@master mysql_shell]$ ./select3.sh Warning: Using a password on the command line interface can be insecure. Logging to file '/home/hadoop_admin/mysql.log' name age wangkun 12 amei 12 Jack 14 Outfile disabled.
相关文章推荐
- mysqlbinlog的使用
- 如何按规定的格式向mysql中导入数据
- mysql新建用户本地无法登录
- 处理mysql的查询语句去重案例一则
- mysqldumpslow使用
- mysql max函数不能用在where条件中的案例
- MySQL绿色版的安装(mysql-5.6.22-win32.zip)
- 跟我一起学习MySQL技术内幕(第五版):(第二章学习日记10)
- [mysql] ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
- mysql表的字符集uft8转换成gbk测试
- mysql ndb集群备份数据库和还原数据库的方法
- mysql 加了 skip-name-resolve不能链接数据库问题的解决方法
- mysql表名大小写敏感
- MySQL排错工具perror
- MySQL查询优化
- "Table 'mysql.plugin' doesn't exist"引发de血案
- 清空mysql的历史记录
- mysqldump 用法
- mysql5.5解压缩版安装步骤
- mysql管理工具