Docker Compose builds mysql master-slave replication

Configuration file and document address: http://git.oschina.net/wangjingj/mycat-rw

system environment

  • docker 1.12.3

  • mysql5.7.17

  • deepin 15.3 desktop version (this does not matter, because we use docker)

Key points

  • Use the `docker bridge` network, set a static IP

  • Mount using `volumes`, not using the data volume container (because I failed with `docker compose` - -!)

  • Images are created with `build` (preserving extensibility), without `image`

  • So far, no port number has been exposed, just two `slave` linked to `master`. Immediately start to study the use of [mycat] (http://www.mycat.org.cn/) to complete mysql master-slave replication + read and write Separation, please look forward to it.

  • Mount the `hosts` file to use `hostname` instead of ip addresses

Environmental preparation

  • `docker-engine` installation

This is a direct reference to the official documentation. [Install docker-engine under debian](https://docs.docker.com/engine/installation/linux/debian/)

  • `docker-compose` installation

[debian下安装docker-compose](https://docs.docker.com/compose/install/)

  • Pull the `mysql:5.7.17` image

Docker Compose builds mysql master-slave replication

Configuration file to be mounted

  • Directory Structure

Docker Compose builds mysql master-slave replication

A brief description:

  • mysql-master: store the master configuration file

  • mysql-s1: store the first slave configuration file

  • mysql-s2: store the second slave configuration file

  • hosts: local route

  • Configuration of mysql-master

Not much, just a `mysqld.cnf` to append at the end:

Docker Compose builds mysql master-slave replication

Configuration of mysql-s1/mysql-s2

Like master, there is only one `mysqld.cnf` that needs to be appended at the end:

Docker Compose builds mysql master-slave replication

hosts file configuration

Docker Compose builds mysql master-slave replication

docker-compose configuration file and Dockerfile

  • Directory Structure

Docker Compose builds mysql master-slave replication

  • Dockerfile

In fact, the Dockerfiles of `master s1 s2` are the same, we just wrote this to maintain a certain scalability.

We can completely use the `image` of `docker-compose` instead.

Image and build cannot be used together in docker-compose.yml

Well, take a look at this `Dockerfile`

Docker Compose builds mysql master-slave replication

  • docker-compose.yml

Well, here comes the point.

Docker Compose builds mysql master-slave replication

Docker Compose builds mysql master-slave replication

Docker Compose builds mysql master-slave replication

run

Run in the directory of the `docker-compose.yml` file

Docker Compose builds mysql master-slave replication

Don't get excited, we're only halfway through now....

Let's start setting up mysql master-slave replication.

configure master

  • Enter the mysql command line of the master

Docker Compose builds mysql master-slave replication

Enter the value m1test of `MYSQL_ROOT_PASSWORD:` to enter the mysql command line mode.

  • Create user `repl` for master-slave replication

Docker Compose builds mysql master-slave replication

  • Grant slave permissions to the `repl` user

Docker Compose builds mysql master-slave replication

  • Lock the library to prevent data from being written again. This command will automatically unlock when the terminal session is ended.

Docker Compose builds mysql master-slave replication

  • View master status

Docker Compose builds mysql master-slave replication

Note down `master-bin.000003` and `636` for later use in slave.

Configure slave1

  • Enter the mysql command line of s1

Docker Compose builds mysql master-slave replication

Enter the value s1test of `MYSQL_ROOT_PASSWORD:` to enter the mysql command line mode.

  • connect to master

Docker Compose builds mysql master-slave replication

  • start slave

Docker Compose builds mysql master-slave replication

Configure slave2

It's almost the same as slave.... we won't write it...

Experiment 1 Test whether the master can be synchronized to the slave after writing

Well, at this position, the configuration has been completed, is it successful? Let's try it.

  • Create the database ms-test under the mysql command line of the master

Docker Compose builds mysql master-slave replication

  • Go to the two slaves to see if there is also the mstest database.

Docker Compose builds mysql master-slave replication

proves success.

Experiment 2 Create a table and insert a piece of data

Come on by yourself hahaha

Summarize

Through the above steps, we have built a master-slave replication in mysql `master-slave` mode managed by `docker-compose`.

Next, we need to expose the port, or use the `links` attribute to allow applications or other client tools to access our mysql.

In the next step, we need to use the `mycat` middleware to complete our read and write separation.

Please give a thumbs up if you support it, and don't spray if you don't like it.

A little bit every day, grow a little bit more.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326116536&siteId=291194637