Basic Docker Swarm for mysql cluster

in #mysql7 months ago

I have created a very basic docker swarm project for mysql HA and Replication
It can be used in any OS

Please find more details here https://gitlab.com/krishiv-g/docker-swarm-mysql-8

Docker Swarm Setup Guide

This guide outlines the steps needed to set up a Docker Swarm environment for a MySQL database cluster.

Step 1: Create a Docker Overlay Network

Run the following command on the Docker Swarm manager node to create an overlay network named swarmnet:

docker network create --driver overlay swarmnet

Step 2: Label the Nodes

docker node update --label-add mydb1=true node01
docker node update --label-add mydb2=true node02
docker node update --label-add mydb3=true node03

Step 3: Create Storage Volumes

docker volume create dbdata
docker volume create dblog

Step 4: MySQL Configuration Files on each node

mydb1.cnf (for node01):

nano mydb1.cnf 
[mysqld]
server-id=1
binlog-format=ROW
log-bin=mysql-bin
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
binlog-do-db=mydb

mydb2.cnf (for node02):

nano mydb2.cnf 
[mysqld]
server-id=2
log-bin=mysql-bin
relay-log=mysql-relay-bin
relay-log-index=mysql-relay-bin.index
log-slave-updates=1
read-only=1
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
replicate-do-db=mydb

mydb3.cnf (for node03):

nano mydb3.cnf 
[mysqld]
server-id=3
log-bin=mysql-bin
relay-log=mysql-relay-bin
relay-log-index=mysql-relay-bin.index
log-slave-updates=1
read-only=1
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=sys
replicate-do-db=mydb

Step 4 : Deploy Containers and Services

docker stack deploy -c docker-compose.yml db

Step 5 : Setup MySQL instances

Create a “replica” user on the Primary instance mydb1

SSH inside the Docker container on node01

docker ps -a

get the container ID from the output , then ssh in the container

docker exec -it 2b9de7807a6e /bin/bash

from inside the container login as mysql root

mysql -u root -p 
CREATE USER 'repli'@'%' IDENTIFIED WITH mysql_native_password BY 'SECURE_REPLI_PASSWORD';
GRANT REPLICATION SLAVE ON *.* TO 'repli'@'%';

create database

CREATE DATABASE mydb;

Get the replication checkpoint from the Primary instance mydb1

SHOW MASTER STATUS;

The output should look something like below. We’ll need the file name and position to setup mydb2 and mydb3:

File: mysql-bin.000003
Position: 573
Binlog_Do_DB: mydb
Binglog_IgnoreDB: mysql,information_schema,performance_schema,sys

step 6: Create the database on the Replica instances

CREATE DATABASE mydb;

Step 7 : Setup Replication on both nodes

STOP REPLICA;
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = 'mydb1',
SOURCE_USER = 'repli',
SOURCE_PASSWORD = 'SECURE_REPLI_PASSWORD',
SOURCE_LOG_FILE = 'mysql-bin.000003',
SOURCE_LOG_POS = 573; 
START REPLICA;

Follow me for more tutorials and guides

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 63017.22
ETH 2457.38
USDT 1.00
SBD 2.61