標籤:shell
一、基礎環境1、版本cat /etc/debian_version 7.82、核心uname -r3.2.0-4-amd643、ip(eth0)10.1.10.11710.1.10.1854、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 $1EOF}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}redirectlogcreatedb 10.1.10.185createtb 10.1.10.185createdb 10.1.10.117createtb 10.1.10.117end=`date "+%s"` exit 0
本文出自 “7928217” 部落格,請務必保留此出處http://7938217.blog.51cto.com/7928217/1707546
shell批量增刪改查百庫百表(mysql)