How to deploy a MySQL Cluster from Scratch with Docker

[Image source : http://www.nanoshots.com.br/2017/01/docker-restaurando-dumps-em-containers.html]

Hi,

Today, let’s see how to deploy, a MySQL cluster with Docker, and connect to the nodes from our local machine.

Read more on MySQL Cluster : MySQL Cluster Reference Manual

Here I’m going to create 1 Management node, 2 Data nodes and 2 SQL nodes. Basically the nodes in the cluster are running on separate hosts in a network. So we create a network in docker and connect the containers to the network. Let’s get started.

Before we start makesure that you have installed docker in your machine. If not go ahead and install docker and get familier with it. This documentation will help you.

Step 1: Create the docker network.

Open the terminal and run the following docker command.

docker network create cluster --subnet=10.100.0.0/16

You can define your own subnet for this.

Step 2: Get the mysql docker repository

This step is not mandetory. I do this because I need to use different sub net other than the default one.

  • Clone the mysql docker repository. git clone https://github.com/mysql/mysql-docker.git
  • Checkout the mysql-cluster branch
  • Open mysql-docker/7.5/cnf/mysql-cluster.cnf
  • By default mysql-cluster.cnf is configured to use a single mysql node. Also the ip addresses are configured. Change the ip addresses of each node to match the subnet.
    For example, here is how my configuration looks like…
[ndb_mgmd]
NodeId=1
hostname=10.100.0.2
datadir=/var/lib/mysql
[ndbd]
NodeId=2
hostname=10.100.0.3
datadir=/var/lib/mysql
[ndbd]
NodeId=3
hostname=10.100.0.4
datadir=/var/lib/mysql
[mysqld]
NodeId=4
hostname=10.100.0.10
[mysqld]
NodeId=5
hostname=10.100.0.11
  • Open mysql-docker/7.5/cnf/my.cnf and modify the ndb-connectstring to match the ndb_mgmd node.
[mysqld]
ndbcluster
ndb-connectstring=10.100.0.2
user=mysql
[mysql_cluster]
ndb-connectstring=10.100.0.2
  • Build the docker image.
docker build -t <image_name> <Path to docker file>docker build -t mysql-cluster mysql-docker/7.5

After successfully completing this step, we can start creating the necessary nodes for our cluster.

Step 3: Create the manager node.

We create manager node with the name, management1 and ip: 10.100.0.2.

docker run -d --net=cluster --name=management1 --ip=10.100.0.2 mysql-cluster ndb_mgmd

Step 4: Create the data nodes

docker run -d --net=cluster --name=ndb1 --ip=10.100.0.3 mysql-cluster ndbd
docker run -d --net=cluster --name=ndb2 --ip=10.100.0.4 mysql-cluster ndbd

Step 5: Create the sql nodes.

docker run -d --net=cluster --name=mysql1 --ip=10.100.0.10 -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql-cluster mysqld
docker run -d --net=cluster --name=mysql2 --ip=10.100.0.11 -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql/mysql-cluster mysqld

Just to check whether is everything worked correctly, run

docker run -it --net=cluster mysql-cluster ndb_mgm

The cluster management console will be loaded.

[Entrypoint] MySQL Docker Image 7.5.7-1.1.0
[Entrypoint] Starting ndb_mgm
-- NDB Cluster -- Management Client --
ndb_mgm>

Run show command and you will see the following out put.

ndb_mgm> show
Connected to Management Server at: 10.100.0.2:1186
Cluster Configuration
— — — — — — — — — — -
[ndbd(NDB)] 2 node(s)
id=2 @10.100.0.3 (mysql-5.7.19 ndb-7.5.7, Nodegroup: 0, *)
id=3 @10.100.0.4 (mysql-5.7.19 ndb-7.5.7, Nodegroup: 0)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @10.100.0.2 (mysql-5.7.19 ndb-7.5.7)
[mysqld(API)] 2 node(s)
id=4 @10.100.0.10 (mysql-5.7.19 ndb-7.5.7)
id=5 @10.100.0.11 (mysql-5.7.19 ndb-7.5.7)

OK, it’s working. Back to work. :)
Now let’s configure our mysql nodes so that we can login to them remotely and create the data bases.

Step 7. Change the default passwords.

When the sql nodes are created initially, a random password is set. To get the default password,

docker logs mysql1 2>&1 | grep PASSWORD

To change the password, run the following command and login to the mysql node.

docker exec -it mysql1 mysql -uroot -p

Copy and paste the password from the previous command and press enter.
Now you will be logged in to the mysql node1. Change the password of the root user.

ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘root’;

To login from a different host,

GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘root’ WITH GRANT OPTION;
FLUSH PRIVILEGES;

Exit from the node1 and do the same for the mysql node 2 as well.

Step 8: Login and create a new database.

Let’s try to login to the mysql node from our local machine.
Run the following command with respective ip, user and password.

mysql -h10.100.0.10 -uroot -p
.
.
.
mysql>

It’s working. :)
Just to check the cluster functionality, create a new database in one mysql node.

CREATE SCHEMA TEST_DB;mysql> create schema test_db;
Query OK, 1 row affected (0.04 sec)
Now login to the other mysql node and run
SHOW DATABASES;
mysql> show databases;
+ — — — — — — — — — — +
| Database |
+ — — — — — — — — — — +
| information_schema |
| mysql |
| ndbinfo |
| performance_schema |
| sys |
| test_db |
+ — — — — — — — — — — +
8 rows in set (0.00 sec)

You can see the same database has created in the other node as well.

Cheers!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store