zoukankan      html  css  js  c++  java
  • Docker 部署 MySQL 8.0 主从集群

    部署 MySQL 8.0 集群 master & slave(仅测试用)

    镜像版本 mysql:8.0

    1、创建 overlay 网络

    docker network create --driver overlay common-network --attachable
    

    2、启动 2 个 MYSQL:mysql-master 、mysql-slave

    docker run -d 
    --name mysql-master 
    --network common-network 
    -e MYSQL_ROOT_PASSWORD=123456 
    -p 3306:3306 
    -d mysql --default-authentication-plugin=mysql_native_password
    
    docker run -d 
    --name mysql-slave 
    --network common-network 
    -e MYSQL_ROOT_PASSWORD=123456 
    -p 3307:3306 
    -d mysql --default-authentication-plugin=mysql_native_password
    

    3、配置 master & slave

    docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -p123456 
    -e "SET PERSIST server_id=1;" 
    -e "SET PERSIST_ONLY gtid_mode=ON;" 
    -e "SET PERSIST_ONLY enforce_gtid_consistency=true; " 
    -e "CREATE USER 'repl'@'%' IDENTIFIED BY 'password' REQUIRE SSL; " 
    -e "GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';"
    
    docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 
    -e "SET PERSIST server_id=2;" 
    -e "SET PERSIST_ONLY gtid_mode=ON;" 
    -e "SET PERSIST_ONLY enforce_gtid_consistency=true; "
    

    4、重启 master & slave

    docker restart mysql-master mysql-slave
    

    5、连接 master & slave

    docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 
    -e "CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_AUTO_POSITION=1, MASTER_SSL=1;" 
    -e "START SLAVE;" 
    

    6、验证 slave 状态

    docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 -e "show slave statusG"
    如下状态为正常:
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
    

    验证数据同步
    在 master 创建数据库 snail

    docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -p123456 
     -e "create database snail default character set utf8mb4 collate utf8mb4_general_ci;"
    

    在 slave 查看数据库列表

    docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 
     -e "show databases;"
    

    mysql: [Warning] Using a password on the command line interface can be insecure.
    +--------------------+
    | Database |
    +--------------------+
    | snail |
    | information_schema |
    | mysql |
    | performance_schema |
    | sys |

    参考

    Docker 部署 MySQL 5.7 & 8.0 主从集群

  • 相关阅读:
    6、linux中同步、互斥、阻塞(原子操作、信号量、阻塞)
    lightOJ-1199 Partitioning Game (SG函数)
    HDU-1013 Digital Roots
    HDU-1004 Let the Balloon Rise (STL map)
    HDU-1020 Encoding (字符串)
    POJ-2524 Ubiquitous Religions (并查集)
    POJ-1988 Cube Stacking (带权并查集)
    POJ-2236 Wireless Network (并查集)
    HDU-1002 A + B Problem II (模拟大数相加)
    HDU-1829 A Bug's Life (种类并查集)
  • 原文地址:https://www.cnblogs.com/snail-gao/p/14155118.html
Copyright © 2011-2022 走看看