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)
半同步复制完成