1. 程式人生 > >mysql一鍵建立從庫指令碼

mysql一鍵建立從庫指令碼

#!/bin/sh
myuser=root
mypass="root123"
mysock=/data/3306/mysql.sock
#main_path=/server/backup
data_path=/server/backup

[ ! -f $data_path ] && mkdir -p $data_path
log_file="$data_path"/mysqllogs_`date +%F`.log
data_file="$data_path"/mysql_backup_`date +%F`.sql.gz
mysql_path=/u01/application/mysql/bin

mysql_cmd="$mysql_path/mysql -u$myuser -p$mypass -S $mysock"
mysql_dump="$mysql_path/mysqldump -u$myuser -p$mypass -S $mysock -A -B -F --flush-privileges --master-data=1 --single-transaction --trigger --routines --hex-blob --events -e"

[ `$mysql_cmd -e "select  user,host from mysql.user"|grep rep|wc -l` -ne 1  ]  &&\
$mysql_cmd -e "grant replication slave on *.* to 'rep'@'%' identified by 'root123'"

$mysql_cmd >$log_file <<eof
flush tables with read lock;
system  echo "---------show master status result --------";
show master status;
system "$mysql_dump"|gzip >$data_file;
unlock tables;
eof
cat $log_file