MySQL 同步多张表到目标库
vi /etc/my.cnf
#目标库
[mysql]
host=172.16.1.10
user=root
password=xxxxxxxx
#源数据库
[mysqldump]
host=127.0.0.1
user=root
password=xxxxxxxx
ssl-mode=DISABLED
=======================================================
#!/bin/bash
# /etc/my.cnf 文件中配置源库和目标库数据库连接信息
sou_mysql_db_name='mydatabase' #源中央库名称
des_mysql_db_name='youdatabase' #目标车站库名称
# 两张数据库中相同的表的名字,同步哪些填哪些,空格相隔
sou_mysql_table_name=(
tb_amazon_asin_info
tb_amazon_keyword_ranking
tb_amazon_pd_best_seller_rank
tb_amazon_pd_bought_together
tb_amazon_pd_five_des
tb_amazon_pd_image
tb_amazon_pd_offers
tb_amazon_pd_product_dec_img
tb_amazon_pd_product_details
tb_amazon_pd_product_overview
tb_amazon_pd_similar_product
tb_amazon_product_dynamics
tb_amazon_product_dynamics_history
tb_amazon_product_level
)
# 存放备份sql文件的目录
sync_db_dir='/home/mysql/backup_sql'
# 存放同步数据日志目录
log_file='/home/mysql/syncdata.log'
func_writeLog(){
local getString=$1
if [ $? -eq 0 ];then
echo "TIME:$(date +"%Y%m%d_%H:%M"),The ${getString}"
echo "TIME:$(date +"%Y%m%d_%H:%M"),The ${getString}" >>${log_file}
else
echo "TIME:$(date +"%Y%m%d_%H:%M"),The ${getString}"
echo "TIME:$(date +"%Y%m%d_%H:%M"),The ${getString}" >>${log_file}
fi
}
func_sourceBackupCmd(){
for tabname in $(echo ${sou_mysql_table_name[@]}|sed 's/ / /g');do
home=/etc /usr/local/mysql/bin/mysqldump \
--set-gtid-purged=off \
--insert-ignore=true \
--default-character-set=utf8 \
-t ${sou_mysql_db_name} ${tabname} >${sync_db_dir}/${sou_mysql_db_name}_${tabname}.sql
func_writeLog "${sou_mysql_db_name}_${tabname}.sql backup ok"
done
}
func_syncMysqlCmd(){
for sqlname in $(ls ${sync_db_dir}/*.sql);do
home=/etc /usr/local/mysql/bin/mysql \
--default-character-set=utf8 ${des_mysql_db_name} < ${sqlname}
func_writeLog "${sqlname} load ok"
[ $? -eq 0 ] && rm -f ${sqlname}
func_writeLog "${sqlname} deleted ok"
done
}
main(){
func_sourceBackupCmd;
func_syncMysqlCmd;
}
main