Skip to content

Instantly share code, notes, and snippets.

@nicosingh
Last active Oct 3, 2021
Embed
What would you like to do?
MySQL cluster using docker
MYSQL_ROOT_PASSWORD=rootpass

TL;DR

To start this cluster, run:

git clone https://gist.github.com/7f4c76b6e1913a11154867149d9cf3cc.git
cd 7f4c76b6e1913a11154867149d9cf3cc
docker-compose up -d

Wait a few seconds, and check the status of the cluster:

docker exec -it 7f4c76b6e1913a11154867149d9cf3cc_mysql-tester_1 ndb_mgm
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)]	2 node(s)
id=11	@172.25.0.8  (mysql-5.7.25 ndb-7.6.9, Nodegroup: 0, *)
id=12	@172.25.0.7  (mysql-5.7.25 ndb-7.6.9, Nodegroup: 0)

[ndb_mgmd(MGM)]	2 node(s)
id=1	@172.25.0.6  (mysql-5.7.25 ndb-7.6.9)
id=2	@172.25.0.3  (mysql-5.7.25 ndb-7.6.9)

[mysqld(API)]	2 node(s)
id=21	@172.25.0.2  (mysql-5.7.25 ndb-7.6.9)
id=22	@172.25.0.4  (mysql-5.7.25 ndb-7.6.9)

ndb_mgm>

🤯

version: "3.4"
x-env-files: &env-files
env_file:
- .env
x-logging: &logging
driver: "json-file"
options:
max-size: "100m"
max-file: "5"
services:
mysql-manager-1:
image: mysql/mysql-cluster:7.6.9-1.1.9-cluster
<<: *env-files
logging: *logging
networks:
- db-network
restart: always
volumes:
- ./my.cnf:/etc/my.cnf
- ./mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: ndb_mgmd
mysql-manager-2:
image: mysql/mysql-cluster:7.6.9-1.1.9-cluster
<<: *env-files
logging: *logging
networks:
- db-network
restart: always
volumes:
- ./my.cnf:/etc/my.cnf
- ./mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: ndb_mgmd
data-node-1:
image: mysql/mysql-cluster:7.6.9-1.1.9-cluster
<<: *env-files
logging: *logging
networks:
- db-network
restart: always
volumes:
- ./my.cnf:/etc/my.cnf
- ./mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: ndbd
data-node-2:
image: mysql/mysql-cluster:7.6.9-1.1.9-cluster
<<: *env-files
logging: *logging
networks:
- db-network
restart: always
volumes:
- ./my.cnf:/etc/my.cnf
- ./mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: ndbd
mysql-server-1:
image: mysql/mysql-cluster:7.6.9-1.1.9-cluster
<<: *env-files
logging: *logging
networks:
- db-network
restart: always
volumes:
- ./my.cnf:/etc/my.cnf
- ./mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: mysqld
ports:
- 3306:3306
mysql-server-2:
image: mysql/mysql-cluster:7.6.9-1.1.9-cluster
<<: *env-files
logging: *logging
networks:
- db-network
restart: always
volumes:
- ./my.cnf:/etc/my.cnf
- ./mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: mysqld
ports:
- 3307:3306
mysql-tester:
image: mysql/mysql-cluster:7.6.9-1.1.9-cluster
<<: *env-files
logging: *logging
networks:
- db-network
restart: always
volumes:
- ./my.cnf:/etc/my.cnf
- ./mysql-cluster.cnf:/etc/mysql-cluster.cnf
command: sleep infinity
networks:
db-network:
[mysqld]
ndbcluster
ndb-connectstring=mysql-manager-1;mysql-manager-2
user=mysql
default_storage_engine=ndbcluster # store your data in data nodes (and not in mysql servers) if we don't specify the db engine in CREATE TABLE
[mysql_cluster]
ndb-connectstring=mysql-manager-1;mysql-manager-2
[ndbd default]
NoOfReplicas=2
DataMemory=80M
IndexMemory=18M
[ndb_mgmd]
NodeId=1
hostname=mysql-manager-1
datadir=/var/lib/mysql
[ndb_mgmd]
NodeId=2
hostname=mysql-manager-2
datadir=/var/lib/mysql
[ndbd]
NodeId=11
hostname=data-node-1
datadir=/var/lib/mysql
[ndbd]
NodeId=12
hostname=data-node-2
datadir=/var/lib/mysql
[mysqld]
NodeId=21
hostname=mysql-server-1
[mysqld]
NodeId=22
hostname=mysql-server-2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment