美文网首页
Docker中MySQL主从数据库+读写分离

Docker中MySQL主从数据库+读写分离

作者: MissJin_git | 来源:发表于2018-12-20 22:33 被阅读0次
    1. 作者:missjin
    2. 交流WX:hcj6932600

    预期目标图

    C17AE36D-D565-4164-B63B-CEE0B5031B52.jpg

    实现后的效果图


    ****【master-mysql 创建了一个数据库】****


    9E6959EA-5336-4D0F-B179-25F7B939D620.png

    ****【slaver-mysql 的数据库】****


    DDBFFC1C-5F53-4F45-8375-3CC1128B6C9F.png AF5F874C-5C2C-48DF-AD44-92BE7230D0E3.png

    【1】docker服务启动

    systemctl restart docker
    # 验证启动
    docker version
    # 拉取指定镜像
    docker pull mysql:5.7.24
    
    

    【2】先建立好挂载目录的配置文件

    # 建主数据的配置文件和数据存放目录
    mkdir -p /data/mysql/master-mysql/data 
    touch /data/mysql/master-mysql/mysql.cnf
    
    # 建从数据的配置文件和数据存放目录
    mkdir -p /data/mysql/slaver-mysql/data 
    touch /data/mysql/slaver-mysql/mysql.cnf
    
    # ----拷贝原始的配置文件 my.cnf--------------------
    # Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; version 2 of the License.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
    
    !includedir /etc/mysql/conf.d/
    !includedir /etc/mysql/mysql.conf.d/
    #vi /etc/mysql/my.cnf
    [mysqld]
    # [必须]启用二进制日志
    log-bin=mysql-bin
    # [必须]服务器唯一ID master=140;slaver=130
    server-id=140
    

    【3】创建容器

    # 创建master-mysql
    docker run --name master-mysql -v /data/mysql/master-mysql/mysql.cnf:/etc/mysql/mysql.cnf -v /data/mysql/master-mysql/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=missjin -d mysql:5.7.24 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    
    # 创建slaver-mysql
    docker run --name slaver-mysql -v /data/mysql/slaver-mysql/mysql.cnf:/etc/mysql/mysql.cnf -v /data/mysql/slaver-mysql/data:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=missjin -d mysql:5.7.24 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    
    # 查看启动状态
    docker ps
    
    # 查看容器内网分配的IP
    docker inspect master-mysql | grep IP #172.17.0.2
    docker inspect slaver-mysql | grep IP #172.17.0.3
    

    【4】进入容器master-mysql

    # 进入master-mysql
    docker exec -it master-mysql /bin/bash
    

    【5】Master配置[docker里面挂载目录,改配置]

    #vi /etc/mysql/my.cnf
    [mysqld]
    # [必须]启用二进制日志
    log-bin=mysql-bin
    # [必须]服务器唯一ID,默认是1,一般取IP最后一段
    server-id=140       
    
    # 登入服务
    mysql -uroot -pmissjin
    
    # 授权给slaver-mysql访问的权限
    mysql>grant replication slave on *.* to 'reader'@'%' identified by '123456';
    
    # 一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如172.17.0.2,加强安全。
    
    # 记录master数据的状态,一配置从数据库
    show master status;
    

    【6】进入slaver-mysql,配置从数据库

    # 进入slaver-mysql
    docker exec -it slaver-mysql /bin/bash
    
    # 进入数据库
    mysql -uroot -p
    
    # 改变同步
    change master to master_host='172.17.0.2',master_user='reader',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=439;
    
    # 启用从数据库
    start slave;
    

    【7】验证效果

    ****【master-mysql 创建了一个数据库】****


    9E6959EA-5336-4D0F-B179-25F7B939D620.png

    ****【slaver-mysql 的数据库】****


    DDBFFC1C-5F53-4F45-8375-3CC1128B6C9F.png AF5F874C-5C2C-48DF-AD44-92BE7230D0E3.png

    相关文章

      网友评论

          本文标题:Docker中MySQL主从数据库+读写分离

          本文链接:https://www.haomeiwen.com/subject/npxpkqtx.html