手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL数据同步原理及实现

[日期:2017-03-28] 来源:Linux社区  作者:zhaizhai [字体: ]

主主复制配置
拓扑

互为主从:两个节点各自都要开启binlog和relay log

配置文件
[MySQLd]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
 
symbolic-links=0
 
skip_name_resolve = ON
innodb_file_per_table = ON
 
server_id=1          #服务器ID
log_bin=mysql-bin    #开启二进制日志
relay_log=relay-log  #开启中继日志
 
auto_increment_offset=1
auto_increment_increment=2
 
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
 
!includedir /etc/my.cnf.d

都授权有复制全的用户账号,并把对方指定为主节点

Master1的配置
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.72.200' IDENTIFIED BY 'userpass';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      506 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
 
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.200',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=506;
Query OK, 0 rows affected (0.03 sec)

Master2的配置
12345678910111213141516 MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.72.100' IDENTIFIED BY 'userpass';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.100',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=506;
Query OK, 0 rows affected (0.04 sec)
 
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      506 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

在两个节点上同时启动IO和SQL Thread
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.72.100
                  Master_User: cpuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 506
              Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
            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: 506
              Relay_Log_Space: 817
              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
1 row in set (0.00 sec)

验证
在Master1上创建数据库test1,可同步至Master2上,相反在Master2上创建test2也可同步至Master1

半同步复制配置

拓扑

半同步的插件在/usr/lib64/mysql/plugin下, master用的semisync_master.so,slave用的semisync_slave.so

配置Master

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_timeout=2000;
Query OK, 0 rows affected (0.00 sec)

配置Slave
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.03 sec)
 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.02 sec)
 
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

验证

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-03/142272.htm

linux
相关资讯       MySQL数据同步 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款