#!/bin/bash
export master_ip=192.168.7.206
export slave_ip=192.168.7.207
export root_passwd=123456
 
echo '#1.取master主机ip的后三位作为master的server_id'
export master_server_id=`echo $master_ip |awk -F . '{print $4}'`
 
echo '#2.取slave主机ip的后三位作为slave的server_id'
export slave_server_id=`echo $slave_ip |awk -F . '{print $4}'`
 
echo '#3.取出master主库的postion'
postion_num=$(ssh -Tq $master_ip <<eof
mysql -uroot   -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
--#mysql -uroot -p123456  -h$master_ip  -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
eof
)
 
echo '#4.取出master主库的log_name'
log_name=$(ssh -Tq $master_ip <<eof
mysql -uroot -p123456  -e 'show master status\G' 2>/dev/null|grep File |cut -f2 -d :
eof
)
 
echo '#5.事先修改master主库的server_id'
echo $master_server_id
#ssh  ${master_ip} <<EOF
mysql -uroot -h$master_ip  <<eof
--#set global server_id=echo $master_ip | cut -b 11,12,13
set global server_id=$master_server_id;
eof
service mysql restart
 
echo '#6.事先修改slave从库的server_id'
echo $slave_server_id
mysql -uroot -h$slave_ip  <<eof
set global server_id=$slave_server_id;
eof
 
echo '#7.slave库设置密码和配置'
#ssh -Tq `${slave_ip}` <<EOF
mysql -uroot -h192.168.7.207  <<eof
use mysql;
stop slave;
select sysdate();
flush privileges;
#set password = password("${root_passwd}");
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${root_passwd}';
GRANT ALL PRIVILEGES ON *.* TO 'root'@localhost IDENTIFIED BY '${root_passwd}';
--#change master to master_host='192.168.7.206',master_port=3309,master_user='root',master_password='123456', master_log_file='bin.000017',master_log_pos=8888;
--#change master to master_host='192.168.7.206',master_port=3309,master_user='root',master_password='123456', master_log_file='${log_name}',master_log_pos=${postion_num};
flush privileges;
eof
 
echo '#8.slave库做chang master 操作'
ssh -Tq $master_ip <<EOF
service mysql restart
EOF
 
ssh -Tq $slave_ip <<EOF
service mysql restart
EOF
 
export master_ip=192.168.7.206
export slave_ip=192.168.7.207
 
echo '#3.取出master主库的postion'
postion_num=$(ssh -Tq $master_ip <<eof
--#mysql -uroot   -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
mysql -uroot -p123456  -h$master_ip  -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
eof
)
 
echo '#4.取出master主库的log_name'
log_name=$(ssh -Tq $master_ip <<eof
mysql -uroot -p123456  -e 'show master status\G' 2>/dev/null|grep File |cut -f2 -d :
eof
)
echo $postion_num
echo $log_name
mysql -uroot -h192.168.7.207  <<eof
 
--#change master to master_host='192.168.7.206',master_port=3309,master_user='root',master_password='123456', master_log_file='bin.000017',master_log_pos=8888;
change master to master_host='${master_ip}',master_port=3309,master_user='root',master_password='123456',master_log_file='${log_name}',master_log_pos=${postion_num};;
 
 
start slave;
select sysdate();
eof
exit;

MySQL主从同步最佳实践的更多相关文章

  1. 这次一定要教会你搭建Redis集群和MySQL主从同步(非Docker)

    前言 一直都想自己动手搭建一个Redis集群和MySQL的主从同步,当然不是依靠Docker的一键部署(虽然现在企业开发用的最多的是这种方式),所以本文就算是一个教程类文章吧,但在动手搭建之前,会先聊 ...

  2. zabbix3.0.4监控mysql主从同步

    zabbix3.0.4监控mysql主从同步 1.监控mysql主从同步原理: 执行一个命令 mysql -u zabbix -pzabbix -e 'show slave status\G' 我们在 ...

  3. MySQL主从同步几个文件

    MySQL主从同步:   M锁表 M导出S导入 M解锁 M建同步帐号 S获取点位:产生master.info S开启同步   3306: mysql-bin.0000x mysql-bin.index ...

  4. 监控mysql主从同步状态脚本

    监控mysql主从同步状态脚本 示例一: cat check_mysql_health #!/bin/sh slave_is=($(mysql -S /tmp/mysql3307.sock -uroo ...

  5. Linux下MySQL主从同步配置

    Centos6.5 MySQL主从同步 MySQL版本5.6.25 主服务器:centos6.5 IP:192.168.1.101 从服务器:centos6.5 IP:192.168.1.102 一. ...

  6. Mysql主从同步(复制)

    目录: mysql主从同步定义      主从同步机制 配置主从同步      配置主服务器      配置从服务器 使用主从同步来备份      使用mysqldump来备份      备份原始文件 ...

  7. MySQL主从同步原理 部署【转】

    一.主从的作用:1.可以当做一种备份方式2.用来实现读写分离,缓解一个数据库的压力二.MySQL主从备份原理master 上提供binlog ,slave 通过 I/O线程从 master拿取 bin ...

  8. shell脚本修复MySQL主从同步

    发布:thebaby   来源:net     [大 中 小] 分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧. 一个可以修改mysql主从同步的shell脚本. 例子 ...

  9. nagios 实现Mysql 主从同步状态的监控

    一.系统环境 主机名 IP nagios 192.168.15.111 mysql_s 192.168.15.21 二.操作步骤 2.1 mysql_s端的配置 2.1.1 编写check_mysql ...

随机推荐

  1. 文件IO(2)

    Lseek:       ***************************************************************************    实验一:     ...

  2. HihoCoder 1195 高斯消元·一(高斯消元)

    题意 https://hihocoder.com/problemset/problem/1195 思路 高斯消元是解决高元方程的一种算法,复杂度 \(O(n^3)\) . 过程大致是: 构造一个未知数 ...

  3. Linux---centos 配置网络

    Linux配置网络,有两种方式,一种是通过图像化的界面来配置网络IP,另一种方式是通过命令行来配置IP 1.第一种方式通过图形化的界面来配置IP 1.0修改之前的IP地址 1.1点击图片中的那个 网络 ...

  4. Cas单点登录配置数据查询用户

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. Jenkins-Linux环境搭建

    一.安装jdk.tomcat等 1. JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2 ...

  6. java多线程同步机制

    一.关键字: thread(线程).thread-safe(线程安全).intercurrent(并发的) synchronized(同步的).asynchronized(异步的). volatile ...

  7. Educational Codeforces Round 23 C. Really Big Numbers 暴力

    C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. SpringBoot配置Aop demo

    1. Demo部分 package com.example.demo.controller; import org.springframework.web.bind.annotation.Reques ...

  9. GCD LCM UVA - 11388

    代码很短理解不容易,在这说不清,大家代码里寻真相. 为什么二者相除就可以A了多找点数试试理解理解. #include<stdio.h> #define mod 1000000007 #de ...

  10. [osg]osg背景图设置

    转自:https://blog.csdn.net/qq_30754211/article/details/61190698 #include <osg/Geometry> #include ...