一、mysql的主從復(fù)制過程:
master中的dump進(jìn)程將二進(jìn)制文件讀出,具有此服務(wù)器中replication client 和replication slave權(quán)限的從服務(wù)器的I/O 線程
讀入主服務(wù)器的二進(jìn)制文件并記錄到relay-log中,從服務(wù)器的sql線程按照my.cnf中定義的規(guī)則,去讀取relay-log,并更新到數(shù)據(jù)庫(kù)中-
由上述過程可知,master維護(hù)bin-log ,slave維護(hù)relay-log 從而實(shí)現(xiàn)主從復(fù)制
主從實(shí)現(xiàn):
主服務(wù)器中的配置 (172.16.21.1)
#vim my.cnf
[mysqld]
server-id=1
sync_binlog=1 //當(dāng)執(zhí)行事務(wù)時(shí),將產(chǎn)生的數(shù)據(jù)和DDL立即同步到binlog中
innodb_flush_logs_at_trx_commit=1
#service mysqld restart
登錄數(shù)據(jù)庫(kù)并添加用戶,此用戶具有replication client 和replication slave 的權(quán)限
mysql>grant replication client,replication slave on *.* to repl@'172.16.%.%' identifided by '123456';
mysql>show grants for repl@'172.16.%.%';
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 11404543 | | |
+------------------+----------+--------------+------------------+
從服務(wù)器中的配置 (172.16.21.2)
vim my.cnf
[mysqld]
server-id=11
skip_slave_start=1
read_only=1
#bin-log=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin.index
登錄數(shù)據(jù)庫(kù),將主服務(wù)器指向172.16.21.1 用戶是repl 密碼為123456 與上面的主服務(wù)器設(shè)置相對(duì)應(yīng)
mysql>change master to master_user='repl',master_host='172.16.21.1',master_bin_log='mysql-bin.000001';
mysql>start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.21.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 11404543
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 11404689
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: 11404543
Relay_Log_Space: 11404839
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)
如果主從服務(wù)器都是第一次搭建,且沒有數(shù)據(jù)存入時(shí),在開啟slave出錯(cuò)時(shí)的解決方法:
在主服務(wù)器中的數(shù)據(jù)庫(kù)執(zhí)行
mysql>flush master; //滾動(dòng)二進(jìn)制日志
在從服務(wù)器中執(zhí)行
mysql>flush slave; //滾動(dòng)中繼日志
二、主主復(fù)制:
主1:(172.16.21.2)
如果是第一次開啟:
開啟二進(jìn)制日志 和中繼日志功能
[mysqld]
server-id=1
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=realy-bin.index
>grant replication client,replication slave on *.* to repl@'172.16.%.%'
>change MASTER TO MASTER_HOST='172.16.21.1',MASTER_USER='repl',MASTER_PASSWORD='123456'
MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=811 //這里的MASTER_LOG_FILE 和MASTER_LOG_POS是主服務(wù)器的
show master status;信息
>start slave;
>show slave status;
主2:(172.16.21.1)
如果是第一次開啟:
開啟二進(jìn)制日志 和中繼日志功能
[mysqld]
server-id=1
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin.index
>grant replication client,replication slave on *.* to repl@'172.16.%.%'
>change MASTER TO MASTER_HOST='172.16.21.2',MASTER_USER='repl',MASTER_PASSWORD='123456'
MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=811 //這里的MASTER_LOG_FILE 和MASTER_LOG_POS是主服務(wù)器的
show master status;信息
>start slave;
>show slave status;
三、設(shè)置半同步:
mysql的主從復(fù)制是基于異步實(shí)現(xiàn)的,可以通過在master端安裝 semisync_master.so 插件
在slave端安裝 semisync_slave.so 插件來實(shí)現(xiàn)半同步,這里所謂的半同步是當(dāng)一主多從時(shí),
主給一個(gè)從實(shí)現(xiàn)同步,當(dāng)此過程超過時(shí)間限定后(通過rpl_semi_sync_master_timeout 來設(shè)置),
則降級(jí)為異步。
設(shè)置方法:
主服務(wù)器(172.16.21.1)
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
從服務(wù)器(172.16.21.2)
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
mysql> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;
// 重啟從服務(wù)器的IO線程 以實(shí)現(xiàn)插件功能
查看從服務(wù)器是否開啟semi_sync
mysql> show global status like 'rpl_semi%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.04 sec)
查看主服務(wù)器是否開啟semi_sync
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.01 sec)
上述命令中的set后的參數(shù)可以寫在各自服務(wù)器中的my.cnf [mysqld] 下,以實(shí)現(xiàn)永久生效
取消加載插件 可使用命令;
mysql> UNINSTALL PLUGIN rpl_semi_sync_master;
四、基于ssl的主從復(fù)制
主從復(fù)制是數(shù)據(jù)在網(wǎng)絡(luò)中是明文傳輸 所以設(shè)置主從服務(wù)基于ssl的復(fù)制就顯得必要 這里有官方文檔,也可使用下列相關(guān)配置
官方文檔地址: http://dev.mysql.com/doc/refman/5.1/en/replication-solutions-ssl.html
以下配置需要在主從復(fù)制建立的基礎(chǔ)上進(jìn)行,所以只添加ssl的配置選項(xiàng),其他選項(xiàng)可參照主從復(fù)制配置
首先分別為主從申請(qǐng)證書(證書申請(qǐng)相關(guān)步驟略過)
master(172.16.21.1):
CA證書:/etc/pki/CA/cacert.pem
私鑰:/usr/local/mysql/ssl/master.key
服務(wù)器證書:/usr/local/mysql/ssl/master.crt
vim my.cnf
[mysqld]
ssl-ca=/etc/pki/CA/cacert.pem
ssl-cert=/usr/local/mysql/ssl/master.crt
ssl-key=/usr/local/mysql/ssl/master.key
#serivce mysqld restart
slave (172.16.21.2):
CA證書:/etc/pki/CA/cacert.pem
私鑰:/usr/local/mysql/ssl/slave.key
服務(wù)器證書:/usr/local/mysql/ssl/slave.crt
vim my.cnf
[client]
ssl-ca=/etc/pki/CA/cacert.pem
ssl-cert=/usr/local/mysql/ssl/slave.crt
ssl-key=/usr/local/mysql/ssl/slave.key
service mysqld restart //因?yàn)橐膍aster的相關(guān)選項(xiàng),所以重啟mysql不需要立即啟動(dòng)slave,可以在配置文件中定義skip_slave_start=1來實(shí)現(xiàn)之
登錄從服務(wù)器mysql 更改master的相關(guān)選項(xiàng),以實(shí)現(xiàn)ssl功能
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.16.21.1',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='123456',
-> MASTER_SSL=1,
-> MASTER_SSL_CA = '/etc/pki/CA/cacert.pem',
-> MASTER_SSL_CAPATH = '/etc/pki/CA',
-> MASTER_SSL_CERT = '/usr/local/mysql/ssl/master.crt',
-> MASTER_SSL_KEY = '/usr/local/mysql/ssl/master.key';
mysql>start slave;
mysql>show slave status;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Master_SSL_Allowed: Yes
Master_SSL_CA_File: /etc/pki/CA/cacert.pem
Master_SSL_CA_Path: /etc/pki/CA
Master_SSL_Cert: /usr/local/mysql/ssl/slave.crt
Master_SSL_Cipher:
Master_SSL_Key: /usr/local/mysql/slave.key
至此基于ssl功能實(shí)現(xiàn) 本文出自:億恩科技【mszdt.com】
服務(wù)器租用/服務(wù)器托管中國(guó)五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|