主配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/master-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=1
sync_binlog=on #在事務提交時,立即把bin-log緩存區中的事件立即刷寫到磁盤到的二進制日誌區上,保證從服務器儘早同步數據
innodb_flush_log_at_trx_commit=on #當使用innodb引擎,在事務提交時,把內存中跟事務相關的數據立即寫到事務日誌中
innodb_support_xa=on #innodb支持分佈式事務
從配置文件
[mysqld]
datadir=/data/mydata
# log-bin=/data/binlogs/bin-log
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
relay-log=/data/binlogs/relay-bin
server-id=7
read_only=on
skip_slave_start=on #第1次啓動START SLAVE後,重啓從mysql後,默認slave自動啓動,應該關閉自動啓動
#在主節點授權具有複製權限用戶 爲了方便,我這裏給的權限十分大,生產環境不建議這麼搞。一般給予REPLICATION SLAVE,REPLICATION CLIENT MariaDB [(none)]> GRANT ALL ON *.* to "test"@"10.0.0.%" identified by "test"; MariaDB [(none)]> flush privileges; #在從服務器上連接到主 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.0.0.41',MASTER_USER='test',MASTER_PASSWORD='test'; MariaDB [(none)]> START SLAVE; #上面的方式,是從數據庫從0開始同步主的數據,很簡單就沒有貼實驗 #下面是先做完全還原後再從事件某位置同步主數據 [root@centos7-4 ~]# mysqldump --all-databases --flush-logs --master-data=2 --lock-all-tables >all.sql [root@centos7-4 ~]# less all.sql -- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000004', MASTER_LOG_POS=245; [root@centos7-4 ~]# scp all.sql 10.0.0.41:/root/ MariaDB [(none)]> create database helloit; MariaDB [(none)]> use helloit; MariaDB [helloit]> CREATE TABLE test (id int); MariaDB [helloit]> INSERT INTO test VALUES (1),(2),(3); MariaDB [helloit]> show tables; +-------------------+ | Tables_in_helloit | +-------------------+ | test | +-------------------+ 1 row in set (0.00 sec) MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ MariaDB [helloit]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.88" IDENTIFIED BY "test"; #在備節點上 #先導入完全備份的數據 MariaDB [(none)]> source /root/all.sql; MariaDB [test]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ #並沒有 helloit這個 #這時再實行主從同步 MariaDB [test]> CHANGE MASTER TO MASTER_HOST="10.0.0.41",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="master-bin.000004",MASTER_LOG_POS=245; #在從上啓動sql和IO線程 MariaDB [test]> START SLAVE; #驗證實驗結果 MariaDB [test]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | helloit | | mysql | | performance_schema | | test | +--------------------+ MariaDB [test]> use helloit; MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ MariaDB [helloit]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000004 | 1115 | | | +-------------------+----------+--------------+------------------+ #再次在主數據庫插入數據 MariaDB [helloit]> INSERT INTO test VALUES (4),(5),(6); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +------+ #在從數據庫上查看結果 MariaDB [helloit]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +------+ 6 rows in set (0.00 sec) MariaDB [helloit]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.41 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000004 Read_Master_Log_Pos: 1115 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 1400 Relay_Master_Log_File: master-bin.000004 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: 1115 Relay_Log_Space: 1688 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、從服務器應該爲只讀模式,在配置文件裏設置read_only=on,但是對於超級管理用戶無效,需要從終端施加讀鎖,一直在後臺運行:
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
2、在主從節點分別設置sync_master_info=on、sync_relay_log=on、sync_relay_log_info=on會降低I/O性能
主主複製
配置步驟:
1、各個節點使用一個唯一的server_id
2、都啓動binary log 和relay_log
3、創建擁有複製權限的用戶帳號
4、各自定義自動增長鍵爲奇數和偶數
5、均把對方指定爲主節點,並啓動複製線程
主配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/master-bin
relay_log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=1
auto_increment_offset=1
auto_increment_increment=2
從配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/bin-log
relay-log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=7
auto_increment_offset=2
auto_increment_increment=2
各自創建授權帳號 主1 MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.88" IDENTIFIED BY "test"; MariaDB [(none)]> flush privileges; MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000001 | 671 | | | +-------------------+----------+--------------+------------------+ 主2 MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.41" IDENTIFIED BY "test"; MariaDB [(none)]> flush privileges; MariaDB [(none)]> show master status; +----------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +----------------+----------+--------------+------------------+ | bin-log.000003 | 671 | | | +----------------+----------+--------------+------------------+ #在主1上連主2 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST="10.0.0.88",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="bin-log.000003",MASTER_LOG_POS=671; #在主2上連主1 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST="10.0.0.41",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="master-bin.000001",MASTER_LOG_POS=671; #分別啓動從模式 start slave #在主1上插入一些數據 MariaDB [(none)]> source /root/hellodb.sql MariaDB [hellodb]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.02 sec) #在主2上查看是否同步 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) #在主2上創建一個數據庫 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | IT | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec) #在主1上查看 MariaDB [hellodb]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | IT | | hellodb | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.02 sec) 已經雙主同步 #主1上的二進制日誌和中繼日誌 [root@centos7-4 data]# ls /data/binlogs/ master-bin.000001 master-bin.index [root@centos7-4 data]# ls binlogs/ master-bin.000001 master-bin.index [root@centos7-4 data]# ls relogs/ relay-bin.000001 relay-bin.000002 relay-bin.index #主2上的二進制日誌和中繼日誌 [root@mariadb data]# ls binlogs/ bin-log.000001 bin-log.000002 bin-log.000003 bin-log.index [root@mariadb data]# ls relogs/ relay-bin.000001 relay-bin.000002 relay-bin.index
半同步
#主配置文件
[mysqld]
datadir=/data/mydata
log-bin=/data/binlogs/master-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=1
#從配置文件
[mysqld]
datadir=/data/mydata
#log-bin=/data/binlogs/bin-log
relay-log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=7
#主上創建具有複製權限的帳號 MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO "test"@"10.0.0.88" IDENTIFIED BY "test"; #在從上連接主數據庫 MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST="10.0.0.41",MASTER_USER="test",MASTER_PASSWORD="test",MASTER_LOG_FILE="master-bin.000003",MASTER_LOG_POS=413; MariaDB [(none)]> start slave; #檢查主從能同步後,關閉主從狀態,加入半同步插件 MariaDB [(none)]> stop slave; #在主節點上安裝主的半同步插件 MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=1; MariaDB [(none)]> show global variables like "%semi%"; +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | rpl_semi_sync_master_enabled | ON | | rpl_semi_sync_master_timeout | 10000 | | rpl_semi_sync_master_trace_level | 32 | | rpl_semi_sync_master_wait_no_slave | ON | +------------------------------------+-------+ #在從節點上安裝從的半同步插件 MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show global variables like "%semi%"; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | ON | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.00 sec) MariaDB [(none)]> start 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 | +--------------------------------------------+-------+ #已經有一個備用節點加入 #在主上創建新數據,測試效果 MariaDB [(none)]> CREATE DATABASE MBA; MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%semi%'; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 17434 | | Rpl_semi_sync_master_net_wait_time | 17434 | | Rpl_semi_sync_master_net_waits | 1 | | 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 | 18407 | | Rpl_semi_sync_master_tx_wait_time | 18407 | | 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 | 1 | +--------------------------------------------+-------+ #完成半同步過程需要1.8s左右。
複製過濾器
讓從節點僅複製指定數據庫,或指定數據庫的指定表
有2種方式:
1、主服務器僅向二進制日誌中記錄與特定數據庫(特定表)相關的事件,不建議使用,因爲無法實現時間還原
2、從服務器SQL_THREAD在replay中繼日誌中的事件時,僅讀取與特定數據庫(特定表)相關的事件並應用於本地
在從服務器配置參數過濾,只複製MBA之個庫
從數據庫配置文件
[mysqld]
datadir=/data/mydata
#log-bin=/data/binlogs/bin-log
relay-log=/data/relogs/relay-bin
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=on
server-id=7
replicate_do_db=MBA #只允許複製MBA
注:可用參數
replicate_do_db= #白名單,只複製某個數據庫
replicate_ignore_db= #黑名單
replicate_do_table= #表的白名單
replicate_ignore_table= #表的黑名單
replicate_wild_do_table= #表的通配表達,白名單
replicate_wild_ignore_table=
在主上插入數據 MariaDB [MBA]> CREATE TABLE hello (id int); MariaDB [MBA]> INSERT INTO hello VALUES (1),(2),(3); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [MBA]> select * from hello; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 在從上查看 MariaDB [MBA]> show tables; +---------------+ | Tables_in_MBA | +---------------+ | hello | +---------------+ 1 row in set (0.00 sec) MariaDB [MBA]> select * from hello; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 在主上插入其他庫數據 MariaDB [MBA]> create database it; Query OK, 1 row affected (0.00 sec) MariaDB [MBA]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | MBA | | it | | mysql | | performance_schema | | test | +--------------------+ 在從數據庫上查看 MariaDB [MBA]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | MBA | | mysql | | performance_schema | | test | +--------------------+ 並沒有it這個新創建的數據庫