1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
我的博客已迁移到xdoujiang.com请去那边和我交流
一、基础环境
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:jimmygong@taomee.com
#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
|
本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1707546,如需转载请自行联系原作者