在做MySQL主从半同步之前先做主从复制
一、部署MySQL主从半同步复制
半同步复制支持多种插件:/usr/lib64/mysql/plugin/*
半同步复制插件:
semisync_master.so
semisync_slave.so
[root@localhost ~]# rpm -ql mariadb-server | grep semisync
/usr/lib64/mysql/plugin/semisync_master.so
/usr/lib64/mysql/plugin/semisync_slave.so
1、111、112进入mysql查看插件是否安装
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 6
Server version: 5.5.41-MariaDB-log MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> show plugins;
2、如果没有安装,则先在主节点安装并启用插件:
MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show plugins;
MariaDB [(none)]> show global variables like '%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 '%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.00 sec)
3、在从节点安装并启用插件:
MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show plugins;
MariaDB [(none)]> show global variables like '%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 '%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.00 sec)
4、先查看主节点,然后从节点启动IO线程,在查看主节点
MariaDB [(none)]> show global status like '%semi%';
从节点启动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.00 sec)
主节点:
5、在主节点为事先创建的用户进行主从复制授权:
MariaDB [(none)]> grant replication slave on *.* to 'myslave'@'192.168.200.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000004 | 931 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
6、从节点:
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.200.111',
-> MASTER_USER='myslave',
-> MASTER_PASSWORD='123456',
-> MASTER_LOG_FILE='mysql-binlog.000004',
-> MASTER_LOG_POS=931;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
7、在主节点创建数据库mydb
MariaDB [(none)]>create database nydb character set 'utf8';
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
| test01 |
| test02 |
+--------------------+
7 rows in set (0.01 sec)
在从节点查看数据库是否复制成功:
发现数据库mydb复制成功
在主节点进行数据操作:
MariaDB [(none)]> use mydb
Database changed
MariaDB [mydb]> create table tbl1 (id int,name varchar(100));
Query OK, 0 rows affected (0.03 sec)
主节点继续操作:
MariaDB [mydb]> insert into tbl1 values (1,'tom');
Query OK, 1 row affected (0.01 sec)
我们发现从节点的数据都在变化: