1. Preparation for construction

  1. To build an environment, you need to have a basic understanding of Docker and skillfully use the basic commands of Docker. If you are not familiar with it, you can take the basic knowledge and basic commands of Docker for a preliminary study
  2. Version Description: Docker19.0, mysql8.0, the configuration between different versions may be different

Two, start building

  1. Service building

    • Find the mysq8.0 image from the Docker repo and pull it to the container

      Docker search docker pull image name :tag Pull imageCopy the code
    • Create a mysql configuration folder in a familiar location to store the mysql configuration. Create a master and slave folder in this directory to configure the master and slave data information respectively

      #Master service configuration directory
      /home/mysql/master
      #From the service configuration directory
      /home/mysql/slave
      Copy the code
    • Create a data folder (master/slave) mysqld. CNF file (slave/master/slave)

      Data (folder) mysqld.cnf (file)Copy the code
    • Mysqld. CNF configuration information

      1. The master configuration

        [mysqld]
        pid-file=/var/run/mysqld/mysqld.pid
        socket=/var/run/mysqld/mysqld.sock
        datadir=/var/lib/mysql
        secure_file_priv=/var/lib/mysql
        #Lower_case_table_names=1
        symbolic-links=0
        #The primary and secondary service ids must be different
        server-id=1
        #Enable the log binlog function
        log-bin=mysql-bin
        #Libraries that do not require master/slave replication
        binlog-ignore-db=mysql
        binlog-ignore-db=information_schema
        binlog-ignore-db=performance_schema
        #The library that requires binlog primary/secondary replication
        binlog-do-db=db-master
        Copy the code
      2. Slave configuration

        [mysqld]
        pid-file=/var/run/mysqld/mysqld.pid
        socket=/var/run/mysqld/mysqld.sock
        datadir=/var/lib/mysql
        secure_file_priv=/var/lib/mysql
        #Lower_case_table_names=1
        symbolic-links=0
        #The primary and secondary service ids must be different
        server-id=2
        #Enabling log Files
        log-bin=mysql-bin
        #A database that does not require binlog logging
        replicate_wild_ignore_table=mysql.%
        replicate_wild_ignore_table=information_schema.%
        replicate_wild_ignore_table=performance_schema.%
        #The database for binlog logging
        replicate_wild_do_table==db-master.%
        Copy the code
    • Start the Master and SLVE containers

      1. The master boot

        docker run -it -d --name mysql-master -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v /home/mysql/master/mysqld.cnf:/etc/mysql/my.cnf -v /home/mysql/master/data:/var/lib/mysql mysql
        Copy the code
      2. Slave to start

        docker run -it -d --name mysql-slave -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -v /home/mysql/slave/mysqld.cnf:/etc/mysql/my.cnf -v /home/mysql/slave/data:/var/lib/mysql mysql
        Copy the code
    • Problem specification

      Docker logs mysql-master(mysql-master) docker logs mysql-master(mysql-master) At this point, we can connect to our own mysql server through the client, and there may be various problems when connecting. Generally, turning off the firewall can solve most of the problems. If you encounter the problem of rejecting the client connection, docker basic knowledge has done a detailed processing can be moved to view.

  2. Setting up the master/slave relationship

    • Service set up, we need to further set up their master-slave relationship, can choose in the server configuration, for convenience, can also be configured on the client, for the convenience of good-looking, we choose to be configured on the client, the following command you just need to enter the SQL commands on the client, no longer need to perform dokcer command

    • Viewing master server information (Master service execution)

      show master status;
      Copy the code

      Mysql-bin is the master-slave’s binlog, which is also a key file for master/slave replication

    • Slave library association (executed from the server)

      change master to master_host={your primary database IP},master_port={primary database port},master_user={username},master_password=Password {}, master_log_file='mysql-bin.000004',master_log_pos=156;
      ​
      #mysql-bin000004.Log information about the primary and secondary replicationCopy the code
    • Restart slave to view the setting status

      showslave status; # slave_iq_running = yes If slave_sql_running fornoIf yes, run the following commandsql
      stop slave; 
      SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; 
      start slave;  
      Copy the code

    • At this point, the master-slave relationship of mysql is set up. The test only needs to create a database on the master server to observe whether the slave server is synchronized