DEV Community

Cover image for MariaDB with Replication in Docker
Alvaro Nabangi
Alvaro Nabangi

Posted on • Updated on

MariaDB with Replication in Docker

In this blog we are going to demonstrate how to replicate a MariaDB database that runs in a Docker container.

Automatically deploy environment by;

Running the deploy_config.sh after cloning this Repo

GitHub logo nabangi / MariaDB-in-Docker

SQL Database cluster deployment with Replication monitoring in Docker

MariaDB-in-Docker

mariadb container master-slave replication with health Monitoring

Automatically deploy environment by;

Running the deploy_config.sh after clonning the Repo to you Server.

Environment prep and manually deploy

sudo apt update

Installing the Docker engine incase its not installed yet

sudo apt-get remove docker docker-engine docker.io

sudo apt install docker.io

sudo systemctl start docker

sudo systemctl enable docker

logout then login again to enable user rights incase they don't apply immediately

sudo groupadd docker

sudo gpasswd -a "${USER}" docker

usermod -aG docker "${USER}"

Create the database persistent volume to be mounted

mkdir -p /opt/mariadb/master-data

Grant Permissions

sudo chown -R 1001:1001 /opt/mariadb/master-data/

Running containers in dettached mode but when you remove the "-d" it could help with debugging the running configs in the container!

docker run -d or docker-compose up -d 

To login to a container

docker exec -it $container_id bash

mariadb

$ mysql -u my_user -p
  #my_password

after login check databases



Environment prep and manually deploy



sudo apt update


Enter fullscreen mode Exit fullscreen mode

Installing the Docker engine incase its not installed yet



sudo apt-get remove docker docker-engine docker.io

sudo apt install docker.io

sudo systemctl start docker

sudo systemctl enable docker


Enter fullscreen mode Exit fullscreen mode

logout then login again to enable user rights incase they don't apply immediately



sudo groupadd docker

sudo gpasswd -a "${USER}" docker

usermod -aG docker "${USER}"


Enter fullscreen mode Exit fullscreen mode

Create the database persistent volume to be mounted



mkdir -p /opt/mariadb/master-data


Enter fullscreen mode Exit fullscreen mode

Grant Permissions



sudo chown -R 1001:1001 /opt/mariadb/master-data/


Enter fullscreen mode Exit fullscreen mode

Running containers in dettached mode but when you remove the "-d" it could help with debugging the running configs in the container!



docker run -d or docker-compose up -d 


Enter fullscreen mode Exit fullscreen mode

To login to a container



docker exec -it $container_id bash


Enter fullscreen mode Exit fullscreen mode

Mariadb



$ mysql -u my_user -p
  #my_password



Enter fullscreen mode Exit fullscreen mode

after login check databases



show databases;
use my_databse;


Enter fullscreen mode Exit fullscreen mode

then create table form there



exit;


Enter fullscreen mode Exit fullscreen mode

check or configure replication

login with root



$ mysql -u root -p
  #master_root_password


Enter fullscreen mode Exit fullscreen mode

then



show master status;


Enter fullscreen mode Exit fullscreen mode

To import:



docker exec -i adprcc3ms_mariadb-master_1 mysql -uroot -pmaster_root_password my_database < mariadb-master-dump.sql


Enter fullscreen mode Exit fullscreen mode

To export:



docker exec -i adprcc3ms_mariadb-master_1 mysqldump -uroot -pmaster_root_password my_database > mariadb-master-dump.sql



Enter fullscreen mode Exit fullscreen mode

Stop and backup the currently running container, Uncomment the following lines



docker stop $container_id


Enter fullscreen mode Exit fullscreen mode


rsync -a /opt/mariadb/master-data /opt/mariadb/master-data.bkp.$(date +%Y%m%d-%H.%M.%S)


Enter fullscreen mode Exit fullscreen mode

incase you want to scale up number of slaves or scale down



docker-compose up --detach --scale mariadb-master=1 --scale mariadb-slave=3


Enter fullscreen mode Exit fullscreen mode

To clean up



docker stop $container_id

docker rm $container_id


Enter fullscreen mode Exit fullscreen mode

You can also remove all images and stopped contaners using



docker prune -a


Enter fullscreen mode Exit fullscreen mode

Run the following to see if the cronjob you've defined actually runs.



sudo grep CRON /var/log/syslog


Enter fullscreen mode Exit fullscreen mode

VOILAA...!!!

Top comments (2)

Collapse
 
nicolekaestner profile image
NicoleKaestner

In this blog we are going to demonstrate how to replicate a MariaDB database that runs in a Docker container. Vashikaran Baba Ka Number

Collapse
 
nabangi profile image
Alvaro Nabangi

Thank you @nicolekaestner