---
title: "Linux下批处理模式使用Mysql"
author: "jef"
date: "2009-05-13T16:11:08Z"
tags: []
slug: "linux-mysql"
draft: false
lastmod: "2009-05-14T21:06:32Z"
id: 385723
url: "https://jef.iteye.com/blog/385723"
---

一个linux下mysql批量查询的shell脚本：

<span style="color:indigo">*#!/bin/sh

mysql -u****** -p****** dbname --default-character-set=gb2312 -vvv << eof
show create table log;
show index from log;

explain partitions select count(*) from log where date = 20090505 and time >= 000500 and time <= 002205;
explain partitions select distinct(domain) from log where date = 20090505 and time >= 000500 and time <= 002205 and sip = 3232235534;

select count(*) from log where date = 20090505 and time >= 000500 and time <= 002205;
select distinct(domain) from log where date = 20090505 and time >= 000500 and time <= 002205 and sip = 3232235534;

exit
eof

exit*</span>

1）	为了回显以输出被执行的命令（格式化结果及每条命令执行的时间，同交互式使用mysql），使用mysql -vvv。
2）	可通过nohup命令后台运行该脚本并将脚本输出重定向指定文件。
<span style="color:indigo"><u>*# nohup ./dbtesting.sh > dbtesting.log &*</u></span>
3）	实时查看输出文件
<span style="color:indigo"><u>*# tail -f dbtesting.log*</u></span>


[参考：MySQL 5.1参考手册 3.5. 在批处理模式下使用mysql](http://dev.mysql.com/doc/refman/5.1/zh/tutorial.html#batch-mode)