美文网首页
记一次部署mysql-ms

记一次部署mysql-ms

作者: 带着小猪闯天下 | 来源:发表于2019-11-04 14:28 被阅读0次

mysql主从复制部署

环境:selinux & firewall

getenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld

master 配置

1,修改my.cnf
vi /etc/my.cnf(添加以下内容)

server-id=1
log-bin=master-bin  ##二进制日志名

重启:

systemctl restart mysqld

2.创建同步用户

GRANT REPLICATION SLAVE ON *.* to 'repo'@'%' identified by 'repo';
flush privileges;

3,查看当前二进制日志的信息
show master status;

image.png

slave配置

1,首先测试master创建的同步用户是否可用


image.png

2,修改my.cnf
vi /etc/my.cnf

server-id=2
log-bin=slave-bin

重启:
systemctl restart mysqld
3,开始同步(使用root登录mysql)

mysql> change master to
master_host='master-ip',
master_user='repo',
master_password='repo',
master_log_file='master-bin.000004',
master_log_pos=785;

最后两行为master第三部操作看到的文件名

开始同步:
start slave;
查看状态:
show slave status\G;


两个Yes即同步成功;

测试:
master:


image.png

slave:


image.png

成功

脚本:(修改好mysql配置文件my.cnf后再使用此脚本)

##mysql主备模式
MYSQL_MASTER_IP=xxxx   #自己定义master-ip
MYSQL_SLAVE_IP=xxxx      #自己定义slave-ip
root_passwd=123456
r_user=repo
r_passwd=repo

##创建同步用户
ssh -Tq ${MYSQL_MASTER_IP}<<EOF
/usr/local/mysql/bin/mysql -uroot -p${root_passwd} <<eof
GRANT REPLICATION SLAVE ON *.* to '${r_user}'@'%' identified by '${r_passwd}';
flush privileges;
eof
EOF


postion_num=$(ssh -Tq $MYSQL_MASTER_IP<<eof
    /usr/local/mysql/bin/mysql -uroot -p${root_passwd} -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
eof
)

#bin-log文件名 
log_name=$(ssh -Tq $MYSQL_MASTER_IP<<eof
    /usr/local/mysql/bin/mysql -uroot -p${root_passwd} -e 'show master status\G' 2>/dev/null|grep File |cut -f2 -d :|sed -e 's/^[ \t]*//g'
eof
)
echo $postion_num
echo $log_name

#执行同步

ssh -Tq ${MYSQL_SLAVE_IP}<<EOF
/usr/local/mysql/bin/mysql -uroot -p'${root_passwd}' <<eof
change master to master_host='${MYSQL_MASTER_IP}',master_port=3306,master_user='${r_user}',master_password='${r_passwd}',master_log_file='${log_name}',master_log_pos=${postion_num};
start slave;
eof
EOF

#检查是否主从复制搭建成功

running_num=$(ssh -Tq $MYSQL_SLAVE_IP <<eof
   /usr/local/mysql/bin/mysql -uroot -p${root_passwd} -e "show slave status\G" 2>/dev/null |grep "Running:"|wc -l
eof
)

if [ $running_num -eq 2 ];then
     echo -e "\033[42;37m mysql主从搭建成功 \033[0m"
  else
     echo -e "\033[41;37m mysql主从搭建失败 \033[0m"
fi

相关文章

网友评论

      本文标题:记一次部署mysql-ms

      本文链接:https://www.haomeiwen.com/subject/rpqdbctx.html