标签:mysql复制
MySQL数据同步方式:
Synchronous Replication 同步复制
Asynchronous Replication 异步复制
Semisynchronous Replication 半同步复制
同步复制:指的是客户端连接到MySQL主服务器写入一段数据,MySQL主服务器同步给MySQL从服务器需要等待从服务器发出同步完成的响应才返回客户端OK, 这其中等待同步的过程是阻塞的, 如果有N台从服务器, 效率极低
异步复制: 指的是客户端连接到MySQL主服务器写入一段数据,MySQL主服务器将写入的数据发送给MySQL从服务器, 然后直接返回客户端OK, 可能从服务器的数据会和主服务不一致
半同步复制:指的是客户端连接到MySQL主服务器写入一段数据, MySQL主服务器只将数据同步复制给其中一台从服务器, 半同步复制给其他的从服务器, 来达到其中一台从服务器完全同步的效果
MySQL复制原理
建立主从关系后, 主服务器如果有数据修改之后, BINLOG会更新, 从服务通过IO-Thread读取主服务器的BINLOG到本地的Relay Log, 再通过SQL-Thread对其进行重放(replay), 从而同步到本地
MySQL简单复制
拓扑:
Master配置文件
[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=master-bin #开启二进制日志 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid !includedir /etc/my.cnf.d
Slave配置文件
[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=2 #从服务器ID relay_log=relay-log #开启中继日志 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid !includedir /etc/my.cnf.d
在主Master上面查看当前正在使用的二进制日志,并记录下position
MariaDB [(none)]> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000003 | 245 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
主Master上创建用户并赋予权限
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘cpuser‘@‘10.1.%.%‘ IDENTIFIED BY ‘ userpass‘; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
配置Slave
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.1.72.100‘,MASTER_USER=‘cpuser‘,MASTER_PASSWORD=‘userpass‘,MASTER_LOG_FILE=‘master-bin.000001‘,MASTER_LOG_POS=245; Query OK, 0 rows affected (0.03 sec) MariaDB [(none)]> SLAVE START; #启动IO和SQL Thread Query OK, 0 rows affected (0.02 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: master-bin.000001 Read_Master_Log_Pos: 245 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 530 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes #IO Thread已启动 Slave_SQL_Running: Yes #SQL Thread已启动 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: 245 Relay_Log_Space: 818 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)
测试:
在Master上创建一个数据库
Slave验证
主主复制配置
拓扑
互为主从:两个节点各自都要开启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的配置
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)
验证
标签:mysql复制
原文地址:http://zhaizhai.blog.51cto.com/11881047/1877414