Docker-Compose implements Mysql master-slave

Docker-Compose implements Mysql master-slave

I bought cloud server from cnaaa.com.

1 Introduction

By using docker-composeto build a master-slave database, this example splits two servers into two compose files for decoupling, and of course it can also be put into one compose file

demo mysql version: 5.7.16

2. Deployment process

master node:

  1. install mysql-server
  2. Change setting
  3. Create an account for synchronization and authorize
  4. Check related configuration

slave node:

  1. install mysql-server
  2. Change setting
  3. Select master node
  4. Check relevant configuration and verify sync functionality

3. master node

3.1 install mysql

  1. Create the mysql folder and go into the folder (folder name mysql)

  2. Create a docker-compose file as follows

    # docker-compose.yml
    version: '3'
    services:
      mysql:
        restart: "no"
        image: mysql:5.7.16
        container_name: mysql-master
        volumes:
          - ./datadir:/var/lib/mysql
          - ./conf/mysql:/etc/mysql
        environment:
          - "MYSQL_ROOT_PASSWORD=123456"
          - "TZ=Asia/Shanghai"
        ports:
          - 3306:3306
        networks:
          - mysql-net
    networks:
      mysql-net:
        driver: bridge
    

    Note: Because the configuration file needs to be hung in the service, the configuration file in the container must be copied to the host first

    1. First start a container for copying files

      $ docker run --name mysql-temp -e MYSQL_ROOT_PASSWORD=root --rm -d  mysql:5.7.16
      
    2. Copy mysql-tempthe configuration file in the container, and now the conf folder contains all the configuration files that come with mysql

      $ docker cp mysql-temp:/etc/mysql conf
      

      img

    3. Because the current conf directory my.cnfis still a link, so directly use the backup file in the current directory as the main configuration file

      $ mv my.cnf.fallback my.cnf
      
  3. Modify the configuration filemy.cnf

    Add configuration information at the bottom of the file

    [mysqld]
    log-bin=mysql-bin # 开启 binlog
    server-id=1 # 当前server在cluster中的id,必须保证在cluster中唯一
    #只保留7天的二进制日志,以防磁盘被日志占满(可选)
    expire-logs-days = 7
    #不备份的数据库 (可选)
    binlog-ignore-db=information_schema  
    binlog-ignore-db=performation_schema
    binlog-ignore-db=sys
    
  4. Start the mysql service, and know the real network name through the output content mysql_mysql-net, that is, the name of the current folder is spliced ​​with the network name specified in the file

    img

  5. After the service is started, create a user for synchronization and authorize it

    The created user name is slavepassword123456

    CREATE USER 'slave' @'%' IDENTIFIED BY '123456';
    GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave' @'%';
    #刷新权限
    FLUSH PRIVILEGES;
    
  6. View master status information

    SHOW MASTER STATUS;
    #查看Mater数据有哪些slave
    select * from information_schema.processlist as p where p.command = 'Binlog Dump'; 
    

    img

4. slave node

The installation steps are the same as the master, only show what needs to be modified, the current directory structure is as follows

img

docker-compose.yamlMainly modified the network-related information and container_name (the network name is explained above)

version: '3'
services:
  mysql:
    restart: "no"
    image: mysql:5.7.16
    container_name: mysql-slave
    volumes:
      - ./datadir:/var/lib/mysql
      - ./conf:/etc/mysql
    environment:
      - "MYSQL_ROOT_PASSWORD=123456"
      - "TZ=Asia/Shanghai"
    ports:
      - 3307:3306
    networks:
      - mysql_mysql-net
networks:
  mysql_mysql-net:
    external: true # 来自外部

my.cnfThe content added is as follows:

[mysqld]
server-id=2
relay_log=relay-log
#开启只读 意味着当前的数据库用作读,当然这也只会影响到非root的用户,如果使用root用户操作本库是不会有影响的
read_only=ON

After the setting is complete, start the slave server, connect the slave and associate with the master node

  • MASTER_HOST: Use container_name directly
  • MASTER_LOG_FILE/MASTER_LOG_POS: Directly use the value of the last step in the master installation step, which is actually to specify the name and offset of the bin-log file to be synchronized
CHANGE MASTER TO
MASTER_HOST='mysql-master',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;

Start after management is completesalve

START SLAVE;

last viewslave status

SHOW SLAVE STATUS;

img

5. Verification

Create a test database on the master and create a user table, refresh and view the salve library, and the corresponding library table appears

img

It has been verified that there is no problem with data synchronization.

6. Possible problems

SHOW SLAVE STATUSfound slave_io_running=No salve_sql_running=Nothat there are many possible reasons, you can check the output in the following fields

img

possible reason:

  1. The master-slave network fails
  2. For the duplication of two nodes server-id, just modify the corresponding id directly
  3. The uuid of the database is the same (probably because the database file is copied directly), just generate a uuid different from the master in the corresponding library and put it auto.cnfin
  4. SQL execution failed. It may be that the slave has just been added and there is no database instance of the master library. As a result, when operating the corresponding library, there is no corresponding instance or table or record on the slave side, which causes an error. This can only be solved specifically
  5. There is a problem with the value setting of the master and slave , just reconnect to the master MASTER_LOG_FILE/MASTER_LOG_POSon the slave nodeSTOP SLAVE;

7. Synchronize some database instances or tables

Add configuration on the master node [optional] (if you only want to read some instances from the library)

Add the following configuration to the my.cnf file

#需要同步的数据库名 有多个库添加多行即可
binlog-do-db=test
binlog-do-db=test1
#排除的数据库
binlog-ignore-db=sys

**salve side: **Add the following configuration to the my.cnf file, so that the salve will only read the configured db or table, and the master’s operations on other db will not affect the slave

#如果salve库名称与master库名相同,使用本配置 
replicate-do-db=test
#如果master库名[test]与salve库名[test001]不同,使用以下配置[需要做映射] 
#replicate-rewrite-db = test -> test001
#如果不是要全部同步[默认全部同步],则指定需要同步的表 
#replicate-wild-do-table=test.user
#replicate-wild-do-table=test.role

Guess you like

Origin blog.csdn.net/weixin_53641036/article/details/127080470