本文详细描述了如何在MySQL中使用master.sh和slave.sh脚本进行主从服务器的数据备份、同步用户权限设置、配置文件生成以及验证主从状态的过程。
首先将mysqlmasterbyslave文件夹(包含master.sh和slave.sh )贝到主服务器中,在主服务器上执行master.sh
输入序号,确认你要备份的数据库,将自动导出你选择的数据库和先备份你的my.cnf文件,和配置my.cnf!
设置用于同步的用户和密码
选择你的服务器IP,从服务器将查询这个IP用于同步!
最后会生成配置文件tb.ini,
内容如下
tbuser=xxxxx #同步用户tbpwd=xxxxx #同步密码File=mysql-bin.000009 #用于同步的日志文件Position=738 #日志文件位置IP=192.168.31.166 #服务器IPserver_uuid=ad35ab19-97bb-11eb-a83f-000c29c3cf9d #MYSQL的应用ID标识,确保唯一,主从不能一样!
把生成的所有文件拷贝到从服务器上!!
从服务器上执行slave.sh!
输入序号,选择要同步的数据库!
等待执行完毕,最后显示如下图!
表示主从同步状态OK!
测试在主服务器上新增一个表!!并插入数据
从服务器上查看
同步完成!!!
master.sh内容
变量相关内容user请填写你mysql的管理员账号!pwd填写你mysql的管理员密码
###变量相关 data="zabbix" user="root" pwd="Zabbix@123" backuppath="/winshare/backup" ###变量相关 clear cnf=`find / -name "my.cnf" ` echo backup your $cnf cp $cnf `date +%F`"[master]"${cnf##*/} items=(log-bin server-id binlog_format auto-increment-offset auto-increment-increment) values=(mysql-bin 1 mixed 1 10) for (( i = 0; i < ${#items[@]}; i++ )); do #echo ${items[$i]} sed -i "/${items[$i]}.*/d" $cnf echo ${items[$i]}=${values[$i]} >>$cnf done mysql -uroot -pZabbix@123 -e "show databases;"|grep -Ev "Database"|awk '{i++}{print "a["i-1"]="$1}'>tmp.txt while read line;do echo ${j:=0}:${line##*=} eval $line let j++ done <tmp.txt echo "select your Synchronize data,such as 2" read -p "" n #binlog-ignore-db binlog-do-db declare -r ignoredb="binlog-ignore-db" declare -r dodb="binlog-do-db" sed -i "/${ignoredb}.*/d" $cnf sed -i "/${dodb}.*/d" $cnf for (( i = 0; i < ${#a[@]}; i++ )); do if [[ $i -ne $n ]]; then echo ${ignoredb}=${a[$i]} >>$cnf else echo ${dodb}=${a[$i]} >>$cnf fi done systemctl restart mysqld sleep 2 systemctl list-units --type=service|grep -i -E "mysqld"|awk '{if($4=="running"&&$3=="active"){print $1",服务启动成功"}else{print $1",服务启动失败,请检查"}}' read -p "设置用于同步的用户" tbuser read -p "设置用于同步的密码" tbpwd echo tbuser=${tbuser}|tee tb.ini echo tbpwd=${tbpwd}|tee -a tb.ini #sql="show global variables like 'validate_password%'" 密码策略 mysql -u"${user}" -p"${pwd}" -e "GRANT REPLICATION SLAVE ON *.* TO '${tbuser}'@'%' IDENTIFIED BY '${tbpwd}';flush privileges;" -N #mysql -u"${user}" -p"${pwd}" -e "SELECT HOST,USER,Repl_slave_priv,password_last_changed from mysql.user WHERE USER='${tbuser}';" -N mysql -u"${user}" -p"${pwd}" -e "show grants for ${tbuser};" -N #备份数据库 mysql -u"${user}" -p"${pwd}" -e "flush tables with read lock;" -N mysqldump -u"${user}" -p"${pwd}" "${a[$n]}" -B -x> ./"${a[$n]}".sql mysql -u"${user}" -p"${pwd}" -e "unlock tables;" -N mysql -u"${user}" -p"${pwd}" -e "show master status\G;" |awk -F : '/File|Position/ {print $1"="$2 }'|tr -d " "|tee -a tb.ini #你服务器IP echo please confirm your serverIP,such as 1 ifconfig | grep inet | awk '{print "["NR"]"$2}' read -p "" ip ifconfig | grep inet | awk 'NR=="'${ip}'"{print "IP="$2}'|tee -a tb.ini #关键是取UUID,防止是克隆主机一样! mysql -u"${user}" -p"${pwd}" -e "show variables like '%server_uuid%'"|awk '/uuid/{print $1"="$2}'|tee -a tb.ini ———————————————— 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 原文链接:https://blog.csdn.net/u014332402/article/details/134475802
slave.sh内容
###变量相关 user="root" pwd="Zabbix@123" ###变量相关 clear find ./ -name "*.sql" -type f|awk '{i++}{print "a["i-1"]="$0 }' >tmp.txt while read line;do eval $line done <tmp.txt echo "select your Synchronize data,such as 0" for (( i = 0; i < ${#a[@]}; i++ )); do name[$i]=`echo ${a[i]##*/}|awk '{gsub(".sql","",$0);print $0}'` echo "[${i}]${name[$i]} Path:${a[i]}" done read -p "" sb echo ${name[$sb]} if [[ ! -e ./tb.ini ]]; then echo tb.ini not exist! please check !!! fi while read line;do eval $line done <./tb.ini echo 用于同步的用户 $tbuser echo 用户密码 $tbpwd echo 数据库同步的日志文件 $File echo 文件位置 $Position echo 服务器IP $IP echo 主服务器UUID ${server_uuid} mysql -u${tbuser} -p${tbpwd} -h ${IP} -e "show grants for ${tbuser} ;" if [[ $? -ne 0 ]]; then echo [master]mysqlserver can not connected! please check!! exit fi cnf=`find / -name "my.cnf" ` echo backup your $cnf cp $cnf `date +%F`"[slave]"${cnf##*/} items=(relay-log server-id) values=(relay-log 2 information_schema performance_schema) for (( i = 0; i < ${#items[@]}; i++ )); do #echo ${items[$i]} sed -i "/${items[$i]}.*/d" $cnf echo ${items[$i]}=${values[$i]} >>$cnf done sed -i "/replicate-ignore-db.*/d" $cnf ignoredb=information_schema,performance_schema echo $ignoredb | awk '{split($0,a,",");for(i in a) system("echo replicate-ignore-db="a[i]"|tee -a '${cnf}'")}' #关键对比UUID,一样是克隆机要改! slave_uuid=$(mysql -u"${user}" -p"${pwd}" -e "show variables like '%server_uuid%'"|awk '$0~"uuid"{print $2}') if [[ "${slave_uuid}" == "${server_uuid}" ]]; then echo UUID IS SAME! MUST UPDATE! read -p "press any key for continue!" s find / -name "auto.cnf"|while read line;do echo auto backup $line! mv $line $line`date +%F`".bak" done else echo slave_uuid:${slave_uuid} fi #service mysqld restart systemctl restart mysqld sleep 2 systemctl list-units --type=service|grep -i -E "mysqld"|awk '{if($4=="running"&&$3=="active"){print $1",服务启动成功"}else{print $1",服务启动失败,请检查"}}' mysql -u${user} -p${pwd} < ${a[$sb]} sql=" STOP SLAVE; CHANGE MASTER TO MASTER_HOST='${IP}', MASTER_USER='${tbuser}', MASTER_PASSWORD='${tbpwd}', MASTER_LOG_FILE='${File}', MASTER_LOG_POS=${Position}; SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; START SLAVE; " mysql -u${user} -p${pwd} -e "${sql}" systemctl restart mysqld sleep 2 systemctl list-units --type=service|grep -i -E "mysqld"|awk '{if($4=="running"&&$3=="active"){print $1",服务启动成功"}else{print $1",服务启动失败,请检查"}}' mysql -u"${user}" -p"${pwd}" -e "show slave status\G;SHOW VARIABLES LIKE '%server_%';" ———————————————— 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 原文链接:https://blog.csdn.net/u014332402/article/details/134475802
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u014332402/article/details/134475802
来源:本文内容搜集或转自各大网络平台,并已注明来源、出处,如果转载侵犯您的版权或非授权发布,请联系小编,我们会及时审核处理。
声明:江苏教育黄页对文中观点保持中立,对所包含内容的准确性、可靠性或者完整性不提供任何明示或暗示的保证,不对文章观点负责,仅作分享之用,文章版权及插图属于原作者。
Copyright©2013-2024 JSedu114 All Rights Reserved. 江苏教育信息综合发布查询平台保留所有权利
苏公网安备32010402000125 苏ICP备14051488号-3技术支持:南京博盛蓝睿网络科技有限公司
南京思必达教育科技有限公司版权所有 百度统计