说明:适用于linux下快速复制多个mysql数据库(如100个库)到另一机器
例:
src: 192.168.1.10:3306 nameA pwdA
des: 192.168.1.20:3306 nameB pwdB
步骤如下:
登陆192.168.1.20
1、
mysql -h192.169.1.10 -unameA -ppwdA -e "show dattabases;" | awk '{print $0}' > tmp生成包含1.10全部数据库名的临时文件,结果如:
dba
dbb
dbc
dbd
dbe
...
dbn
2、vi tmp
:%s/\n/ /g
将tmp文件中的换行转换为空格,内容如:
dba dbb dbc dbd dbe dbf ... dbn
3、新建sh文件,添加内容如下并执行
#!/bin/bash
srcip="192.168.1.10"
srcuser="nameA"
srcpwd="pwdA"
desip="192.168.1.20"
desuser="userB"
despwd="pwdB"
dbname=(dba dbb dbc dbd dbe dbf dbn)
count=${#dbname[@]}
echo db count:$count
declare -i i=0;
while [ "${dbname[$i]}" != "" ]
do
db=${dbname[$i]}
declare -i num=$i+1
echo now $num/$count dbname:$db
mysqladmin -h$desip -u$desuser -p$despwd create $db
mysqldump --opt -h$srcip -u$srcuser -p$srcpwd $db|mysql-h$desip -u$desuser -p$despwd $db
i=$i+1
done
结果如下:
dq-z@dq-z-tpe420:~/work$ ./db.sh
db count:7
now 1/7 dbname:dba
now 2/7 dbname:dbb
now 3/7 dbname:dbc
now 4/7 dbname:dbd
now 5/7 dbname:dbe
now 6/7 dbname:dbf
now 7/7 dbname:dbn
dq-z@dq-z-tpe420:~/work$
现在192.1168.1.20机器上已经有与1.10相同的所有数据库.