Mysql的ssl主從複製+半同步主從複製

Mysql的ssl主從複製+半同步主從複製 mysql

準備工做 sql

一、主從服務器時間同步 數據庫

[root@localhost ~]# crontab -e vim

*/30 * * * * /usr/sbin/ntpdate 172.16.0.1 &>/dev/null 服務器

 

 

MariaDB(10以上版本)的編譯安裝 網絡

部署配置 session

二、mysql說明 架構

(1) 主服務器 異步

hostname:master    IP:172.16.21.2 async

 

(1) 從服務器

hostname:master    IP:172.16.21.3

 

(3) 數據目錄

 

/mydata/data

 

(4) 二進制日誌目錄

/mydata/binlogs

 

(5) 中繼日誌目錄

/mydata/relaylogs

 

 

 

主庫配置

vi /etc/my.cnf

    server-id = 10 # 在複製架構中,需保持全局惟一

    log-bin = /mydata/binlogs/mysql-bin # 默認在數據目錄下

sync_binlog = 1 # 設置mariadb每次在提交事務前會將二進制日誌同步到磁盤,保證服務器崩潰時不會丟失事件

 

 

=====

service mysqld start # 啓動mariadb10

=====

mysql -hlocalhost -uroot -p # 登陸mysql

MariaDB [mysql]> grant replication slave,replication client on *.* to 'repluser'@'172.16.%.%' identified by 'replpass'; # 建立最小權限的複製帳號

MariaDB [mysql]> flush privileges;

MariaDB [mysql]> show master status; # 查看主庫的狀態信息

 

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 | 663 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

 

 

從庫配置

 

vi /etc/my.cnf

    server-id = 20 # 在複製架構中,需保持全局惟一

    log-bin = /mydata/binlogs/mysql-bin # 也可設置爲none,即關閉從庫的二進制日誌

    relay-log=/mydata/relaylogs/relay-bin # 設置中繼日誌文件

    log-slave-updates = 1 # 容許從庫將其重放的事件也記錄到自身的二進制日誌中

    read_only = 1 # 從庫設置爲只讀

=====

service mysqld start # 啓動mariadb10

=====

mysql -hlocalhost -uroot -p # 登陸mysql

MariaDB [mysql]> change master to master_host='172.16.21.2',master_user='repluser',master_password='replpass',master_log_file='master-bin.000002',master_log_pos=663; # 鏈接主庫

MariaDB [mysql]> start slave;

MariaDB [mysql]> show slave status\G 查看從庫狀態

 

驗證

 

# 在主庫上新建數據庫並建立數據

MariaDB [(none)]> create database test_for_replication;

MariaDB [(none)]> create table test_for_replication.user(id int not null primary key auto_increment,name char(20) not null,year int not null,classid int not null);

MariaDB [(none)]> insert into test_for_replication.user(name,year,classid) values('Jason Kk',23,2),('Hello Kitty',18,1);

# 查看從庫可否正常同步數據

MariaDB [(none)]> select * from test_for_replication.user; # 見下圖1

MariaDB [(none)]> show slave status\G # 見下圖2

圖 1:

 

 

圖 2 :

 

 

能夠看出主從同步正常,數據無誤!

 

3、SSL主從同步的實現

一、將master(172.16.7.202)作爲CA服務器

[root@localhost ~]# cd /etc/pki/CA/

[root@localhost CA]# ls

certs crl newcerts private

[root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)

Generating RSA private key, 2048 bit long modulus

.................................................................+++

......................................................................................+++

e is 65537 (0x10001)

 

 

[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 36500

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:HA

Locality Name (eg, city) [Default City]:ZZ

Organization Name (eg, company) [Default Company Ltd]:changsheng

Organizational Unit Name (eg, section) []:tech

Common Name (eg, your name or your server's hostname) []:changsheng

Email Address []:

 

 

[root@localhost CA]# touch index.txt serial crlnumber

[root@localhost CA]# echo 01 > serial

 

 

master(172.16.21.2)簽發證書

 

[root@localhost CA]# mkdir /usr/local/mysql/ssl

[root@localhost CA]# cd /usr/local/mysql/ssl

[root@localhost ssl]# (umask 077;openssl genrsa -out master.key 2048)

Generating RSA private key, 2048 bit long modulus

..................................+++

..........+++

e is 65537 (0x10001)

 

 

 

[root@localhost ssl]# openssl req -new -key master.key -out master.csr -days 36500

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:HA

Locality Name (eg, city) [Default City]:ZZ

Organization Name (eg, company) [Default Company Ltd]:changsheng

Organizational Unit Name (eg, section) []:tech

Common Name (eg, your name or your server's hostname) []:changsheng

Email Address []:

 

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

 

 

 

 

 

[root@localhost ssl]# openssl ca -in master.csr -out master.crt -days 36500

Using configuration from /etc/pki/tls/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

Serial Number: 1 (0x1)

Validity

Not Before: Jan 25 03:42:29 2015 GMT

Not After : Jan 1 03:42:29 2115 GMT

Subject:

countryName = CN

stateOrProvinceName = HA

organizationName = changsheng

organizationalUnitName = tech

commonName = changsheng

X509v3 extensions:

X509v3 Basic Constraints:

CA:FALSE

Netscape Comment:

OpenSSL Generated Certificate

X509v3 Subject Key Identifier:

5D:CB:5F:32:BB:24:6C:6F:4B:23:92:11:7D:FC:C1:9B:2B:57:50:E4

X509v3 Authority Key Identifier:

keyid:22:1F:2F:97:5B:70:84:F9:5C:BE:7E:7E:49:F3:CE:47:00:6D:19:61

 

Certificate is to be certified until Jan 1 03:42:29 2115 GMT (36500 days)

Sign the certificate? [y/n]:y

 

 

1 out of 1 certificate requests certified, commit? [y/n]y

Write out database with 1 new entries

Data Base Updated

 

 

 

Slave(172.16.21.3)生成證書申請請求

 

 

[root@localhost ~]# mkdir /usr/local/mysql/ssl

[root@localhost ~]# cd /usr/local/mysql/ssl

[root@localhost ssl]# ls

[root@localhost ssl]# (umask 077;openssl genrsa -out slave.key 2048)

Generating RSA private key, 2048 bit long modulus

..........+++

....................................+++

e is 65537 (0x10001)

[root@localhost ssl]# openssl req -new -key slave.key -out slave.csr -days 36500

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:HA

Locality Name (eg, city) [Default City]:ZZ

Organization Name (eg, company) [Default Company Ltd]:changsheng

Organizational Unit Name (eg, section) []:tech

Common Name (eg, your name or your server's hostname) []:changsheng

Email Address []:

 

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

 

[root@localhost ssl]# ls

slave.csr slave.key

[root@localhost ssl]# scp slave.csr root@172.16.21.2:/root

 

 

Master (172.16.21.2)向slave(172.16.7.250)簽發證書

 

在主節點(172.16.21.2):

 

---------------------------------------------------------------------------------------------------------------------------------

[root@localhost ~]# openssl ca -in slave.csr -out slave.crt -days 36500

Using configuration from /etc/pki/tls/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

Serial Number: 2 (0x2)

Validity

Not Before: Jan 25 03:51:18 2015 GMT

Not After : Jan 1 03:51:18 2115 GMT

Subject:

countryName = CN

stateOrProvinceName = HA

organizationName = changsheng

organizationalUnitName = tech

commonName = changsheng

X509v3 extensions:

X509v3 Basic Constraints:

CA:FALSE

Netscape Comment:

OpenSSL Generated Certificate

X509v3 Subject Key Identifier:

BF:B2:EB:07:56:20:17:07:D7:CB:47:44:07:A7:75:48:68:F1:CF:A1

X509v3 Authority Key Identifier:

keyid:22:1F:2F:97:5B:70:84:F9:5C:BE:7E:7E:49:F3:CE:47:00:6D:19:61

 

Certificate is to be certified until Jan 1 03:51:18 2115 GMT (36500 days)

Sign the certificate? [y/n]:y

 

 

1 out of 1 certificate requests certified, commit? [y/n]y

Write out database with 1 new entries

Data Base Updated

 

 

[root@localhost ~]# scp slave.crt root@172.16.21.3:/usr/local/mysql/ssl/

The authenticity of host '172.16.21.3 (172.16.21.3)' can't be established.

RSA key fingerprint is 4a:47:8c:1b:c9:52:74:38:80:23:05:e4:27:0a:60:d0.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.16.21.3' (RSA) to the list of known hosts.

root@172.16.21.3's password:

slave.crt

 

 

---------------------------------------------------------------------------------------------------------------------------------

 

 

master及slave提供CA的證書

 

仍是在master這個節點上(172.16.21.2):

--------------------------------------------------------------------------------------------------------

 

[root@localhost ~]# cp /etc/pki/CA/cacert.pem /usr/local/mysql/ssl/

[root@localhost ~]# cd /usr/local/mysql/ssl/

[root@localhost ssl]# ls

cacert.pem master.crt master.csr master.key

 

 

[root@localhost ~]# scp /etc/pki/CA/cacert.pem root@172.16.21.3:/usr/local/mysql/ssl/

root@172.16.21.3's password:

cacert.pem 100% 1306 1.3KB/s 00:00

[root@localhost ~]#

 

 

 

 

查看slave 節點(172.16.21.3)

 

[root@localhost ssl]# pwd

/usr/local/mysql/ssl

[root@localhost ssl]# ls

cacert.pem slave.crt slave.csr slave.key

[root@localhost ssl]#

 

---------------------------------------------------------------------------------------------------------------------------------

 

改master和slave的屬主、屬組爲"mysql"

 

Master(172.16.21.2)

 

[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/ssl/

[root@localhost ~]# ll /usr/local/mysql/ssl/

total 20

-rw-r--r-- 1 mysql mysql 1306 Jan 25 11:56 cacert.pem

-rw-r--r-- 1 mysql mysql 4431 Jan 25 11:42 master.crt

-rw-r--r-- 1 mysql mysql 993 Jan 25 11:41 master.csr

-rw------- 1 mysql mysql 1679 Jan 25 11:39 master.key

 

 

Slave(172.16.21.3)

 

[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/ssl/

[root@localhost ~]# ll /usr/local/mysql/ssl/

total 20

-rw-r--r-- 1 mysql mysql 1306 Jan 25 11:57 cacert.pem

-rw-r--r-- 1 mysql mysql 4432 Jan 25 11:52 slave.crt

-rw-r--r-- 1 mysql mysql 997 Jan 25 11:46 slave.csr

-rw------- 1 mysql mysql 1675 Jan 25 11:45 slave.key

 

 

 

 

 

 

改mysql配置文件開啓SSL加密功能

 

Master(172.16.21.2)

-----------------------------------------------------------------------------------------------

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

ssl

ssl_ca = /usr/local/mysql/ssl/cacert.pem

ssl_key = /usr/local/mysql/ssl/master.key

ssl_cert = /usr/local/mysql/ssl/master.crt

[root@localhost ~]## service mysqld restart

#

#

Slave(172.16.21.3)

--------------------------------------------------------------------------------------

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

ssl

ssl_ca = /usr/local/mysql/ssl/cacert.pem

ssl_key = /usr/local/mysql/ssl/slave.key

ssl_cert = /usr/local/mysql/ssl/slave.crt

[root@localhost ~]# service mysqld restart

 

 

 

 

 

Master(172.16.21.2)上驗證SSL加密功能開啓並建立基於密鑰認證用戶

[root@localhost ~]# mysql

MariaDB [(none)]> show variables like '%ssl%';

+---------------+---------------------------------+

| Variable_name | Value |

+---------------+---------------------------------+

| have_openssl | YES |

| have_ssl | YES |

| ssl_ca | /usr/local/mysql/ssl/cacert.pem |

| ssl_capath | |

| ssl_cert | /usr/local/mysql/ssl/slave.crt |

| ssl_cipher | |

| ssl_crl | |

| ssl_crlpath | |

| ssl_key | /usr/local/mysql/ssl/slave.key |

+---------------+---------------------------------+

9 rows in set (0.00 sec)

 

 

MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.%.%' identified by 'replpass' require ssl;

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

 

查看master(172.16.21.2)狀態信息

 

MariaDB [(none)]> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 | 681 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.01 sec)

 

 

 

 

 

 

 

驗證slave開啓SSL加密功能

 

[root@localhost ~]# mysql

MariaDB [(none)]> show variables like '%ssl%';

+---------------+---------------------------------+

| Variable_name | Value |

+---------------+---------------------------------+

| have_openssl | YES |

| have_ssl | YES |

| ssl_ca | /usr/local/mysql/ssl/cacert.pem |

| ssl_capath | |

| ssl_cert | /usr/local/mysql/ssl/slave.crt |

| ssl_cipher | |

| ssl_crl | |

| ssl_crlpath | |

| ssl_key | /usr/local/mysql/ssl/slave.key |

+---------------+---------------------------------+

9 rows in set (0.00 sec)

 

 

slave鏈接master

-------------------------------------------------------------------------------------------------------

MariaDB [(none)]> stop slave;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> change master to master_host='172.16.21.2',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=681,master_ssl=1,master_ssl_ca='/usr/local/mysql/ssl/cacert.pem',master_ssl_cert='/usr/local/mysql/ssl/slave.crt',master_ssl_key='/usr/local/mysql/ssl/slave.key';

 

Query OK, 0 rows affected (0.07 sec)

 

MariaDB [(none)]> start slave;

 

MariaDB [(none)]> show slave status\G # 查看從庫狀態

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.16.21.2

Master_User: repluser

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 681

Relay_Log_File: relay-bin.000002

Relay_Log_Pos: 535

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: 681

Relay_Log_Space: 826

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: Yes

Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem

Master_SSL_CA_Path:

Master_SSL_Cert: /usr/local/mysql/ssl/slave.crt

Master_SSL_Cipher:

Master_SSL_Key: /usr/local/mysql/ssl/slave.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: 10

Master_SSL_Crl: /usr/local/mysql/ssl/cacert.pem

Master_SSL_Crlpath:

Using_Gtid: No

Gtid_IO_Pos:

1 row in set (0.00 sec)

 

 

 

 

 

 

驗證:

 

# 主庫寫入:

MariaDB [test]> create table t1(name char(20) not null ,age int not null);

# 從庫讀取;

MariaDB [(none)]> show tables in test;

 

 

 

 

 

 

 

 

 

 

上面的操做是實現好了,到這裏異步的主從複製到這裏配置完成。下面咱們來講一下什麼是半同步複製(或說是同步也行)。

-------------------------------------------------------------------------------------------------------------

實驗第二部分

-------------------------------------------------------------------------------------------------------------------

 

Mysql 主從複製(半同步)

 

1.半同步複製

       在說明半同步複製以前咱們先來了解一下,什麼是同步複製?同步複製:同步複製能夠定義爲數據在同一時刻被提交到一臺或多臺機器,一般這是經過衆所周知的"兩階段提交"作到的。雖然這確實給你在多系統中保持一致性,但也因爲增長了額外的消息交換而形成性能降低。使用MyISAM或者InnoDB存儲引擎的MySQL自己並不支持同步複製,然而有些技術,例如分佈式複製塊設備(簡稱DRBD),能夠在下層的文件系統提供同步複製,容許第二個MySQL服務器在主服務器丟失的狀況下接管(使用第二服務器的複本)。瞭解了同步複製咱們正下面來講一下,什麼是半同步複製?

       MYSQL 5.5開始,支持半自動複製。以前版本的MySQL Replication都是異步(asynchronous)的,主庫在執行完一些事務後,是不會管備庫的進度的。若是備庫不幸落後,而更不幸的是主庫此時又出現Crash(例如宕機),這時備庫中的數據就是不完整的。簡而言之,在主庫發生故障的時候,咱們沒法使用備庫來繼續提供數據一致的服務了。Semisynchronous Replication(半同步複製)則必定程度上保證提交的事務已經傳給了至少一個備庫。Semi synchronous中,僅僅保證事務的已經傳遞到備庫上,可是並不確保已經在備庫上執行完成了。

       此外,還有一種狀況會致使主備數據不一致。在某個session中,主庫上提交一個事務後,會等待事務傳遞給至少一個備庫,若是在這個等待過程當中主庫Crash,那麼也可能備庫和主庫不一致,這是很致命的。若是主備網絡故障或者備庫掛了,主庫在事務提交後等待10秒(rpl_semi_sync_master_timeout的默認值)後,就會繼續。這時,主庫就會變回原來的異步狀態。

MySQL在加載並開啓Semi-sync插件後,每個事務需等待備庫接收日誌後才返回給客戶端。若是作的是小事務,兩臺主機的延遲又較小,則Semi-sync能夠實如今性能很小損失的狀況下的零數據丟失。

2.異步與半同步異同

       默認狀況下MySQL的複製是異步的,Master上全部的更新操做寫入Binlog以後並不確保全部的更新都被複制到Slave之上。異步操做雖然效率高,可是在Master/Slave出現問題的時候,存在很高數據不一樣步的風險,甚至可能丟失數據。

       MySQL5.5引入半同步複製功能的目的是爲了保證在master出問題的時候,至少有一臺Slave的數據是完整的。在超時的狀況下也能夠臨時轉入異步複製,保障業務的正常使用,直到一臺salve追遇上以後,繼續切換到半同步模式。

3.具體配置

注,mysql5.5半同步插件是由谷歌提供,具體位置/usr/local/mysql/lib/plugin/下,一個是master用的semisync_master.so,一個是slave用的semisync_slave.so,下面咱們就來具體配置一下。

Master(172.16.21.2)

--------------------------------------------------------------------------------------------------

(1).安裝插件

 

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; 

Query OK, 0 rows affected (0.39 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)

 

 

(2).修改配置文件

 

 

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

rpl_semi_sync_master_enabled=1 #啓用半同步

rpl_semi_sync_master_timeout=1000 #超時時間爲1s

 

 

 

 

(3).從新啓動服務

 

[root@localhost ~]# service mysqld restart

Shutting down MySQL... SUCCESS!  

Starting MySQL.. SUCCESS!

 

 

 

 

 

 

 

 

Slave(172.16.21.3):

 

(1).安裝插件

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; 

Query OK, 0 rows affected (0.38 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1; 

Query OK, 0 rows affected (0.00 sec)

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.00 sec)

mysql> START SLAVE IO_THREAD;

Query OK, 0 rows affected (0.01 sec)

 

 

 

 

 

 

(2).修改配置文件

 

 

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

rpl_semi_sync_slave_enabled=1  #啓用半同步複製

 

 

 

 

(3).從新啓動服務

 

[root@localhost ~]# service mysqld restart

 

 

 

4.查看一下狀態

 

 

Master(172.16.21.2):

 

 

mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';

 

+--------------------------------------------+-------+

 

| Variable_name | Value |

 

+--------------------------------------------+-------+

 

| Rpl_semi_sync_master_clients | 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 | 0 |

 

| Rpl_semi_sync_master_no_tx | 0 |

 

| 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.00 sec)

 

 

 

Slave(172.16.21.3):

 

mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';

+----------------------------+-------+ 

| Variable_name              | Value | 

+----------------------------+-------+ 

| Rpl_semi_sync_slave_status | ON    | 

+----------------------------+-------+ 

1 row in set (0.01 sec)

 

 

5.測試一下

 

 

Master(172.16.21.2):

 

mysql> create table user (id int(10));

Query OK, 0 rows affected (0.42 sec)

mysql> show tables;

+----------------+ 

| Tables_in_mydb | 

+----------------+ 

| user           | 

+----------------+ 

1 row in set (0.00 sec)

mysql> insert user value (1);

Query OK, 1 row affected (0.34 sec)

 

 

注,你們能夠看到建立一個表的插入一個數據的時間都很長,說明半同步配置完成。

 

 

 

 

 

6.模擬一下故障

 

 

 

 

 

 

Slave(172.16.21.3):

 

 

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.01 sec)

master:

mysql> create table user1 (id int(10));

Query OK, 0 rows affected (1.03 sec)

 

 

注,你們能夠看到主服務器會卡1s,咱們超時時間設置的爲1s

 

 

7.查看一下狀態

 

 

mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';

 

+--------------------------------------------+-------+

 

| Variable_name | Value |

 

+--------------------------------------------+-------+

 

| Rpl_semi_sync_master_clients | 1 |

 

| Rpl_semi_sync_master_net_avg_wait_time | 1560 |

 

| Rpl_semi_sync_master_net_wait_time | 10920 |

 

| Rpl_semi_sync_master_net_waits | 7 |

 

| Rpl_semi_sync_master_no_times | 1 |

 

| Rpl_semi_sync_master_no_tx | 1 |

 

| Rpl_semi_sync_master_status | OFF |

 

| Rpl_semi_sync_master_timefunc_failures | 0 |

 

| Rpl_semi_sync_master_tx_avg_wait_time | 985 |

 

| Rpl_semi_sync_master_tx_wait_time | 985 |

 

| 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 | 6 |

 

+--------------------------------------------+-------+

 

14 rows in set (0.00 sec)

 

mysql> STOP SLAVE IO_THREAD;

 

Query OK, 0 rows affected (0.01 sec)

 

mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%';

 

+----------------------------+-------+

 

| Variable_name | Value |

 

+----------------------------+-------+

 

| Rpl_semi_sync_slave_status | OFF |

 

+----------------------------+-------+

 

1 row in set (0.00 sec)

 

 

 

好了,到這裏咱們就配置完成了半同步複製。

 

 

 

 

 

 

 

注,在主-從架構上建議使用的配置

master:

    sync_binlog=1 # 馬上同步binlog

innodb_flush_logs_at_trx_commit=1 #馬上刷新innodb日誌

slave:

skip_slave_start=1 #設置開機不一樣步

read_only=1 #設置爲只讀

 

Mysql 複製過濾

    master:

        binlog-do-db=mydb

binlog-ignore-db=mysql

slave:

replicate_do_db

rpplicate_ignore_db

replicate_do_table

replicate_ignore_table

replicate_wild_do_table

replicate_wild_ignore_table

 

測試一下:

在從服務器上只複製testdb一個數據庫

 

slave:

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

replicate_do_db=testdb

replicate_do_db=mysql

[root@localhost ~]# service mysqld restart

master:

mysql> create database mydb1;

Query OK, 1 row affected (0.34 sec)

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mydb1              | 

| mysql              | 

| performance_schema | 

| test              

+--------------------+ 

6 rows in set (0.00 sec)

 

slave:

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mysql              | 

| performance_schema | 

| test              

+--------------------+ 

5 rows in set (0.00 sec)

注,你們能夠看到沒有同步mydb1,再測試一下。

master:

mysql> create database testdb;

Query OK, 1 row affected (0.01 sec)

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mydb1              | 

| mysql              | 

| performance_schema | 

| test              

| testdb             | 

+--------------------+ 

7 rows in set (0.00 sec)

slave:

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mysql              | 

| performance_schema | 

| test              

| testdb             | 

+--------------------+ 

6 rows in set (0.00 sec)

 

你們能夠看到同步了testdb

相關文章
相關標籤/搜索