MySQL主從複製--MySQL5.5異步、半同步配置以及複製過濾實現

大綱

一、主從複製複製原理

二、主從複製的作用及複製類型

三、MySQL5.5異步複製的實現

四、MySQL5.5半同步複製的實現

五、MySQL複製過濾功能的實現



一、主從複製原理

wKioL1aYkvPwbq1LAABUrjWR9Ok816.gif

工作原理

1、當Master接收到了一個寫請求,處理寫請求,將結果保存至磁盤中,並且會將此操作記錄到二進制日誌文件中

2、Slave會從Master的二進制日誌中讀取其中的事件保存至本地的中繼日誌中

3、Slave會啓動一個線程來逐條讀取中繼日誌中的事件並應用於本地的


二、主從複製的作用及複製類型

複製的作用

  • 輔助實現備份

  • 提供類似高可用的機制

  • 異地容災

  • scale out:分攤負載,如實現讀寫分離

複製類型

  • 基於語句(statement)複製優點是基於語句的複製的二進制日誌可以很好的進行壓縮,而且日誌的數據量也較小,缺點是基於語句的複製必須是串行化。

  • 基於行復制(Row-Based Replication),在二進制日誌中記錄下實際數據的改變,優點就是可以對任何語句都能正確工作,一些語句的效率更高,缺點就是二進制日誌會很大,不能使用mysqlbinlog來查看二進制日誌.

  • 混合類型(mixed)的複製,默認採用基於語句的複製,一旦發現基於語句的無法精確的複製時,就會採用基於行的複製.


三、MySQL5.5異步複製的實現

系統環境

CentOS5.8 x86_64

軟件包

mysql-5.5.28-linux2.6-x86_64.tar.gz(二進制安裝包)


配置主從複製的基本步驟

  • master:啓用二進制日誌、惟一server-id、具有複製權限的用戶(REQUIRE SSL)

  • slave:啓用中繼日誌、惟一server-id、連接主服務器並啓動複製線程(IO_THREAD, SQL_THREAD)



1、Master安裝並配置MySQL

數據目錄底層最好是個邏輯卷,我這裏就不再演示邏輯卷的創建了,我的博文中有,此處沒有使用邏輯卷

準備數據目錄並添加mysql用戶
[root@Master ~]# mkdir -pv /mydata/data
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[root@Master ~]# groupadd -g 3306 mysql
[root@Master ~]# useradd -u 3306 -g mysql -M -s /sbin/nologin -d /mydata/data/ mysql
[root@Master ~]# chown -R mysql.mysql /mydata/data/

解壓並初始化mysql
[root@Master ~]# cd /tmp/
[root@Master tmp]# tar xf mysql-5.5.28-linux2.6-x86_64.tar.gz  -C /usr/local/
[root@Master tmp]# cd /usr/local/
[root@Master local]# ln -sv mysql-5.5.28-linux2.6-x86_64 mysql
create symbolic link `mysql' to `mysql-5.5.28-linux2.6-x86_64'
[root@Master local]# cd mysql
[root@Master mysql]# chown -R root.mysql ./*
[root@Master mysql]# ll
total 132
drwxr-xr-x  2 root mysql  4096 Jan 15 15:18 bin
-rw-r--r--  1 root mysql 17987 Aug 29  2012 COPYING
drwxr-xr-x  4 root mysql  4096 Jan 10 12:01 data
drwxr-xr-x  2 root mysql  4096 Jan 15 15:17 docs
drwxr-xr-x  3 root mysql  4096 Jan 15 15:18 include
-rw-r--r--  1 root mysql  7604 Aug 29  2012 INSTALL-BINARY
drwxr-xr-x  3 root mysql  4096 Jan 15 15:18 lib
drwxr-xr-x  4 root mysql  4096 Jan 10 12:02 man
drwxr-xr-x 10 root mysql  4096 Jan 15 15:18 mysql-test
-rw-r--r--  1 root mysql  2552 Aug 29  2012 README
drwxr-xr-x  2 root mysql  4096 Jan 15 15:18 scripts
drwxr-xr-x 27 root mysql  4096 Jan 15 15:18 share
drwxr-xr-x  4 root mysql  4096 Jan 15 15:18 sql-bench
drwxr-xr-x  2 root mysql  4096 Jan 15 15:18 support-files

[root@Master mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h Master password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!

複製配置文件與服務啓動腳本
[root@Master mysql]# cp support-files/my-large.cnf /etc/my.cnf 
[root@Master mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@Master mysql]# chkconfig --add mysqld

修改配置文件增加或修改如下行
[root@Master mysql]# vim /etc/my.cnf 
[mysqld]
datadir = /mydata/data
innodb_file_per_table = 1
log-bin=master-bin
log-bin-index=master-bin.index

增加PATH環境變量
[root@Master mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@Master mysql]# . /etc/profile.d/mysql.sh

啓動服務並創建複製用戶
[root@Master mysql]# service mysqld start
Starting MySQL....                                         [  OK  ]
[root@Master mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'172.16.%.%' IDENTIFIED BY 'repuser';
Query OK, 0 rows affected (0.34 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> \q
Bye

將mysql的二進制包和配置文件拷貝至從節點
[root@Master mysql]# scp  /tmp/mysql-5.5.28-linux2.6-x86_64.tar.gz Slave:/tmp
mysql-5.5.28-linux2.6-x86_64.tar.gz                       100%  178MB   7.4MB/s   00:24
[root@Master mysql]# scp /etc/my.cnf Slave:/etc/
my.cnf                                          100% 4745     4.6KB/s   00:00

2、Slave安裝並配置MySQL

準備數據目錄並添加mysql用戶
[root@Slave ~]# mkdir -pv /mydata/data
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[root@Slave ~]# groupadd -g 3306 mysql
[root@Slave ~]# useradd -u 3306 -g mysql -M -s /sbin/nologin -d /mydata/data/ mysql
[root@Slave ~]# chown -R mysql.mysql /mydata/data/

解壓並初始化mysql
[root@Slave ~]# cd /tmp/
[root@Slave tmp]# tar xf mysql-5.5.28-linux2.6-x86_64.tar.gz  -C /usr/local/
[root@Slave tmp]# cd /usr/local/
[root@Slave local]# ln -sv mysql-5.5.28-linux2.6-x86_64 mysql
create symbolic link `mysql' to `mysql-5.5.28-linux2.6-x86_64'
[root@Slave local]# cd mysql
[root@Slave mysql]# chown -R root.mysql ./*
[root@Slave mysql]# ll
total 132
drwxr-xr-x  2 root mysql  4096 Jan 15 15:45 bin
-rw-r--r--  1 root mysql 17987 Aug 29  2012 COPYING
drwxr-xr-x  4 root mysql  4096 Jan 10 11:15 data
drwxr-xr-x  2 root mysql  4096 Jan 15 15:45 docs
drwxr-xr-x  3 root mysql  4096 Jan 15 15:46 include
-rw-r--r--  1 root mysql  7604 Aug 29  2012 INSTALL-BINARY
drwxr-xr-x  3 root mysql  4096 Jan 15 15:46 lib
drwxr-xr-x  4 root mysql  4096 Jan 10 11:16 man
drwxr-xr-x 10 root mysql  4096 Jan 15 15:46 mysql-test
-rw-r--r--  1 root mysql  2552 Aug 29  2012 README
drwxr-xr-x  2 root mysql  4096 Jan 15 15:46 scripts
drwxr-xr-x 27 root mysql  4096 Jan 15 15:46 share
drwxr-xr-x  4 root mysql  4096 Jan 15 15:46 sql-bench
drwxr-xr-x  2 root mysql  4096 Jan 15 15:46 support-files

[root@Slave mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h Slave password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!

複製服務啓動腳本
[root@Slave mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@Slave mysql]# chkconfig --add mysqld

修改配置文件做如下修改
[root@Slave mysql]# vim /etc/my.cnf 
[mysqld]
這兩行要註釋掉,從節點不開啓二進制日誌功能
#log-bin=Slave-bin                    
#log-bin-index=Slave-bin.index

修改server_id,不能與主節點重複,要全局唯一
server-id       = 11

增加中繼日誌功能
relay-log = relay-log
relay-log-index = relay-log.index

使從節點只讀,不過此參數對具有SUPER權限的用戶無效
read-only = ON

增加PATH環境變量並使之生效
[root@Slave mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@Slave mysql]# . /etc/profile.d/mysql.sh

啓動服務
[root@Slave ~]# service mysqld start
Starting MySQL....                                         [  OK  ]

3、啓動Slave上的複製功能

首次在Master上查看當前二進制日誌所處的position
[root@Master mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.28-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

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

mysql> SHOW BINLOG EVENTS IN 'master-bin.000001';
+-------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
| Log_name          | Pos | Event_type  | Server_id | End_log_pos | Info                                                                             |
+-------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
| master-bin.000001 |   4 | Format_desc |         1 |         107 | Server ver: 5.5.28-log, Binlog ver: 4                                            | 
| master-bin.000001 | 107 | Query       |         1 |         262 | GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'172.16.%.%' IDENTIFIED BY 'repuser' | 
| master-bin.000001 | 262 | Query       |         1 |         337 | FLUSH PRIVILEGES                                                                 | 
+-------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------+
3 rows in set (0.02 sec)

mysql> \q
Bye

再在Slave上啓動複製功能
[root@Slave mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.28 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CHANGE MASTER TO MASTER_HOST='172.16.1.101',MASTER_PORT=3306,MASTER_USER='repuser',MASTER_PASSWORD='repuser',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=337;
Query OK, 0 rows affected (0.18 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.16.1.101
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 337
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 337
              Relay_Log_Space: 107
              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: NULL
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: 0
1 row in set (0.00 sec)

mysql> START SLAVE;
Query OK, 0 rows affected (0.49 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.101
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 337
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-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: 337
              Relay_Log_Space: 404
              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.04 sec)

4、測試複製功能

首先在主節點上創建一個測試庫
[root@Master mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.28-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| performance_schema | 
| test               | 
+--------------------+
4 rows in set (0.28 sec)

mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.10 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mydb               | 
| mysql              | 
| performance_schema | 
| test               | 
+--------------------+
5 rows in set (0.01 sec)

再在從節點上查看是否有此庫
[root@Slave mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.28 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.101
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 420                        # 可以看到已經讀取到420位置了
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 337
        Relay_Master_Log_File: master-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: 420                        # 同樣執行到了420位置
              Relay_Log_Space: 487
              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.01 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mydb               | 
| mysql              | 
| performance_schema | 
| test               | 
+--------------------+
5 rows in set (0.00 sec)
可以看到主從複製效果已然實現

查看從節點的錯誤日誌,發現複製相關的日誌信息也會記錄到錯誤日誌中
[root@Slave mysql]# tail /mydata/data/Slave.err 
160115 15:55:06 InnoDB: 1.1.8 started; log sequence number 0
160115 15:55:07 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
160115 15:55:07 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
160115 15:55:07 [Note] Server socket created on IP: '0.0.0.0'.
160115 15:55:08 [Note] Event Scheduler: Loaded 0 events
160115 15:55:08 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.5.28'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
160115 16:05:15 [Note] 'CHANGE MASTER TO executed'. Previous state master_host='', master_port='3306', master_log_file='', master_log_pos='4'. New state master_host='172.16.1.101', master_port='3306', master_log_file='master-bin.000001', master_log_pos='337'.
160115 16:10:46 [Note] Slave I/O thread: connected to master '[email protected]:3306',replication started in log 'master-bin.000001' at position 337
160115 16:10:46 [Note] Slave SQL thread initialized, starting replication in log 'master-bin.000001' at position 337, relay log './relay-log.000001' position: 4

四、MySQL5.5半同步複製的實現

1、設置Master,安裝插件並啓用

[root@Master ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';  
Query OK, 0 rows affected (0.10 sec)

mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;  
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;  
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye

將兩個服務器變量寫入配置文件中
[root@Master ~]# vim /etc/my.cnf
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000

查看從服務器上的semi_sync是否開啓
[root@Master ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.28-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |     # 值爲1證明主從半同步複製連接成功
| Rpl_semi_sync_master_net_avg_wait_time     | 0     | 
| Rpl_semi_sync_master_net_wait_time         | 0     | 
| Rpl_semi_sync_master_net_waits             | 0     | 
| Rpl_semi_sync_master_no_times              | 1     | 
| Rpl_semi_sync_master_no_tx                 | 1     | 
| Rpl_semi_sync_master_status                | ON    | 
| Rpl_semi_sync_master_timefunc_failures     | 0     | 
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     | 
| Rpl_semi_sync_master_tx_wait_time          | 0     | 
| Rpl_semi_sync_master_tx_waits              | 0     | 
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     | 
| Rpl_semi_sync_master_wait_sessions         | 0     | 
| Rpl_semi_sync_master_yes_tx                | 0     | 
+--------------------------------------------+-------+
14 rows in set (0.07 sec)

2、設置Slave,安裝插件並啓用

[root@Slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.28 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; 
Query OK, 0 rows affected (0.06 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1; 
Query OK, 0 rows affected (0.00 sec)

mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.03 sec)

mysql> \q
Bye

將上面那個服務器變量寫入配置文件中
[root@Slave ~]# vim /etc/my.cnf
[mysqld]
rpl_semi_sync_slave_enabled=1

查看從服務器上的semi_sync是否開啓
[root@Slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.28 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    | 
+----------------------------+-------+
1 row in set (0.00 sec)

3、測試半同步效果

[root@Master ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.28-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     | 
| Rpl_semi_sync_master_net_avg_wait_time     | 15488 | 
| Rpl_semi_sync_master_net_wait_time         | 15488 | 
| Rpl_semi_sync_master_net_waits             | 1     | 
| Rpl_semi_sync_master_no_times              | 1     | 
| Rpl_semi_sync_master_no_tx                 | 1     | 
| Rpl_semi_sync_master_status                | ON    | 
| Rpl_semi_sync_master_timefunc_failures     | 0     | 
| Rpl_semi_sync_master_tx_avg_wait_time      | 23055 | 
| Rpl_semi_sync_master_tx_wait_time          | 23055 | 
| Rpl_semi_sync_master_tx_waits              | 1     | 
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     | 
| Rpl_semi_sync_master_wait_sessions         | 0     | 
| Rpl_semi_sync_master_yes_tx                | 1     | 
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

mysql> CREATE DATABASE testdb;
Query OK, 1 row affected (0.21 sec)            # 注意這個時間

此時關掉從節點的IO_Thread
[root@Slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.28 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

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

再來創建個hellodb庫
mysql> CREATE DATABASE hellodb;
Query OK, 1 row affected (1.01 sec)            # 我們設置的超時時長爲1s

再啓用Slave的IO_Thread
mysql> START SLAVE IO_Thread;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye

五、MySQL複製過濾功能的實現

建議在從服務器上設置,因爲在主服務器上設置replicate_do_db或replicate_ignore_db時,
任何不涉及到數據庫相關的寫操作都不會被記錄到二進制日誌當中,
那麼會造成主服務器上的二進制日誌不完整,
一旦將來數據庫崩潰,不能做及時點還原,所以建議在從服務器上設置,儘管會浪費大量網絡IO和磁盤IO

要只想複製某個庫,只需在從服務器的my.cnf配置文件中[mysqld]段中加一行replicate_do_db = db_name即可;
若只想複製某張表,只需在從服務器的my.cnf配置文件中[mysqld]段中加一行replicate_do_db = tb_name即可;
若想使用通配符,則使用replicate_wild_do_table = db[%_],%表示任意長度任意字符,_表示任意單個字符;

從服務器設置完成,重啓mysql服務器,然後進入交互式模式,執行mysql> SHOW SLAVE STATUS\G
查看Replicate_Do_DB這一字段,即可驗證配置是否生效,具體操作過程如下

首先編輯從服務器的配置文件
[root@Slave ~]# vim /etc/my.cnf 
[mysqld]
replicate-do-db = mydb            # 表示只複製這一個庫,如想複製多個,此指令可以使用多次

重啓Slave服務器
[root@Slave ~]# service mysqld restart
Shutting down MySQL...                                     [  OK  ]
Starting MySQL...                                          [  OK  ]

然後連接Slave,查看狀態信息
[root@Slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.28 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.101
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 358
               Relay_Log_File: relay-log.000009
                Relay_Log_Pos: 254
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: mydb                        # 可以看到指令生效了
          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: 358
              Relay_Log_Space: 404
              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)

接下來我們在主服務器上創建三個庫,測試一下
[root@Master ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.28-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| performance_schema | 
| test               | 
+--------------------+
4 rows in set (0.00 sec)

mysql> CREATE DATABASE hellodb;
Query OK, 1 row affected (0.01 sec)

mysql> CREATE DATABASE testdb;
Query OK, 1 row affected (0.01 sec)

mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)

再在從服務器上查看是否有這三個庫
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |     
| mydb               |                     # 可以看到只有mydb,因爲我們定義了只複製mydb庫
| mysql              | 
| performance_schema | 
| test               | 
+--------------------+
5 rows in set (0.00 sec)
如此即可實現mysql的複製過濾功能









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章