Docker build MySQL master-slave replication

Docker build MySQL master-slave replication

  1. Master and slave are respectively installed on the server Docker

    1.1 Docker requirements CentOS system kernel version is higher than 3.10

    [root@localhost ~]# uname -r
    3.10.0-693.el7.x86_64
    

    1.2 ensure that the yum package up to date.

    [root@localhost ~]# sudo yum update
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.cqu.edu.cn
     * extras: mirrors.cqu.edu.cn
     * updates: mirrors.cqu.edu.cn
    base                                                                                                                                             | 3.6 kB  00:00:00     
    docker-ce-stable                                                                                                                                 | 3.5 kB  00:00:00     
    extras                                                                                                                                           | 2.9 kB  00:00:00     
    updates                                                                                                                                          | 2.9 kB  00:00:00
    

    1.3 installation package needs, yum-util provide yum-config-manager function, the other two are driven dependent devicemapper

    [root@localhost ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.cqu.edu.cn
     * extras: mirrors.cqu.edu.cn
     * updates: mirrors.cqu.edu.cn
    Package yum-utils-1.1.31-52.el7.noarch already installed and latest version
    Package device-mapper-persistent-data-0.8.5-1.el7.x86_64 already installed and latest version
    Package 7:lvm2-2.02.185-2.el7_7.2.x86_64 already installed and latest version
    Nothing to do
    

    1.4 Setting yum source

    [root@localhost ~]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    Loaded plugins: fastestmirror, langpacks
    adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
    grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
    repo saved to /etc/yum.repos.d/docker-ce.repo
    

    1.5 Installation docker

    [root@localhost ~]#  sudo yum install docker-ce
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.cqu.edu.cn
     * extras: mirrors.cqu.edu.cn
     * updates: mirrors.cqu.edu.cn
    Package 3:docker-ce-19.03.8-3.el7.x86_64 already installed and latest version
    Nothing to do
    

    1.6 Start docker docker and set to start automatically

    [root@localhost ~]# sudo systemctl start docker
    [root@localhost ~]# sudo systemctl enable docker
    

    1.7 docker detect whether the installation is successful

    [root@localhost ~]# docker version
    Client: Docker Engine - Community
     Version:           19.03.8
     API version:       1.40
     Go version:        go1.12.17
     Git commit:        afacb8b
     Built:             Wed Mar 11 01:27:04 2020
     OS/Arch:           linux/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          19.03.8
      API version:      1.40 (minimum version 1.12)
      Go version:       go1.12.17
      Git commit:       afacb8b
      Built:            Wed Mar 11 01:25:42 2020
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.2.13
      GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
     runc:
      Version:          1.0.0-rc10
      GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
     docker-init:
      Version:          0.18.0
      GitCommit:        fec3683
    
    
  2. docker replace domestic Ali warehouse, download and install MySQL

    2.1 docker replace domestic Ali warehouse

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://rdwyjupq.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    

    2.2 Installing MySQL

    MySQL 2.2.1 download

    [root@localhost ~]# docker pull mysql
    Using default tag: latest
    latest: Pulling from library/mysql
    c499e6d256d6: Pull complete 
    22c4cdf4ea75: Pull complete 
    6ff5091a5a30: Pull complete 
    2fd3d1af9403: Pull complete 
    0d9d26127d1d: Pull complete 
    54a67d4e7579: Pull complete 
    fe989230d866: Pull complete 
    3a808704d40c: Pull complete 
    826517d07519: Pull complete 
    69cd125db928: Pull complete 
    b5c43b8c2879: Pull complete 
    1811572b5ea5: Pull complete 
    Digest: sha256:b69d0b62d02ee1eba8c7aeb32eba1bb678b6cfa4ccfb211a5d7931c7755dc4a8
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest
    

    MySQL 2.2.2 View Mirror

    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    mysql               latest              9228ee8bac7a        4 days ago          547MB
    

    2.2.3 Creating MySQL mount file directory (log data, configuration)

    [root@localhost ~]# mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
    

    2.2.4 to start the container (To copy the configuration file) (This practice is redundant, but not very proficient Docker I did not think of a better way to please the god guide)

    [root@localhost conf]# docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD="123456" mysql
    05d161e69f3b2e3e4ebaa50f822934599a31e7ade330e1440a68aec9e404f7ae
    

    2.2.5 Copy Profile

    [root@localhost conf]# docker cp mysql:/etc/mysql/my.cnf /root/mysql/conf/
    

    2.2.6 Stop and delete container

    [root@localhost conf]# docker stop mysql
    mysql
    [root@localhost conf]# docker rm mysql
    mysql
    

    2.2.7 Restart container (plus mount)

    [root@localhost conf]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --mount type=bind,src=/root/mysql/conf/my.cnf,dst=/etc/mysql/my.cnf --mount ty
    pe=bind,src=/root/mysql/data,dst=/var/lib/mysql --mount type=bind,src=/root/mysql/logs,dst=/logs --restart=on-failure:3 -d mysql64824c7e84ceb3513b65a375c55f2c53bc653f95b119008296122b13ff632ba1
    

    2.2.8 MySQL query into the existing character set, the result is utf8mb4

    show variables like '%char%';
    

    2.2.9 change the character set in the mounted configuration file

    character-set-server=utf8
    
    default-character-set=utf8
    
    default-character-set=utf8
    

    2.2.10 restart docker container, and check the character set has changed, proven mount configuration file is valid

file

  1. Build a master-slave replication database

    3.1 Master Configuration main library

    3.1.1 mount into the main library configuration file my.cnf, add the following configuration and restart docker container

    [mysqld]
    ## 同一局域网内注意要唯一
    server-id=100  
    ## 开启二进制日志功能,可以随便取(关键)
    log-bin=mysql-bin
    

    3.1.2 execute the statement in the main Master Library

    CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
    GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
    

    To prevent given later [Authentication plugin 'caching_sha2_password' can not be loaded], the following statement is executed

    ALTER USER 'slave'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;   #修改加密规则 
    ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'password';   #更新一下用户的密码 
    FLUSH PRIVILEGES;
    alter user 'slave'@'%' identified by '123456';
    

    The last execution statement to obtain File, Position

    show master status;
    

    3.2 Slave configuration from the library

    3.2.1 from the library into the mount configuration file my.cnf, add the following configuration and restart docker container

    [mysqld]
    ## 设置server_id,注意要唯一
    server-id=101  
    ## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
    log-bin=mysql-slave-bin   
    ## relay_log配置中继日志
    relay_log=edu-mysql-relay-bin  
    

    3.2.2 The main link from the library, from the library in the statement is executed

    change master to master_host='192.168.47.128', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 5938, master_connect_retry=30;
    

    MASTER_HOST : Master Address

    MASTER_PORT : Master port number, port number refers to a container

    MASTER_USER : user data for synchronization

    master_password : used to synchronize the user's password

    MASTER_LOG_FILE : Specifies Slave start copying data from which log file, i.e., File field value in the above-mentioned

    master_log_pos value from which to start reading Position, Position i.e. the above-mentioned fields:

    master_connect_retry : If the connection fails, retry interval, in seconds, default is 60 seconds

    3.2.3 View master-slave synchronization status

    show slave status ;
    

file

Under normal circumstances, SlaveIORunning and SlaveSQLRunning are No, because we have not turned on the master-slave replication process.

  3.2.4 开启主从复制
start slave;

Revisit master-slave synchronization state, and then SlaveIORunning SlaveSQLRunning are Yes, if your SlaveIORunning and SlaveSQLRunning one of which is Connecting or No, to prove wrong configuration, view detailed information being given Last_IO_Error

The main configuration has been deleted from the link information, re-configured in the following command

stop slave;
reset master;
	 3.2.5 测试主从复制,最简单的方式是在主库建立个新库,这时,从库会自动同步

Guess you like

Origin www.cnblogs.com/undefined22/p/12635237.html