一、主从复制
MariaDB是将客户端上传的数据从主节点复制一份到从节点,从而可以提高读性能,而这种方式并不能提升写性能,因为每一份数据都会在从节点上写一份
缺点:为了增加读写性能,数据库都是将数据 先存入内存中,随后同步到数据文件中,也就是磁盘上,两者同步是异步同步,也就是说,从节点上的数据是落后于主节点的
复制过程:
客户端写入数据---->主服务器的事务日志内存---->事物日志文件---->同步到数据文件---->通过二进制文件---从服务器的IO线程---->中继日志--->通过SQL线程来重放中继日志
实现:
主数据库服务器
[[email protected] ~]# vim /etc/my.cnf [mysqld] server_id=1 log_bin=log-bin MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO [email protected]"192.168.1.%" IDENTIFIED BY 'centos'; #授权主从复制账户 MariaDB [(none)]> FLUSH PRIVILEGES; #刷新 MariaDB [(none)]> SHOW MASTER STATUS; #查看当前使用的是哪一个二进制日志,还有POS的位置,需要在从节点使用 +----------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +----------------+----------+--------------+------------------+ | log-bin.000001 | 489 | | | +----------------+----------+--------------+------------------+
从服务器
[[email protected] ~]# vim /etc/my.cnf server_id=2 relay_log=relay-log read_only=ON MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.151',MASTER_USER='test',MASTER_PASSWORD='centos',MASTER_LOG_FILE='log-bin.000001',MASTER_LOG_POS=489; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G; Slave_IO_Running: Yes #这两项需要为yes,代表开启IO线程和SQL线程 Slave_SQL_Running: Yes
测试在主节点创建数据库或者插入数据,即可同步
二、双主模型
双主模型,容易造成数据不一致,一般不建议使用
主机A
[[email protected] ~]# vim /etc/my.cnf [mysqld] server_id=1 log_bin=log-bin relay_log=relay-log MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO [email protected]"192.168.1.%" IDENTIFIED BY 'centos'; CHANGE MASTER TO MASTER_HOST='192.168.1.152',MASTER_USER='test',MASTER_PASSWORD='centos',MASTER_LOG_FILE='log-bin.000001',MASTER_LOG_POS=489; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G;
主机B
[[email protected] ~]# vim /etc/my.cnf [mysqld] server_id=2 log_bin=log-bin relay_log=relay-log MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO [email protected]"192.168.1.%" IDENTIFIED BY 'centos'; CHANGE MASTER TO MASTER_HOST='192.168.1.151',MASTER_USER='test',MASTER_PASSWORD='centos',MASTER_LOG_FILE='log-bin.000001',MASTER_LOG_POS=489; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G;
三、半同步复制
半同步复制是有一台主机同步写磁盘,当客户端写入数据,主数据库服务器将立刻将数据写入指定从服务器,立即同步,然后在相应客户端,以证明有一份完整的数据,以防主服务器故障
实现半同步复制需要向数据库添加主服务器添加semisync_master.so,从服务器添加semisync_slave.so
主服务器
MariaDB [none]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; MariaDB [none]> SHOW GLOBAL VARIABLES LIKE 'rpl_semi%'; MariaDB [none]> SET GLOBAL rpl_semi_sync_master_enabled=ON; #开启同步写数据文件 MariaDB [none]> SHOW GLOBAL VARIABLES LIKE 'rpl_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 | +------------------------------------+-------+
从服务器
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; MariaDB [none]> SHOW GLOBAL VARIABLES LIKE 'rpl_semi%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | OFF | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ MariaDB [none]> STOP SLAVE IO_THREAD; MariaDB [none]> SHOW GLOBAL VARIABLES LIKE 'rpl_semi%'; MariaDB [none]> START SLAVE IO_THREAD; MariaDB [none]> SHOW GLOBAL VARIABLES LIKE 'rpl_semi%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | OFF | | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+
原文地址:http://blog.51cto.com/10492754/2125260
时间: 2024-10-24 15:52:53