Docker installs and configures Mysql Cluster on a server

Keywords: Programming MySQL Docker iptables firewall

1. Download the image of mysql5.6 from the docker hub

docker pull mysql:5.6

2. Use mysql 5.6 image to run 4 mysql services, and distinguish by port number

Preparatory work

# Four directories are created on the local machine to store the data, logs and configuration files of four mysql services.
mkdir /data/mysql
cd /data/mysql
mkdir mysql3307 mysql3308 mysql3309 mysql3310
cd mysql3307
mkdir conf logs data
cp -r conf/ data/ logs/ ../mysql3308
cp -r conf/ data/ logs/ ../mysql3309
cp -r conf/ data/ logs/ ../mysql3310

Create and run four mysql containers. The root password of each server is liuhaizhuang.

cd /data/mysql/mysql3307
docker run -p 3307:3306 --name mysql3307 \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=liuhaizhuang \
-d mysql:5.6

cd /data/mysql/mysql3308
docker run -p 3308:3306 --name mysql3308 \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=liuhaizhuang \
-d mysql:5.6

cd /data/mysql/mysql3309
docker run -p 3309:3306 --name mysql3309 \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=liuhaizhuang \
-d mysql:5.6

cd /data/mysql/mysql3310
docker run -p 3310:3306 --name mysql3310 \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=liuhaizhuang \
-d mysql:5.6

All 4 mysql server containers have been created and started!

If you want to link these 4 mysql remotely, you need to open the firewall to 330733083093310

# Edit iptables
vim /etc/sysconfig/iptables
# Add the following 4 lines to exit the save
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3307 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3308 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3309 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3310 -j ACCEPT

Restart the firewall. You can link to the newly installed 4 mysql servers at this remote location.

systemctl restart iptables.service

Description is all linked! Next, you can configure master-slave replication.

Posted by flaab on Fri, 01 Nov 2019 01:03:30 -0700