MySQL 5.7.17主从复制实战(一主多从)

主从复制的原理:

mysql8主从复制搭建步骤 mysql 5.7 主从复制_数据库

分为同步复制和异步复制,实际复制架构中大部分为异步复制。 复制的基本过程如下:

1).Slave上面的IO进程连接上Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容;

2).Master接收到来自Slave的IO进程的请求后,通过负责复制的IO进程根据请求信息读取制定日志指定位置之后的日志信息,返回给Slave 的IO进程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息已经到Master端的bin-log文件的名称以及bin-log的位置;

3).Slave的IO进程接收到信息后,将接收到的日志内容依次添加到Slave端的relay-log文件的最末端,并将读取到的Master端的 bin-log的文件名和位置记录到master-info文件中,以便在下一次读取的时候能够清楚的告诉Master“我需要从某个bin-log的哪个位置开始往后的日志内容,请发给我”;

4).Slave的Sql进程检测到relay-log中新增加了内容后,会马上解析relay-log的内容成为在Master端真实执行时候的那些可执行的内容,并在自身执行。


一、环境准备

操作系统版本:centos 7.2

服务器架构:

Master(主)            ip:192.168.2.70  主机名称:node01    server_id:1  

Slave(从)             ip:192.168.2.71  主机名称:node02    server_id:2  

Slave(从)             ip:192.168.2.5   主机名称:node03    server_id:3  


其他准备:

每台服务器配置以下hosts

# vim /etc/hosts  

192.168.2.70  node01    

192.168.2.71  node02    

192.168.2.5   node03


系统时间同步:

#yum install chrony

配置时间同步源:# vi /etc/chrony.conf

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 0.rhel.pool.ntp.org iburst

server 1.rhel.pool.ntp.org iburst

# systemctl start chronyd.service  #启动

# chronyc sources -v  #同步时间源

关闭selinux

关闭firewalld防火墙

准备测试数据库apps.sql

解决依赖关系:yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison

先移除:rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

新建目录/mydata/data(建议使用lvm2镜像,实现物理备份),存放数据库数据

mysql 安装部署请参照http://daisywei.blog.51cto.com/7837970/1896614 (这里不再阐述)


二、SSH密钥登录方式设置

在node01节点192.168.2.70操作:



[root@node01 ~]# ssh-keygen -t rsa [root@node01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.71 [root@node01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.5





在node02节点192.168.2.71操作:



[root@node02 ~]# ssh-keygen -t rsa [root@node02 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.70 [root@node02 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.5





在node03节点192.168.2.5操作:



[root@node03 ~]# ssh-keygen -t rsa [root@node03 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.70 [root@node03 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.71





三、搭建主从复制架构

1、node01、node02、node03安装mysql,并初始化数据库(省略)

2、默认安装mysql有随机生成密码,node01、node02、node3节点上数据库设置管理员密码(测试密码:111111),同时创建复制使用的账号repl(密码111111)

#授权192.168.2.%网段主机root用户访问任意库,本地登录密码



mysql>  SET PASSWORD = PASSWORD('111111'); mysql> UPDATE user SET host='192.168.2.%' where user = 'root'; mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhsot' IDENTIFIED BY '111111'; mysql> GRANT ALL PRIVILEGES ON *.* TO  'root'@'192.168.2.%' IDENTIFIED BY '111111'; mysql> grant replication slave on *.* to 'repl'@'%' identified by '111111';  mysql> FLUSH PRIVILEGES;



3、node01(192.168.2.70)master配置mysql文件my.cnf(修改前建议备份该文件)修改如下内容,修改配置后重启数据库:



[mysqld] datadir = /mydata/data  #数据存放目录 socket = /tmp/mysql.sock #socket innodb_file_per_table = ON  #开启独立的表空间 skip_name_resolve = ON   #禁用dns解析 log_bin = mysql-bin  #开启日志(日志存储位置尽量不要同数据存储同一磁盘同一目录,这里测试方便不重新指向) relay_log = relay-bin #开启中继日志 (日志存储位置尽量不要同数据存储同一磁盘同一目录,这里测试方便不重新指向) binlog-format = row  #日志格式 log-slave-updates = true   # 配置从服务器的更新写入二进制日志 sever_id = 1              #server_id一定要唯一; [root@node01 ~]# systemctl restart mysqld.service #重启服务




注意:日志格式,binlog的格式也有三种:STATEMENT,ROW,MIXED。

mysql> show variables like 'log_%';   #查看日志是否开启


binlog-do-db 和 replicate-ignore-db 必须相同

log_bin、relay_log,二进制日志和中继日志尽量不要跟数据存储放在同一磁盘同一目录,防止硬盘损坏时日志也丢失


4、node01(192.168.2.70)master导入测试数据库apps.sql

mysql> CREATE DATABASE apps;
Query OK, 1 row affected (0.00 sec)
mysql> use apps;
Database changed
mysql> source /home/soft/apps.sql



5、node02(192.168.2.71)slave配置mysql文件my.cnf修改如下内容,修改配置后重启数据库:



[mysqld] datadir = /mydata/data   socket = /tmp/mysql.sock #socket innodb_file_per_table = ON   skip_name_resolve = ON    log_bin = mysql-bin   relay_log = relay-bin  binlog-format = row   log-slave-updates = true    sever_id = 2               #relay_log_purge=0           #禁止自动删除中继日志(slave配置文件多了下面这两条),如果是MHA开启此项 [root@node02 ~]# systemctl restart mysqld.service #重启服务 [root@node02 ~]#mysql -uroot -p111111 -e "set global read_only=1"  #从库只读,不建议写在配置文件中





6、node03(192.168.2.5)slave配置mysql文件my.cnf修改如下内容,修改配置后重启数据库:



[mysqld] datadir = /mydata/data   socket = /tmp/mysql.sock #socket innodb_file_per_table = ON   skip_name_resolve = ON    log_bin = mysql-bin   relay_log = relay-bin  binlog-format = row   log-slave-updates = true    sever_id = 3               #relay_log_purge=0           #禁止自动删除中继日志(slave配置文件多了下面这两条),如果是MHA开启此项 [root@node03 ~]# systemctl restart mysqld.service #重启服务 [root@node03 ~]#mysql -uroot -p111111 -e "set global read_only=1"  #从库只读,不建议写在配置文件中





7、在node01(192.168.2.70)Master上备份一份完整的数据:



[root@node01 /]# mysqldump -uroot -p111111 -h192.168.2.70 --master-data=2 --single-transaction -R --triggers -A > /home/soft/all.sql;




说明:

--master-data=2代表备份时刻记录master的Binlog位置和Position

--single-transaction意思是获取一致性快照

-R意思是备份存储过程和函数

--triggres的意思是备份触发器

-A代表备份所有的库

查看更多信息mysqldump --help


8、在node01(192.168.2.70)Master上创建复制用户repl(密码111111),并授权访问所有主机:



mysql> grant replication slave on *.* to 'repl'@'%' identified by '111111';  mysql> flush privileges; #刷新缓存




9、查看node01(192.168.2.70)Master主库备份时的binlog名称和位置,MASTER_LOG_FILE和MASTER_LOG_POS:



[root@node01 soft]# head -n 30 /home/soft/all.sql | grep 'CHANGE MASTER TO' -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;





10、把node01 Master主库备份all.sql复制到从库node01(192.168.2.71)和node3(192.168.2.5)/home/soft/目录下,并且导入到从库中



[root@node01 /]# scp /home/soft/all.sql root@192.168.2.71:/home/soft/   [root@node01 /]# scp /home/soft/all.sql root@192.168.2.5:/home/soft/





11、从库node02,node3导入all.sql,并设置复制参数



[root@node02 /]# mysql -uroot -p111111 -h192.168.2.71 < /home/soft/all.sql  [root@node03 /]# mysql -uroot -p111111 -h192.168.2.5 < /home/soft/all.sql




mysql> CREATE DATABASE apps;
mysql> use apps;
Database changed
mysql> source /home/soft/all.sql


node02和node03,连接mysql,执行以下命令:



[root@node02 /]# mysql -uroot -p111111 -h192.168.2.71




mysql> stop slave;  #暂停从库
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.70',MASTER_USER='repl', MASTER_PASSWORD='111111',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154; 
Query OK, 0 rows affected, 2 warnings (0.04 sec)


说明:MASTER_HOST #主库 ,MASTER_USER和MASTER_PASSWORD #复制账号密码  ,CHANGE MASTER TO #还原同步文件和日志

mysql> start slave; #启动复制
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G   #查看复制状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.70
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 521
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 4c9775f6-ef61-11e6-9973-5297c04d0733
             Master_Info_File: /mydata/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)




======================================================



[root@node03 /]# mysql -uroot -p111111 -h192.168.2.5




mysql> stop slave;  #暂停从库
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.70',MASTER_USER='repl', MASTER_PASSWORD='111111',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154; 
Query OK, 0 rows affected, 2 warnings (0.04 sec)
说明:MASTER_HOST #主库 ,MASTER_USER和MASTER_PASSWORD #复制账号密码  ,CHANGE MASTER TO #还原同步文件和日志
mysql> start slave; #启动复制
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G   #查看复制状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.70
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 521
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 4c9775f6-ef61-11e6-9973-5297c04d0733
             Master_Info_File: /mydata/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.01 sec)



12、验证复制是否正常!

############主库node01 Master插入一行数据,并查看Position号

mysql> INSERT INTO apps (app_name,url,country) VALUES ('BAIDU','http://www.baidu.com','CN');
Query OK, 1 row affected (0.00 sec)
mysql> show master status \G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 449
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)


#######从库node02 slave查看是否已经同步存在,Position号是否与主库master一致

mysql> SELECT * from apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP   | http://weibo.com/       | CN      |
|  3 | 淘宝 APP   | https://www.taobao.com/ | CN      |
|  4 | BAIDU      | http://www.baidu.com    | CN      |
+----+------------+-------------------------+---------+
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.70
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 449    #与主库Position一致
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 615
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 449
              Relay_Log_Space: 816
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 4c9775f6-ef61-11e6-9973-5297c04d0733
             Master_Info_File: /mydata/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)


########从库node03 slave查看是否已经同步存在,Position号是否与主库master一致

mysql> SELECT * from apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP   | http://weibo.com/       | CN      |
|  3 | 淘宝 APP   | https://www.taobao.com/ | CN      |
|  4 | BAIDU      | http://www.baidu.com    | CN      |
+----+------------+-------------------------+---------+
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.70
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 449   #与主库Position保持一致
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 615
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 449
              Relay_Log_Space: 816
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 4c9775f6-ef61-11e6-9973-5297c04d0733
             Master_Info_File: /mydata/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)



四、主从故障切换

1、确保所有主从数据库都开启二进制日志

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.01 sec)



2、确保切换时数据时从库都是最新先把主库node01设为只读:set global read_only=1;并且刷新一下主库log-bin日志

mysql>  show variables like 'read_only';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only     | ON    |
+---------------+-------+
1 row in set (0.00 sec)
mysql>  flush logs;
Query OK, 0 rows affected (0.02 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)



注意:这里的file已经是mysql-bin.000002不是mysql-bin.000001了,Position号是154,不是上面的449了!!!!!


3、确认从库node02,node03的file和pos是否与主库一致。

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.70
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002     #注意这里与主库是否一致
          Read_Master_Log_Pos: 154                  #注意这里与主库是否一致
               Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes



4、以上确认主库node01没有数据更新后,提升node02(192.168.2.71)为主库,node01变为从库

1)node02暂停从库(注意这里也可以暂停node03从库SLAVE,让它依然同步node01)

mysql> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)


2)更改node02为主库master

mysql> RESET MASTER;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


3)关闭新主库node02的只读属性,并创建一个test01数据库(为后面新链接的从库node01和node03是否正常同步)

mysql> SET GLOBAL read_only=0;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE test01;
Query OK, 1 row affected (0.00 sec)


这时新主库的Position为以下:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      319 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4)设置node01(原主库)身份为slave,并且更改链接新主库node02信息(注:MASTER_LOG_POS这时应该是老的154,不是创建新test01库后的319),并启动SLAVE

mysql> RESET SLAVE;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.71',MASTER_USER='repl', MASTER_PASSWORD='111111',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154; 
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> START SLAVE;
mysql> show slave status\G;  #查看从库状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.71
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 319
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 485
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
…………
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| apps               |
| mysql              |
| performance_schema |
| sys                |
| test01             |        #test01数据也已经过来了
+--------------------+
6 rows in set (0.00 sec)



注意:这时查看node03从库,Master_Host依然是node01(192.168.2.70),但测试验证的数据库test01也依然同步过来了,如果需要更改链接新主库node02,详见下面操作!!

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.70
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 319   #与新主库node02的Position号也保持一致
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
…………
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| apps               |
| mysql              |
| performance_schema |
| sys                |
| test01             |    #新创建的test01库也同步过来
+--------------------+
6 rows in set (0.00 sec)




**********注意:修改node03从库SLAVE身份,链接新主库node02的信息(注:MASTER_LOG_POS也依然老的154),修改并重新启动SLAVE*********

mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.71',MASTER_USER='repl', MASTER_PASSWORD='111111',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.71
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 319   #与新主库node02的Position号也保持一致
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
…………
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| apps               |
| mysql              |
| performance_schema |
| sys                |
| test01             |    #新创建的test01库也同步过来
+--------------------+
6 rows in set (0.00 sec)



5、再次验证数据,在node02(192.168.2.71)新主库的apps库apps表中,再插入一条新记录,查看是否同步至其他从库

node02主库上执行以下命令:

mysql> USE apps;
Database changed
mysql> INSERT INTO apps (app_name,url,country) VALUES ('XINLANG','http://www.sina.com.cn','CN');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP   | http://weibo.com/       | CN      |
|  3 | 淘宝 APP   | https://www.taobao.com/ | CN      |
|  4 | BAIDU      | http://www.baidu.com    | CN      |
|  5 | XINLANG    | http://www.sina.com.cn  | CN      |
+----+------------+-------------------------+---------+
5 rows in set (0.00 sec)

查看从库node01、node03节点,apps表是否也成功插入数据,得到如下结果:

mysql>  SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP   | http://weibo.com/       | CN      |
|  3 | 淘宝 APP   | https://www.taobao.com/ | CN      |
|  4 | BAIDU      | http://www.baidu.com    | CN      |
|  5 | XINLANG    | http://www.sina.com.cn  | CN      |
+----+------------+-------------------------+---------+
5 rows in set (0.00 sec)




转载于:https://blog.51cto.com/daisywei/1896822