A simple docker file to setup mysql databases with both master and slave ones.
You can use this Docker file to setup a local development environment with master-slave MySQL databases architecture. Currently it supports only one master and one slave database in the wrapped start_servers.sh
shell script, but it is possible to expand more slave through running command shown in that script.
- Ensure you have installed docker correctly, if not or do not know how to do this, you should read official installation documents;
Then, you have two different ways to run containers, one is to build the image by yourself, while the other one is to pull the image through docker hub directly.
- Pull this repository to one of whatever directory whatever you like:
cd /any/directory/you/want/to/work/on
git clone [email protected]:Martin91/mysql-replication.git
- Build the image:
cd mysql-replication
docker build -t "any-docker-image-name-you-like" .
- After waiting a long time to finishing update apt and install mysql automatically, you can setup two containers now:
chmod +x start_servers.sh
./start_servers.sh
docker pull martin91/mysql_server
docker run --name=mysql_master -p 3306 -e master_role=true -d martin91/mysql_server
# Wait about 5 seconds before run the below command
docker run --name=mysql_slave --link=mysql_master -p 3306 -e slave_role=true -d martin91/mysql_server
docker-machine ip # record your vm's ip address here
docker ps # record ports of containers related to mysql
mysql -uroot -h{vm's ip address} -P{containers binded port on the vm}
It is strongly recommended to setup these containers ONLY under development, test or staging evironments. Use it for production environment is VERY DANGEROUS. Instead, if you are looking for a safe and reliable image, you should checkout to MySQL official image: official: mysql.
More flexible commands based on environment variables, includes:
- mount file dir as persisted data dir
- multiple master
- slave servers count
- explicitly specify ports binding
- database users and passwords
- refactor based on docker-compose
- etc...