標籤:space its ati connect 就會 不用 try roc 忽略
mysql主從複製架構及實現mysql主從複製配置
-
主節點:
-
從節點:
-
啟動中繼日誌
-
為當前節點設定一個全域唯一的ID號
- 使用有複製許可權的使用者帳號串連至主伺服器,並啟動複製線程
具體步驟主節點(192.168.182.130)
-
編輯設定檔/etc/my.cnf,添加以下內容
[mysqld]log_bin=1server_id=1innodb_file_per_table=on
- 建立有複製許可權的使用者帳號
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘192.168.182.132‘ IDENTIFIED BY ‘replpass‘;MariaDB [(none)]> FLUSH PRIVILEGES;
從節點(192.168.182.132)
-
編輯設定檔/etc/my.cnf,添加以下內容
[mysqld]relay_log=relay_logrelay_log_index=relay_log.indexserver_id=7skip_name_resolve=1
-
使用有複製許可權的使用者帳號串連至主伺服器
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.182.130‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000075‘,MASTER_LOG_POS=245;
-
啟動複製線程
MariaDB [(none)]> START SLAVE;
- 查看slave的狀態
MariaDB [(none)]> SHOW SLAVE STATUS\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.182.130 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000075 Read_Master_Log_Pos: 245 Relay_Log_File: 1.000002 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000075 Slave_IO_Running: Yes ##io線程啟動了 Slave_SQL_Running: Yes ## sql線程啟動了 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: 245 Relay_Log_Space: 809 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: 0Master_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
mysql主從複製中應該要注意的問題
-
限制從伺服器為唯讀
-
限制從伺服器為唯讀,在從伺服器上設定read_only=on,想要永久有效,就寫在設定檔中,但是此限制對擁有SUPER許可權的使用者均無效
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘read_only‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| read_only | OFF |+---------------+-------+MariaDB [(none)]> SET GLOBAL read_only=1;
- 如果想要限制所有使用者不能夠進行寫操作,那麼啟動一個串連加讀鎖,並且不要退出該串連
MariaDB [(none)]> FLUSH TABLE WITH READ LOCK;
-
如何保證主從複製的事務安全?
-
slave節點上的兩個檔案
-
複製的監控和維護:
-
清理日誌:使用PURGE命令,清理之前確保資料已經備份過了
-
複製監控
MariaDB [(none)]> SHOW BINARY LOGS;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 483 || mysql-bin.000002 | 264 || mysql-bin.000003 | 264 || mysql-bin.000004 | 264 || mysql-bin.000005 | 990 || mysql-bin.000006 | 514 || mysql-bin.000007 | 264 || mysql-bin.000008 | 245 || mysql-bin.000009 | 245 |+------------------+-----------+MariaDB [(none)]> SHOW BINLOG EVENTS;+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------+| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------+| mysql-bin.000001 | 4 | Format_desc | 1 | 245 | Server ver: 5.5.45-MariaDB-log, Binlog ver: 4 || mysql-bin.000001 | 245 | Query | 1 | 315 | BEGIN || mysql-bin.000001 | 315 | Intvar | 1 | 343 | INSERT_ID=9 || mysql-bin.000001 | 343 | Query | 1 | 456 | use `S_SC_C`; INSERT INTO S (sname,sdept) VALUES (‘xiao‘,‘MA‘) || mysql-bin.000001 | 456 | Xid | 1 | 483 | COMMIT /* xid=43 */ |+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------+MariaDB [(none)]> SHOW MASTER STATUS;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000009 | 245 | | |+------------------+----------+--------------+------------------+MariaDB [(none)]> SHOW PROCESSLIST;+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+| Id | User | Host | db | Command | Time | State | Info | Progress |+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+| 5 | root | localhost | NULL | Query | 0 | NULL | SHOW PROCESSLIST | 0.000 || 6 | system user | | NULL | Connect | 3850 | Waiting for master to send event | NULL | 0.000 || 7 | system user | | NULL | Connect | 3806 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0.000 |+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+MariaDB [(none)]> SHOW SLAVE STATUS\G
-
從伺服器是否落後與主伺服器,在從伺服器的salve有Seconds_Behind_Master可以查看
MariaDB [(none)]> SHOW SLAVE STATUS-> \G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.182.130 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000079 Read_Master_Log_Pos: 334 Relay_Log_File: 1.000006 Relay_Log_Pos: 618Relay_Master_Log_File: mysql-bin.000079 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: 334 Relay_Log_Space: 1182 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
-
確定主從節點資料是否一致,使用一些工具,例如percona-tools
- 如果資料不一致,那麼就要將從節點的所有資料刪除,將主節點的資料備份一份匯入到從節點上,重新複製
-
在主節點上有一個參數,設定為1表示每一次dump事件到從節點的時候,本地的master.info資訊要立即同步到磁碟上,讓從節點的master.info及時得到更新,
MariaDB [(none)]> SET GLOBAL sync_master_info=1;MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘sync_master_info‘;+------------------+-------+| Variable_name | Value |+------------------+-------+| sync_master_info | 1 |+------------------+-------+
- 在從節點上有關於relay_log的參數,例如sync_relay_log_info,表示每一次寫操作立即同步到磁碟檔案上,還有sync_relay_log
雙主模型
-
遇到的問題:
-
資料不一致,這時,我們可能就需要將資料可靠性比較高的伺服器留下來,根據保留的mysql伺服器重新再做一個從伺服器
-
對於自動成長的欄位,如果一個主節點是1,2,3,...,另一個主節點也是1,2,3,...,那麼合并的時候就會出現問題,所以,我們可以讓一個節點的自動成長的欄位使用偶數id,另一個主節點使用奇數id,這樣合并的時候就不會出現問題
-
奇數id的設定
auto_increment_offset=1 #表示從1開始auto_increment_increment=2 #表示一次增長2個
- 偶數id的設定
auto_increment_offset=2auto_increment_increment=2
配置步驟
-
各節點使用一個唯一的server_id
-
都啟動binary log和relay log
-
建立擁有複製許可權的使用者帳號
- 定義自動成長id欄位的數值範圍為奇偶
具體步驟
-
其中一個節點為:
-
編輯設定檔
log_bin=1server_id=1innodb_file_per_table=onrelay_log=relay-logrelay_log_index=relay-log.indexauto_increment_offset=1auto_increment_increment=2
-
建立使用者帳號
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘192.168.182.132‘ IDENTIFIED BY ‘replpass‘;MariaDB [(none)]> FLUSH PRIVILEGES;
- 使用有複製許可權的使用者帳號串連至主伺服器並開啟slave線程
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.182.132‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000006‘,MASTER_LOG_POS=245;MariaDB [(none)]> START SLAVE;
-
在另一個主節點上的操作:
-
編輯設定檔
[mysqld]bin_log=1relay_log=1relay_log_index=relay-log.indexserver_id=7auto_increment_offset=2auto_increment_increment=2
-
建立具有複製許可權的使用者帳號
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘192.168.182.130‘ IDENTIFIED BY ‘replpass‘;MariaDB [(none)]> FLUSH PRIVILEGES;
- 使用具有複製許可權的使用者帳號串連至主節點並啟用slave線程
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.182.130‘,MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000075‘,MASTER_LOG_POS=665;MariaDB [(none)]> START SLAVE;
半同步複製
-
需要用到一個外掛程式,如果是rpm包安裝的外掛程式路徑在/usrl/lib64/mysql/plugin,我使用二進位格式安裝的外掛程式路徑是/usr/local/mysql/lib/plugin
-
主節點上的操作
-
編輯設定檔
[mysqld]log_bin=1server_id=1innodb_file_per_table=onplugin_dir=/usr/local/mysql/lib/plugin
-
添加具有複製許可權的使用者帳號
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘192.168.182.132‘ IDENTIFIED BY ‘replpass‘;MariaDB [(none)]> FLUSH PRIVILEGES;
-
安裝外掛程式
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘;
-
查看有哪些外掛程式
MariaDB [(none)]> SHOW PLUGINS;
-
查看半同步相關的變數
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;+------------------------------------+-------+| Variable_name | Value |+------------------------------------+-------+| rpl_semi_sync_master_enabled | OFF || rpl_semi_sync_master_timeout | 10000 || rpl_semi_sync_master_trace_level | 32 || rpl_semi_sync_master_wait_no_slave | ON |+------------------------------------+-------+
-
rpl_semi_sync_master_enabled:為OFF表示禁用為半同步複製的主節點
-
rpl_semi_sync_master_timeout:表示等待從伺服器應答的逾時時間長度,預設是10s,如果超過這個時間從伺服器沒有給主伺服器應答,那麼就降級為非同步方式運行,不再等待
-
rpl_semi_sync_master_trace_level:表示追蹤層級
-
rpl_semi_sync_master_wait_no_slave:表示在沒有從節點的時候是否要等待,on為等待
- 啟用半同步
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=1;
-
從節點上的操作:
-
編輯設定檔
[mysqld]relay_log=1relay_log_index=relay-log.indexserver_id=7skip_name_resolve=1plugin_dir=/usr/local/mysql/lib/plugin/
-
使用具有複製許可權的使用者帳號串連至主節點
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.182.130‘,MASTER_USER=‘rpluser‘,MASTER_PASSWORD=‘replpass‘,MASTER_LOG_FILE=‘mysql-bin.000078‘,MASTER_LOG_POS=245;
-
安裝外掛程式
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so‘;
-
查看半同步相關的變數
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%semi%‘;+---------------------------------+-------+| Variable_name | Value |+---------------------------------+-------+| rpl_semi_sync_slave_enabled | OFF || rpl_semi_sync_slave_trace_level | 32 |+---------------------------------+-------+
-
啟用半同步
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1;
- 啟用slave線程
MariaDB [(none)]> START SLAVE;
- 以上操作就可以完成一個半同步的複製,可以在主節點上查看相關的狀態變數,查看是否有半同步的slave節點
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE ‘%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 |+--------------------------------------------+-------+# Rpl_semi_sync_master_clients為0表示沒有,為1表示有
複製過濾器
-
讓從節點僅複製指定的資料庫,或指定資料庫的指定表
-
有兩種實現方式:
-
主伺服器僅向二進位日誌中記錄與特定資料庫(特定表)相關的事件,但是可能會導致還原時間點無法實現(畢竟二進位日誌記錄的事件是不完全的),不建議使用
binlog_do_db #指定只記錄哪些資料庫的相關修改操作到二進位記錄檔中,可以指定一個列表,使用逗號隔開binlog_ignore_db #指定只忽略哪些不記錄,其他都記錄,可以指定一個列表,使用逗號隔開
-
從伺服器SQL_THREAD在replay中繼日誌中的事件時,僅讀取與特定資料庫(特定表)相關的事件並應用與本地;但是會造成網路及磁碟io浪費
replicate_do_db= #該處指定的資料庫的相關事件都要進行複製replicate_ignore_db=replicate_do_table=replicate_ignore_table=replicate_wild_do_table= #在指定表的時候可以使用萬用字元replcate_wild_ignore_table=
- 樣本:複製時只複製MYDB資料庫的相關事件
#在從伺服器上進行的操作MariaDB [(none)]> SET GLOBAL replicate_do_db=‘MYDB‘;MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘%replicate%‘;+----------------------------------+-----------+| Variable_name | Value |+----------------------------------+-----------+| replicate_annotate_row_events | OFF || replicate_do_db | MYDB || replicate_do_table | || replicate_events_marked_for_skip | replicate || replicate_ignore_db | || replicate_ignore_table | || replicate_wild_do_table | || replicate_wild_ignore_table | |+----------------------------------+-----------+MariaDB [(none)]> SHOW SLAVE STATUS\G;
mysql主從複製架構及實現