Docker搭建MySQL主从复制

2020-04-04 16:09:34来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Docker搭建MySQL主从复制

Docker搭建MySQL主从复制

  1. 主从服务器上分别安装Docker

    1.1 Docker 要求 CentOS 系统的内核版本高于 3.10

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

    1.2 确保 yum 包更新到最新。

    [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 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是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 设置yum源

    [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 安装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 启动docker并设置docker自动启动

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

    1.7 检测docker是否安装成功

    [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更换国内阿里仓库,并下载安装MySQL

    2.1 docker更换国内阿里仓库

    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 安装MySQL

    ? 2.2.1 下载MySQL

    [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
    

    ? 2.2.2 查看MySQL镜像

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

    ? 2.2.3 创建MySQL的挂载文件目录(日志、数据、配置)

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

    ? 2.2.4 先启动容器(为了复制配置文件)(这种做法显得多余,但是Docker不是很精通的我没想到更好的办法,请各位大神指导)

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

    ? 2.2.5 复制配置文件

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

    ? 2.2.6 停止并删容器

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

    ? 2.2.7 重新启动容器(加上挂载)

    [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查询现有字符集,结果都是utf8mb4

    show variables like '%char%';
    

    ? 2.2.9 在挂载的配置文件更改字符集

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

    ? 2.2.10 重启docker容器,并查看字符集已经更改,证明挂载的配置文件有效

file

  1. 搭建主从复制数据库

    3.1 配置主Master库

    ? 3.1.1 进入主库挂载配置文件my.cnf,加入以下配置并重启docker容器

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

    ? 3.1.2 在主Master库执行语句

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

    ? 为防止后面出现报错[Authentication plugin 'caching_sha2_password' cannot be loaded],执行下面语句

    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';
    

    最后执行语句获取File, Position

    show master status;
    

    3.2 配置从Slave库

    ? 3.2.1 进入从库挂载配置文件my.cnf,加入以下配置并重启docker容器

    [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 链接主从库,在从库执行语句

    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的地址

    master_port:Master的端口号,指的是容器的端口号

    master_user:用于数据同步的用户

    master_password:用于同步的用户的密码

    master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值

    master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值

    master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒

    ? 3.2.3 查看主从同步状态

    show slave status ;
    

file

? 正常情况下,SlaveIORunning 和 SlaveSQLRunning 都是No,因为我们还没有开启主从复制过程。

  3.2.4 开启主从复制
start slave;

重新查看主从同步状态, 这时SlaveIORunning 和 SlaveSQLRunning 都是Yes,如果你的SlaveIORunning 和 SlaveSQLRunning 其中一个是Connecting或者No,就证明配置有错,查看Last_IO_Error报错的详细信息

通过下面命令,删除已经配置的主从链接信息,重新进行配置

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

原文链接:https://www.cnblogs.com/undefined22/p/12635237.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:eclipse 安装spring插件及spring无法使用

下一篇:java 列表迭代器