Mysql雙主配置

mysql 5.6 bin-log雙主配置


環境:
master1  192.168.169.101
端口  3307
備註:由於主機上安裝了多個實例,採用mysqld_multi管理(該部分與主從複製無關)

master2  192.168.169.102
端口  3307

1.確保master1及master2機器mysql實例已安裝好

2.啓動雙主數據庫,創建同步用戶
master 1

mysqld_multi start 1
GRANT REPLICATION SLAVE ON *.* to 'sync'@'192.168.169.102' identified by 'sync';
flush privileges;

master 2

service mysqld start
GRANT REPLICATION SLAVE ON *.* to 'syc'@'192.168.169.101' identified by 'sync';
flush privileges;

3.my.cnf配置
master1配置文件

vim /etc/my.cnf
[mysqld1]
basedir = /mysql
datadir = /mysqldata
port = 3307
socket = /tmp/mysql.sock
log-bin=mysql-bin
server-id=1
auto-increment-increment = 2
auto-increment-offset = 1
log-slave-updates
slave-skip-errors=all
sync_binlog=1
innodb_data_file_path=ibdata1:76M;ibdata2:12m:autoextend

master2配置文件

vim /etc/my.cnf
basedir = /mysql
datadir = /mysqldata
port = 3307
log-bin=mysql-bin
server-id=2
auto-increment-increment = 2
auto-increment-offset = 2
log-slave-updates
slave-skip-errors=all
sync_binlog=1

4.記錄bin-log和pos位置,master1和master2分別記錄

show master status;

master1

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

master2

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 |      410 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

5.啓動複製
master1上

stop slave
change master to master_host='192.168.169.102',master_user='sync',
master_password='sync',master_port=3307,
master_log_file='mysql-bin.000006',master_log_pos=410;
start slave

master2

stop slave
change master to master_host='192.168.169.101',master_user='syc',
master_password='syc',master_port=3307,
master_log_file='mysql-bin.000005',master_log_pos=120;
start slave
show slave status\G;

master1和master2都檢查
確保狀態
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

7.建表測試
參數說明
auto-increment-increment= 2 # 應設爲整個結構中服務器的總數
auto-increment-offset = 1 # 設定數據庫中自動增長的起點,避免兩臺服務器數據同步時出現主鍵衝突
log-slave-updates # 這個參數用來配置從服務器的更新是否寫入二進制日誌,這個選項默認是不打開的,但是,如果這個從服務器B是服務器A的從服務器,同時還作爲服務器C的主服務器,那麼就需要開發這個選項,這樣它的從服務器C才能獲得它的二進制日誌進行同步操作
slave-skip-errors
在複製過程中,由於各種的原因,從服務器可能會遇到執行BINLOG中的SQL出錯的情況,在默認情況下,服務器會停止複製進程,不再進行同步,等到用戶自行來處理。
  Slave-skip-errors的作用就是用來定義複製過程中從服務器可以自動跳過的錯誤號,當複製過程中遇到定義的錯誤號,就可以自動跳過,直接執行後面的SQL語句。
  --slave-skip-errors=[err1,err2,…….|ALL]
  但必須注意的是,啓動這個參數,如果處理不當,很可能造成主從數據庫的數據不同步,在應用中需要根據實際情況,如果對數據完整性要求不是很嚴格,那麼這個選項確實可以減輕維護的成本


sync_binlog


“sync_binlog”:這個參數是對於MySQL系統來說是至關重要的,他不僅影響到Binlog對MySQL所帶來的性能損耗,而且還影響到MySQL中數據的完整性。對於“sync_binlog”參數的各種設置的說明如下:


sync_binlog=0,當事務提交之後,MySQL不做fsync之類的磁盤同步指令刷新binlog_cache中的信息到磁盤,而讓Filesystem自行決定什麼時候來做同步,或者cache滿了之後才同步到磁盤。


sync_binlog=n,當每進行n次事務提交之後,MySQL將進行一次fsync之類的磁盤同步指令來將binlog_cache中的數據強制寫入磁盤。


在MySQL中系統默認的設置是sync_binlog=0,也就是不做任何強制性的磁盤刷新指令,這時候的性能是最好的,但是風險也是最大的。因爲一旦系統Crash,在binlog_cache中的所有binlog信息都會被丟失。而當設置爲“1”的時候,是最安全但是性能損耗最大的設置。因爲當設置爲1的時候,即使系統Crash,也最多丟失binlog_cache中未完成的一個事務,對實際數據沒有任何實質性影響。從以往經驗和相關測試來看,對於高併發事務的系統來說,“sync_binlog”設置爲0和設置爲1的系統寫入性能差距可能高達5倍甚至更多。

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