美文网首页
docker mysql集群

docker mysql集群

作者: xiaoqiaobian | 来源:发表于2022-05-22 16:35 被阅读0次

    目的使用docker安装mysql主从集群

    拉取镜像

    docker pull mysql:5.7.33

    1.创建存放映射文件夹

    mkdir -p mydata/mysql/master/log
    mkdir -p mydata/mysql/master/data
    mkdir -p mydata/mysql/master/conf

    启动

    docker run -p 3307:3306 --name mysql-master -v /mydata/mysql/master/log:/var/log/mysql -v /mydata/mysql/master/data:/var/lib/mysql -v /mydata/mysql/master/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.33
    docker run -p 3308:3306 --name mysql-slave1 -v /mydata/mysql/slave1/log:/var/log/mysql -v /mydata/mysql/slave1/data:/var/lib/mysql -v /mydata/mysql/slave1/conf:/etc/mysql --link mysql-master:master -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.33
    docker run -p 3309:3306 --name mysql-slave2 -v /mydata/mysql/slave2/log:/var/log/mysql -v /mydata/mysql/slave2/data:/var/lib/mysql -v /mydata/mysql/slave2/conf:/etc/mysql --link mysql-master:master -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.33

    映射文件夹修改配置
    vi /mydata/mysql/conf/my.conf

    下面是my.conf配置文件内容
    [client]
    default-character-set=utf8
    [mysql]
    default-character-set=utf8
    [mysqld]
    init_connect='SET collation_connection = utf8_unicode_ci'
    init_connect='SET NAMES utf8'
    character-set-server=utf8
    collation-server=utf8_unicode_ci
    skip-character-set-client-handshake
    skip-name-resolve

    再修改my.cnf配置
    vi /mydata/mysql/conf/my.cnf

    下面是my.cnf配置文件内容
    [mysqld]
    log-bin=/var/lib/mysql/mysql-bin
    server-id=1001
    expire_logs_days = 30

    重启容器

    docker restart mysql

    进入容器

    docker exec -it mysql bash

    登录mysql

    mysql -u root -p
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'Lens2022';

    授权同步账号

    grant replication slave on . to 'upstream'@'%' identified by '123456';
    flush privileges;

    查看状态

    show variables like '%log_bin%';
    show master status\G;

    从数据库执行

    stop slave;
    change master to master_host='master', master_user='slave', master_password='123456', master_port=3307, master_log_file='mysql-bin.000001', master_log_pos=154, master_connect_retry=30;
    start slave;
    show slave status\G;

    相关文章

      网友评论

          本文标题:docker mysql集群

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