Docker builds MySQL master-slave cluster

Use Docker to build a MySQL cluster with one master and one slave. The version 8 MySQL image is used. If it is not version 8, some commands in this article will be invalid, and the host system is arbitrary. One-master-one-slave is the same as one-master-multiple-slave process, except that there are a few more containers, and you need to connect to the master several times

insert image description here
planning

port Role
3307 master
3308 slave

train of thought
insert image description here

prepare in advance

Make sure that the linux host has Docker installed. Then download the MySQL image to the local, you can first go to the Docker Hub to find the corresponding version, then copy the download command of the corresponding version on the host machine, and finally wait for the download to complete.
insert image description here

External construction

  1. Get the MySQL configuration file
    You can start a MySQL container at will, and then copy the contents of the /etc directory to the host machine, which is easy to modify.
    For example: Create a folder in the home directory etc3307/, and then use the command docker cp MySQL容器Id/etc/ ~/etc3007/
    At this time , there will be a MySQL configuration file information in this directory.

  2. Modify the MySQL configuration file

    • Copy the configuration file: etc3307copy the directory cp -r ~etc3307/ ~/etc3308/, and then there will be two directories under the home directory, ect3307the configuration file under the directory is used as the configuration of the master, and etc3308the directory is used as the configuration under the slave
    • Use vim to modify the configuration in the two directories my.cnf, and [mysqld]add the following content below the Note the difference!
      #[必须]从服务器唯一ID  数字任意,各个主机间id不能相同!!!
      server-id=1
      #[可选] 0(默认)表示读写(主机),1表示只读(从机) 
      read-only=0
      
  3. Start the container, mount the host directory
    and start the host 3307

docker run --name mysql-master --privileged=true -v ~/etc3307/:/etc/ -p 3307:3306 -e MYSQL_ROOT_PASSWORD=你的主机密码 -d 镜像id

Turn on the slave 3308

docker run --name mysql-slave --privileged=true -v ~/etc3308/:/etc/ -p 3308:3306 -e MYSQL_ROOT_PASSWORD=你的从机密码 -d 镜像id
  1. Create a network so that the two containers can access each other. Note: The access port between containers is 3306
  • Create a network docker network inspect my-bridge(my-bridge is a custom name)
  • Adding the container to the network docker network connect my-bridge mysql-slavemeans docker network connect my-bridge mysql-master
    adding the slave to the network and the host to the network. The format of the command isdocker network connect 网络名 容器名
  • Use the command docker network inspect my-bridgeto check whether the join is successful

At this point, the external environment has been built. Currently, the two MySQL servers can run independently, and the containers can access each other. The server-id in the configuration file is different.

internal configuration

Establish a master-slave relationship between two SQL services

  1. Connect two MySQL servers (pay attention to the firewall). Of course, you docker exec -it 容器id /bin/bashcan also use the command line to connect to the respective containers.

Host 3307 configuration :

--执行以下命令
-- 创建一个专门用来在从服务器使用从而复制主服务器数据的用户
CREATE USER '用户名'@'%' IDENTIFIED WITH mysql_native_password BY '用户密码';
-- 为此用户授予REPLICATION权限
GRANT REPLICATION SLAVE ON *.* TO '此用户名'@'%';
-- 刷新
flush privileges;

Then check your current binlog log status show master status;and remember the file to be copied and the starting position
insert image description here

Slave 3308 configuration :
1.

--执行以下命令
CHANGE REPLICATION SOURCE TO SOURCE_HOST='mysql-master', SOURCE_USER='用户名', SOURCE_PASSWORD='次用户密码', SOURCE_LOG_FILE='上面要记住的主机日志文件 binlog.000003', SOURCE_LOG_POS=上面要记住的起始位置335;

-- 开启从模式,连接主机master
start replica ;
  1. Check the connection status (whether the configuration is successful) show replica status ;
    insert image description here
    If there is a problem, you can use docker logs 容器idthe corresponding log error message to know where the error is.

Now that the setup is complete, any write operation on the master will be immediately synchronized to the slave

Notice:

  • If the slave is not set to read-only, the slave can still write, but the master write will be synchronized to the slave, and the slave write will not synchronize the master
  • Once the host is down, the entire master-slave cluster will not be able to write. There is no such thing as a Redis master-slave cluster that can be monitored similar to the sentinel mechanism, and the master crashes on top of the slave. A single master and multiple slaves cannot achieve high availability.
  • If you want to achieve high availability, you can configure a multi-master and multi-slave cluster. The master will write data to each other, and the data will be transferred between the master and the slave. When one master is down, the other masters and their slaves can still work normally.
  • If you want to operate the cluster, this native situation is perceptible. For example: I want to write data to the master, and read data from the slave, that is, read and write separation, so I have to manually switch the database during operation, as well as at the code level. If we want to achieve non-awareness, we need a 反向代理tool that can do it. We only operate one database, and this code helps us use the corresponding server. Therefore, clusters usually need to be used with tools MyCatlike ShardingSphere.

Guess you like

Origin blog.csdn.net/m0_52889702/article/details/128386931