一、基础环境 1、版本 cat /etc/debian_version 7.8 2、内核 uname -r 3.2.0-4-amd64 3、ip(eth0) 10.1.10.117 10.1.10.185 4、2台机器上都设置权限 grant all privileges on `xxx%`.* to ‘jimmy‘@‘10.1.10.%‘ identified by ‘redhat‘; flush privileges; 5、要注意数据库必须监听在相应本机IP地址上 二、shell脚本函数说明 redirectlog #记录日志 createdb #创建库 createtb #创建表 三、具体代码 cat createdbtb.sh #!/bin/bash #-------------------------------------------------- #Author:jimmygong #Email:[email protected] #FileName:createdbtb.sh #Function: #Version:1.0 #Created:2015-10-29 #-------------------------------------------------- currdate=$(date +%Y%m%d) dbuser="jimmy" dbpass="redhat" logdir="/root/log" tmptb="table.sql" tmpdb="db.sql" function redirectlog () { logfile=$logdir/${currdate}log mkdir -p $logdir exec 1>$logfile exec 2>$logfile } function createdbsql () { cat <<EOF > $tmpdb create database $1 EOF } function createdb () { dbfront=0 dbend=100 while [[ $dbfront -lt $dbend ]] do dbx=`printf "%02d" $dbfront` createdbsql xxx$dbx cat $tmpdb|mysql -u"$dbuser" -p"$dbpass" -h"$1" let "dbfront+=1" done } function createtbsql () { cat <<EOF > $tmptb CREATE TABLE IF NOT EXISTS t_xxx_$1( userid INT UNSIGNED NOT NULL DEFAULT ‘0‘, toolid INT UNSIGNED NOT NULL DEFAULT ‘0‘, number INT UNSIGNED NOT NULL DEFAULT ‘0‘, get_time INT UNSIGNED NOT NULL DEFAULT ‘0‘, PRIMARY KEY (userid, toolid) ) ENGINE=innodb, CHARSET=utf8; EOF } function createtb () { dbfront=0 dbend=100 tablefront=0 tableend=100 while [[ $dbfront -lt $dbend ]] do dbx=`printf "%02d" $dbfront` echo $dbx while [[ $tablefront -lt $tableend ]] do tbx=`printf "%02d" $tablefront` createtbsql $tbx cat $tmptb|mysql -u"$dbuser" -p"$dbpass" -h"$1" "xxx$dbx" let "tablefront+=1" done let "dbfront+=1" let "tablefront=0" done } redirectlog createdb 10.1.10.185 createtb 10.1.10.185 createdb 10.1.10.117 createtb 10.1.10.117 end=`date "+%s"` exit 0
时间: 2024-10-29 19:12:37