標籤:master mysql 二進位 style
背景介紹 mysql5.5之前版本,mysql主從複製比較簡單
mysql5.6:gtid,multi-thread replication
master1 啟用二進位日誌log-bin = master-binlog-bin-index = master-bin.index
2 選擇一個唯一的server idserver-id = [0~2^32]
3 建立具有複製許可權的使用者replication slave,複製的從節點replication client,聯絡master,擷取資訊的許可權
slave1 啟用二進位日誌relay-log = relay-logrelay-log-index = relay-log.index
2 選擇一個唯一的server id,和主不同server-id = [0~2^32]
3 串連至主伺服器複製檔案從哪裡開始複製?1)master是新的,slave從頭開始複製2)master已經運行一段時間,在master執行一次備份,記錄二進位記錄檔名和事件位置,在slave還原資料,串連至哪一個二進位檔案的哪一個位置?
mysql> change master to master_host= ,master_port= ,master_log_file= ,master_log_pos= ,master_user= ,master_password= ;mysql> start slave;
4 mysql複製線程master會為每一個slave啟動1個dump線程master:dumpslave:IO_thread,SQL_thread可單獨啟動mysql> start slave IO_threadmysql> start slave SQL_thread650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/83/A2/wKioL1d5GneiMxnOAACG0zBjF84685.jpg" border="0" name="" "637" height="209">
5 半同步複製時應指定同步逾時時間,一旦逾時,降級為非同步複製
mysql主從非同步複製 使用二進位格式安裝mysql,並初始化、mtsql服務指令碼複製、my.cnf設定檔複製。
匯出PATH,方便使用mysql命令# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
master
1 修改設定檔# vim /etc/my.cnf
[mysqld]datadir = /data/mysql
innodb_file_per_table = 1log-bin=master-binlog-bin-index=master-bin.indexserver-id = 1
啟動mysql# service mysqld start
2 授權slave複製mysql> grant replication slave on *.* to ‘replicationuser‘@‘192.168.8.31‘ identified by ‘replicationuser‘;
mysql> flush privileges;
mysql> flush tables with read lock; # 鎖住table為唯讀
3 查看master二進位記錄檔狀態,在slave上需要使用mysql> show master status;+-------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-------------------+----------+--------------+------------------+| master-bin.000001 | 355 | | |+-------------------+----------+--------------+------------------+mysql> show binlog events in "master-bin.000001";
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/83/A3/wKiom1d5GnegxXrDAAArlD-a56g963.png" border="0" name="">
slave1 修改設定檔
# vim /etc/my.cnf[mysqld]
datadir = /data/mysql
innodb_file_per_table = 1
relay-log = relay-logrelay-log-index = relay-log.indexserver-id = 10
啟動mysql# service mysqld start
2 配置slave同步設定,並啟動slave複製mysql> change master to master_host=‘192.168.8.30‘,master_user=‘replicationuser‘,master_password=‘replicationuser‘,master_log_file=‘master-bin.000001‘,master_log_pos=355;
mysql> start salve;
3 查看slave的狀態mysql> show slave status\GSlave_IO_State: Waiting for master to send event Master_Host: 192.168.8.30 Master_User: replicationuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 438 Relay_Log_File: relay-log.000004 Relay_Log_Pos: 254 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 438
4 在master查看slavemysql> show slave hosts;
其他設定和說明1 slave上不允許資料庫的寫操作,因此在slave上設定為使用者唯讀模式,但此設定對具有super許可權的使用者無效# vim /etc/my.cnf[mysqld]read-only = on
重啟mysql,即生效# service mysqld restart重啟mysqld,複製線程會自動重啟
修改配置後,或者不重啟,在資料庫中直接修改參數也可以
查看唯讀模式是否生效mysql> show global variables like ‘read_only‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| read_only | ON |+---------------+-------+
2 為了保證master bin-log不在緩衝區快取,立即同步到磁碟上,減少主從複製的延遲時間,在master設定[mysqld] sync-binlog = on
重啟mysql,即生效# service mysqld restart重啟mysqld,複製線程會自動重啟
修改配置後,或者不重啟,在資料庫中直接修改參數也可以
查看立即同步模式是否生效
mysql> show global variables like ‘sync_binlog‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| sync_binlog | 0 |+---------------+-------+
3 重啟mysqld,複製線程會自動重啟,存在哪些問題,如何禁止? 當master執行了一些誤操作,由於延遲原因,誤操作還未同步slave;此時在slave關閉mysql,備份資料恢複到master上,啟動slave的mysql,讓IO_thread跳過剛才的誤操作,再啟動複製功能。若slave啟動後,主從複製立即開始,還會同步剛才的誤操作。 不讓其隨mysql自動啟動,防止同步誤操作。
串連master 需要這兩個檔案master.info,relay-log.info,臨時移除。slave無法串連master
在資料檔案下
master.info 記錄了登陸master和相關資訊relay-log.info 記錄了relay-log和master-bin的相關資訊
4 從伺服器的相關日誌會記錄在slave的錯誤記錄檔中。
5 若master工作了一定時間,此時做主從的注意事項5.1 master鎖表mysql> flush tables with read lock;
5.2 master mysql的資料庫匯出# mysqldump mydb > mydb.sql5.3 slave 建立同名資料庫,並匯入資料庫# mysqldump mydb < mydb.sql
5.4 此時操作和全新的資料庫是相同的,注意master_log_file,master_log_pos即可
6 mysql複製過濾在master上# vim /etc/my.cnf[mysqld]binlog-do-db=db1,db2binlog-ignore-db=db1,db2
master binlog-ignore-db帶來的問題 不記錄部分資料庫的二進位日誌,二進位日誌不完整。當伺服器崩潰時,只能恢複記錄了二進位日誌的資料,未記錄的將不能恢複,因此不建議使用此選項。
在slave上可以進行資料庫層級的過濾,也可以進行表層級的過濾# vim /etc/my.cnf[mysqld]資料庫層級replicate-do-db=replicate-ignore-db=
表層級replicate-do-table=replicate-ignore-table=
在表層級使用通配replicate-wild-do-table=mydb.tb% # 僅複製mydb中以tb開頭的所有表replicate-wild-ignore-table=mydb.tb_ # 僅複製mydb中以tb開頭的、後面跟上一個字元的表
slave replicate-ignore-db帶來的問題 使用此選項,雖不記錄指定slave資料庫的二進位日誌,但是中繼日誌是完整的,因此會佔有slave的頻寬資源。
綜上,如果必須對錶進行過濾,建議在slave上進行。半同步主從複製
1 master 添加模組mysql> install plugin rpl_semi_sync_master soname ‘semisync_master.so‘;
mysql> set global rpl_semi_sync_master_enabled = 1;
mysql> show variables like ‘rpl%‘;+------------------------------------+-------+| Variable_name | Value |+------------------------------------+-------+| rpl_recovery_rank | 0 || rpl_semi_sync_master_enabled | ON || rpl_semi_sync_master_timeout | 10000 | # 非同步複製逾時時間,單位ms| rpl_semi_sync_master_trace_level | 32 || rpl_semi_sync_master_wait_no_slave | ON | # 是否必須等待slave上線+------------------------------------+-------+
2 slave 添加模組mysql> install plugin rpl_semi_sync_slave soname ‘semisync_slave.so‘;
mysql> set global rpl_semi_sync_slave_enabled = 1;
mysql> show variables like ‘rpl%‘;+---------------------------------+-------+| Variable_name | Value |+---------------------------------+-------+| rpl_recovery_rank | 0 || rpl_semi_sync_slave_enabled | ON || rpl_semi_sync_slave_trace_level | 32 |+---------------------------------+-------+
3 若此時主從同步是開啟的,設定不會立即生效,需重啟slave io_thread3.1 master狀態查看mysql> show global status like ‘rpl%‘;+--------------------------------------------+-------------+| Variable_name | Value |+--------------------------------------------+-------------+| Rpl_semi_sync_master_clients | 0 || 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 || Rpl_status | AUTH_MASTER |+--------------------------------------------+-------------+
3.2 slave狀態查看mysql> show global status like ‘rpl%‘;+----------------------------+-------------+| Variable_name | Value |+----------------------------+-------------+| Rpl_semi_sync_slave_status | OFF || Rpl_status | AUTH_MASTER |+----------------------------+-------------+
4 只重啟io_thread即可mysql> stop slave io_thread;
mysql> start slave io_thread;
4.1 master狀態查看mysql> show global status like ‘rpl%‘;+--------------------------------------------+-------------+| Variable_name | Value |+--------------------------------------------+-------------+| Rpl_semi_sync_master_clients | 1 |+--------------------------------------------+-------------+
4.2 slave狀態查看mysql> show global status like ‘rpl%‘;+----------------------------+-------------+| Variable_name | Value |+----------------------------+-------------+| Rpl_semi_sync_slave_status | ON || Rpl_status | AUTH_MASTER |+----------------------------+-------------+
5 測試 當半同步逾時後(10000ms)一次後,會降級為非同步複製。在slave停止io_threadmysql> stop slave io_thread;
在master進行寫操作時,會卡住100000ms,之後降級為非同步複製,恢複速度;
6 為了使參數永久生效,在Master和Slave的my.cnf中編輯: # On Master [mysqld] rpl_semi_sync_master_enabled=1 rpl_semi_sync_master_timeout=1000 # 1 second # On Slave [mysqld] rpl_semi_sync_slave_enabled=1
7 mysql主從機制比較脆弱,若需重啟master,需先要停止slave複製,即stop slave。監控和監控主從複製工具percona-toolkit(mattkit-tools)https://www.percona.com/downloads/percona-toolkit/ # yum localinstall -y percona-toolkit-2.2.18-1.noarch.rpm --nogpgcheck
安裝之後會出現一大堆pt命令pt-slave-delay:使slave比master慢一些時間pt-table-checksum:通過單向加密比較主從的資料是否一致
rhel使用的是光碟片中的rpm庫,無法解決percona的依賴yum localinstall時出現如下提示 You could try using --skip-broken to work around the problem You could try running: package-cleanup --problems package-cleanup --dupes rpm -Va --nofiles --nodigestThe program package-cleanup is found in the yum-utils packagemysql ssl 簡要說明授權時,增加ssl選項,強制使用ssl;若無此選項,不受限制,即使啟用了ssl功能,複製時使用和不使用ssl都可以mysql> grant replication slave on *.* to ‘replicationuser‘@‘192.168.8.31‘ identified by ‘replicationuser‘ require ssl;
ssl需要的內容mysql> show global variables like ‘%ssl%‘;+---------------+----------+| Variable_name | Value |+---------------+----------+| have_openssl | DISABLED || have_ssl | DISABLED || ssl_ca | || ssl_capath | || ssl_cert | || ssl_cipher | || ssl_key | |+---------------+----------+
來自為知筆記(Wiz)
附件列表
mysql主從複製--mysql-5.5非同步、半同步配置