MySQL Group Replication on Ubuntu 16.04
Scenario, we want to set up mysql group replication in 2 mysql nodes. Both are master and master (can read and write).
We implement the nodes in cloud. Each node has 1 IP Public and 1 IP Private. We set the nodes in same local network (switch).
Mysql Server | IP Private | Type |
---|---|---|
Node 1 | 172.16.0.1 | Master |
Node 2 | 172.16.0.2 | Master |
How to install mysql server in ubuntu you can see in previous post Install-mysql-in-Ubuntu-16.04.
1. Configure my.cnf file in Node 1 and Node 2
Generate unique code by uuidgen command (only in node 1), copy the code and save it in somewhere. The code will be used for loose-group_replication_group_name in my.cnf
1 | root@abcdefg:~# uuidgen |
Then edit my.cnf in node 1 and node 21
root@abcdefg:~# sudo nano /etc/mysql/my.cnf
my.cnf file for node 1
1 | !includedir /etc/mysql/conf.d/ |
my.cnf file for node 2 will be similar with node 1. The difference is only in the #Host specific replication configuration section. So just replace that secton with this line below.1
2
3
4
5# Host specific replication configuration
server_id = 2
bind-address = "172.16.0.2"
report_host = "172.16.0.2"
loose-group_replication_local_address = "172.16.0.2:33061"
Restart mysql to apply the changes1
root@abcdefg:~# sudo systemctl restart mysql
Open up access to these two ports in our firewall1
2root@abcdefg:~# sudo ufw allow 33061
root@abcdefg:~# sudo ufw allow 3306
Do not forget to open up access in your external firewall if you have it. If you use the Alibaba cloud instance, you can open the port by following this tutorial https://www.alibabacloud.com/help/doc-detail/25475.htm
2. Configure Replication User and Enable Group Replication Plugin
Do it in node 1 and node 21
root@abcdefg:~# mysql -u root -p
Then you will be in mysql console1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'password' REQUIRE SSL;
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='password' FOR CHANNEL 'group_replication_recovery';
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> SHOW PLUGINS;
+----------------------------+----------+--------------------+----------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+----------------------+---------+
| | | | | |
| . . . | . . . | . . . | . . . | . . . |
| | | | | |
| group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL |
+----------------------------+----------+--------------------+----------------------+---------+
45 rows in set (0.00 sec)
3. Start Group Replication
In node 1:1
2
3mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
You only need to set ON and OFF for SET GLOBAL group_replication_bootstrap_group in one node only, and for another node you just do START GROUP_REPLICATION.
Run this in node 2:1
mysql> START GROUP_REPLICATION;
4. Check and Test Group Replication
To check group replication node is working, you just do this in one of node. Check MEMBER_STATE column if it is ONLINE or OFFLINE.1
2
3
4
5
6
7
8mysql> SELECT * FROM performance_schema.replication_group_members;
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| group_replication_applier | 0dcbd0f2-8e47-4f04-9ae1-2650f5087d7b | 172.16.0.1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| group_replication_applier | 0dcbd0f2-8e47-4f04-9ae1-2650f5087d7b | 172.16.0.2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
1 row in set (0.00 sec)
You can test to create a database, insert the data inside the table in one of the node. If it is working, the database will be replicated in another node. We configure the group replication as MASTER and MASTER so both of nodes can WRITE and READ.
Do this in node 1.
1 | mysql> CREATE DATABASE playground; |
Check the data also in node 2.1
2
3
4
5
6
7
8mysql> SELECT * FROM playground.equipment;
+----+-------+-------+-------+
| id | type | quant | color |
+----+-------+-------+-------+
| 1 | slide | 2 | blue |
+----+-------+-------+-------+
1 row in set (0.00 sec)
mysql> INSERT INTO playground.equipment (type, quant, color) VALUES ("swing", 10, "yellow");
You can check the data inside playground.equipment in node 1 and node 2. If it works fine, i will show like this bellow1
2
3
4
5
6
7
8mysql> SELECT * FROM playground.equipment;
+----+-------+-------+--------+
| id | type | quant | color |
+----+-------+-------+--------+
| 1 | slide | 2 | blue |
| 2 | swing | 10 | yellow |
+----+-------+-------+--------+
2 row in set (0.00 sec)