MySQL 主从复制、主主复制、半同步复制
时间:2022-03-14 23:48
半同步复制是基于主从复制模型的,所以实验环境是在主从复制的基础上配置的;
操作步骤如下:
1.配置主从复制的环境,如下:
1)主节点,编辑配置文件/etc/my.cnf
[root@master ~]# vim /etc/my.cnf [mysqld] skip_name_resolve = ON innodb_file_per_table = ON log_bin=master-log # 添加启动二进制日志文件 server-id=1 # server-id
2)从节点,编辑配置文件如下:
[root@slave ~]# vim /etc/my.cnf [mysqld] skip-name-resolve = ON innodb-file-per-table = ON relay-log=relay-log # 中继日志 server-id=2 # server-id=2
2.启动主节点服务器,授权一个可以连接复制的用户账号,并查看二进制日志文件的位置;
[root@master1 ~]# systemctl start mariadb.service MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser‘@‘10.1.%.%‘ IDENTIFIED BY ‘replpass‘; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-log.000001 | 419 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
3.在从节点连接设置连接主节点服务器的相关信息,并启动复制功能;
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘10.1.252.153‘, -> MASTER_USER=‘repluser‘, -> MASTER_PASSWORD=‘replpass‘, -> MASTER_LOG_FILE=‘master-log.000001‘, # 复制主节点的二进制的日志文件 -> MASTER_LOG_POS=419; # 复制主节点二进制日志开始的位置 Query OK, 0 rows affected (0.07 sec) MariaDB [(none)]> START SLAVE; # 启动复制线程 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> SHOW SLAVE STATUS\G # 再次查看slave状态 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.1.252.153 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000001 Read_Master_Log_Pos: 245 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 530 Relay_Master_Log_File: master-log.000001 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: 245 Relay_Log_Space: 818 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 row in set (0.00 sec)
如上,主从复制就已经配置好了,现在我们来实现半同步复制,以为我这里只有一主一从,所以,从节点和主节点之间为同步复制,如下:
4.在主节点上安装支持同步复制的插件 semisync_master.so,并启动该插件
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘; Query OK, 0 rows affected (0.16 sec) # 查看是否安装的插件 semisync_master.so 是否激活 MariaDB [(none)]> SHOW PLUGINS; +--------------------------------+----------+--------------------+--------------------+---------+ | Name | Status | Type | Library | License | +--------------------------------+----------+--------------------+--------------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL | | mysql_old_password | ACTIVE | AUTHENTICATION | NULL | GPL | | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | FEDERATED | ACTIVE | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | Aria | ACTIVE | STORAGE ENGINE | NULL | GPL | | FEEDBACK | DISABLED | INFORMATION SCHEMA | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | | rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL | +--------------------------------+----------+--------------------+--------------------+---------+ 43 rows in set (0.01 sec) #======================================================================================== MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘rpl_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 | +------------------------------------+-------+ 4 rows in set (0.00 sec) MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=ON; # 启动插件 Query OK, 0 rows affected (0.00 sec) 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 | +------------------------------------+-------+ 4 rows in set (0.01 sec)
5.同样,在从节点上也安装支持异步复制的插件 semisync_slave.so,如下:
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so‘; Query OK, 0 rows affected (0.04 sec) # 确保安装的插件是激活的 MariaDB [(none)]> SHOW PLUGINS; +--------------------------------+----------+--------------------+-------------------+---------+ | Name | Status | Type | Library | License | +--------------------------------+----------+--------------------+-------------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL | | mysql_old_password | ACTIVE | AUTHENTICATION | NULL | GPL | | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL | |INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | FEDERATED | ACTIVE | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | Aria | ACTIVE | STORAGE ENGINE | NULL | GPL | | FEEDBACK | DISABLED | INFORMATION SCHEMA | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | | rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL | +--------------------------------+----------+--------------------+-------------------+---------+ 43 rows in set (0.00 sec) #=================================================================================================== MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘rpl_semi%‘; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | OFF | # 目前为关闭状态 | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.00 sec) MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=ON; # 启动插件 Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE ‘rpl_semi%‘; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | rpl_semi_sync_slave_enabled | ON | # 正常启动 | rpl_semi_sync_slave_trace_level | 32 | +---------------------------------+-------+ 2 rows in set (0.01 sec)
6.在从节点上首先关闭复制线程,然后再启动,在主节点上可以发现同步复制的从节点个数从0变为1;
# 在从节点上首先关闭IO复制线程,然后再启动,因为刚启动同步复制插件不会自动跳转为同步; MariaDB [(none)]> STOP SLAVE IO_THREAD; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> START SLAVE IO_THREAD; Query OK, 0 rows affected (0.01 sec) #======================================================================================== # 在主节点上查看两次同步复制的状态 MariaDB [(none)]> SHOW GLOBAL STATUS LIKE ‘%semi%‘; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 0 | # 同步复制的客户端个数为0 | 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 | +--------------------------------------------+-------+ 14 rows in set (0.00 sec) MariaDB [(none)]> SHOW GLOBAL STATUS LIKE ‘%semi%‘; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | # 同步复制的客户端个数为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 | +--------------------------------------------+-------+ 14 rows in set (0.01 sec)
测试:
在主节点导入hellodb数据库,再次查看同步复制的状态,如下:
# 导入hellodb数据库 MariaDB [(none)]> \. /root/hellodb.sql # 查看同步复制的状态,可以发现 MariaDB [hellodb]> SHOW GLOBAL STATUS LIKE ‘%semi%‘; +--------------------------------------------+-------+ | Variable_name | Value | +--------------------------------------------+-------+ | Rpl_semi_sync_master_clients | 1 | | Rpl_semi_sync_master_net_avg_wait_time | 1580 | # 平均等待时长1.58s | Rpl_semi_sync_master_net_wait_time | 55334 | # 累计等待时长55334ms | Rpl_semi_sync_master_net_waits | 35 | # 一共等待35次 | 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 | 1727 | | Rpl_semi_sync_master_tx_wait_time | 55287 | | Rpl_semi_sync_master_tx_waits | 32 | # 等待事务提交的次数 | Rpl_semi_sync_master_wait_pos_backtraverse | 0 | | Rpl_semi_sync_master_wait_sessions | 0 | | Rpl_semi_sync_master_yes_tx | 35 | +--------------------------------------------+-------+ 14 rows in set (0.00 sec)
本文出自 “” 博客,转载请与作者联系!