1.主從複製
實驗環境:2臺裝有mariadb的centos6,ip地址分別爲192.168.198.203(master ),192.168.194.90(slave)
測試:在master上新建一個數據庫,查看slave中是否同步
#####################################################
master上的配置:
a. 啓動二進制日誌;在mariadb的配置文件/etc/my.cnf中添加
[mysqld]
log_bin=mysql-bin #開啓二進制日誌功能,並將二進制命名爲mysql-bin
b. 爲當前節點設置一個全局惟的ID號;
[mysqld]
server_id=1 #服務器號,要全局唯一
c. 創建有複製權限的用戶賬號;
REPLICATION SLAVE, REPLICATION CLIENT
mariadb> grant replication slave,replication client on *.* to 'repluser'@'192.168.194.90' identified by 'huanghu';
####################################################################
slave上的配置:
(1) 啓動中繼日誌;
[mysqld]
relay_log=relay-log
relay_log_index=relay-log.index
(2) 爲當前節點設置一個全局惟的ID號;
[mysqld]
server_id=2
(3) 使用有複製權限的用戶賬號連接至主數據庫,並啓動複製線程;
change master to master_host='192.168.198.203',master_user='repluser',master_password='huanghu',master_log_file='master-bin.000003', master_log_pos=7828;
## #master_log_file與master_log_pos的值根據在master中運行show master status的結果來確定;
(4)查看salve是否已經連接上master
show slave status
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.198.203
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 8820
Relay_Log_File: relay-log.000003
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
slave與master已經建立連接
#########################################################
主主複製:
兩臺mariadb服務器互爲主從,這樣可以用來實現數據的讀負載均衡,但是寫請求在兩臺服務器上都得直接或間接執行;另外主主模型,不可避免的將會存在時間的延遲與及數據的不一致
實驗環境:2臺裝有mariadb的centos6,ip地址分別爲192.168.198.203(A ),192.168.194.90(B)
配置如下:
1.設置A爲slave,B爲master
a.在/etc/my.cnf中添加以下內容
[mariadb]
server_id=1
relay_log=relay-log ###開啓中繼日誌功能,並將其命名爲relay-log
skip_name_resolv=1####關閉主機名解析
在B的配置文件中添加以下內容:
[mariadb]
server_id=2
log_bin=master-bin
skip-name-resolv=1
#################
b.連接進入mariadb服務器,創建有複製功能的用戶賬號,並開啓複製線程
1.在B上查看二進制日誌的相應信息
MariaDB [hellodb]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 653 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
2.在B上授權一個有複製功能的用戶:
MariaDB [hellodb]> grant replication slave,replication client on *.* to 'repluser'@'192.168.198.203' identified by 'huanghu';
3.在B上設置當A 做爲salve時的master服務器
grant master to master_host='192.168.194.90', master_user='repluser',master_password='huanghu',master_log_file='master-bin.000001',master_log_pos=653;
4.在A上查看連接master服務器的信息
MariaDB [hellodb]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.194.90
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 653
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 758
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
設置A爲slave,B爲master完成
2、對A 設置master ,B設置slave
1.在A 的配置文件/etc/my.cnf中添加以下內容
[mariadb]
log_bin=master-bin
2.在B的配置文件中添加以下內容
[mariadb]
relay_log=relay-log
3.在A中設置具有複製權限的用戶
mysql> grant replication slave,replication client on *.* to 'repluser'@'192.168.194.90' identified by 'huanghu';
4.查看A主機當前的二進制日誌信息
MariaDB [hellodb]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000004 | 473 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
5.在B上設置其對應的master服務器
MariaDB [hellodb]> change master to master_host='192.168.198.203',master_user='repluser',master_password='huanghu' master_log_file='master-bin.000004' master_log_pos=473;
啓動I/O THREAD與SQL THREAD線程
start slave
6.查看B設置是否 成功
MariaDB [hellodb]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.198.203
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000004
Read_Master_Log_Pos: 473
Relay_Log_File: relay-log.000007
Relay_Log_Pos: 758
Relay_Master_Log_File: master-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
B主機爲slave,A爲master也設置成功了
#########################################################
3.半同步複製
主從複製除了具有讀數據負載均衡的作用外,還可以用來做主服務器的數據備份;而爲了能使從數據庫上的數據是主數據庫的完整備份,引入了半同步複製;
半同步複製的原理:半同步是在簡單的主從複製的結構上發展而來的,當主數據庫上執行對數據的修改(增、刪、改)時,不再直接的返回客戶端,而是在從數據庫上至少已經有一臺已經同步了數據後才返回;
在主從複製已可以正常工作的前提下配置半同步複製:
1.在master服務器上安裝 semisync_master.so模塊
a.查看mariadb安裝時是否帶有該模塊
rpm -ql MariaDB-server
/usr/lib64/mysql/plugin/ha_tokudb.so
/usr/lib64/mysql/plugin/handlersocket.so
/usr/lib64/mysql/plugin/query_cache_info.so
/usr/lib64/mysql/plugin/semisync_master.so
/usr/lib64/mysql/plugin/semisync_slave.so
/usr/lib64/mysql/plugin/server_audit.so
/usr/lib64/mysql/plugin/sphinx.so
/usr/lib64/mysql/plugin/sql_errlog.so
b.行裝載操作
A: MariaDB [hellodb]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
##plugin:後接插件名 soname:後接模塊名
B: MariaDB [hellodb]> install plugin rpl_semi_sync_master soname 'semisync_slave.so';
2.查看與rpl_semi_sync_master插件相關的信息
+------------------------------------+-------+
| 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 |
+------------------------------------+-------+
4 rows in set (0.00 sec)
可以看到,插件裝載後並不會自動啓動生效,還需要手動開啓
3.開啓A與B上的插件功能
A: MariaDB [hellodb]> set global rpl_semi_sync_master_enabled=1;
B: MariaDB [hellodb]> set global rpl_semi_sync_slave_enabled=1;
4.查看插件相關參數
MariaDB [hellodb]> show global status like '%semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 14467 |
| Rpl_semi_sync_master_net_wait_time | 28935 |
| Rpl_semi_sync_master_net_waits | 2 |
| Rpl_semi_sync_master_no_times | 1 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | OFF |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 2507 |
| Rpl_semi_sync_master_tx_wait_time | 5014 |
| Rpl_semi_sync_master_tx_waits | 2 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 2 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
半同步複製完成