美文网首页dockeralreadymysql
docker mysql主从安装

docker mysql主从安装

作者: wudl | 来源:发表于2022-01-15 22:36 被阅读0次

    1. 首先拉去镜像

    docker pull mysql:5.7

    2. 运行镜像

    主要将日志 存储文件, 配置文件 映射到主机上

    docker run -p 13307: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=123456 \-d mysql:5.7
    
    [root@basenode ~]# docker run -p 13307: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=123456 \-d mysql:5.7
    76176e5ae29317ea207080be62aeffadaada4c82c03753d2c500916449d6476e
    [root@basenode ~]# 
    
    
    

    3. 主节点创建配置文件并且添加内容如下

    1. 目录:
    /mydata/mysql-master/cnf
    [root@basenode conf]# vi my.cnf
    
    [mysqld]
    ## 设置server_id,同一局域网中需要唯一
    server_id=101
    ## 指定不需要同步的数据库名称
    binlog-ignore-db=mysql
    ## 开启二进制日志功能
    log-bin=mall-mysql-bin
    ## 设置二进制日志使用内存大小(事务)
    binlog_cache_size=1M
    ## 设置使用的二进制日志格式(mixed,statement,row)
    binlog_format=mixed
    ## 二进制日志过期清理时间。默认值为0,表示不自动清理。
    expire_logs_days=7
    ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
    ## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
    slave_skip_errors=1062
    
    

    4. 添加完配置后重启mysql

    [root@basenode conf]# docker restart mysql-master
    mysql-master
    [root@basenode conf]# 
    

    5. 进入到mysql 容器内部

    # 进入到容器
    docker exec -it mysql-master /bin/bash
    # 登录mysql
    mysql -uroot -p123456
    

    6. 创建同步用户

    6.1 创建用户命令:

    mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> 
    
    

    7. 创建从服务器

    [root@basenode ~]# docker run -p 13308:3306 --name mysql-slave \
    > -v /mydata/mysql-slave/log:/var/log/mysql \
    > -v /mydata/mysql-slave/data:/var/lib/mysql \
    > -v /mydata/mysql-slave/conf:/etc/mysql \
    > -e MYSQL_ROOT_PASSWORD=123456  \
    > -d mysql:5.7
    230880d2ab8c53351656ceb3998e28595dcd5af28879de3755cf6c6993b0879d
    [root@basenode ~]# 
    
    

    8. 从节点创建配置文件并且添加内容如下

    [root@basenode ~]# cd /mydata/mysql-slave/cnf
    [root@basenode conf]# vi my.cnf
    [mysqld]
    ## 设置server_id,同一局域网中需要唯一
    server_id=102
    ## 指定不需要同步的数据库名称
    binlog-ignore-db=mysql
    ## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
    log-bin=mall-mysql-slave1-bin
    ## 设置二进制日志使用内存大小(事务)
    binlog_cache_size=1M
    ## 设置使用的二进制日志格式(mixed,statement,row)
    binlog_format=mixed
    ## 二进制日志过期清理时间。默认值为0,表示不自动清理。
    expire_logs_days=7
    ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
    ## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
    slave_skip_errors=1062
    ## relay_log配置中继日志
    relay_log=mall-mysql-relay-bin
    ## log_slave_updates表示slave将复制事件写进自己的二进制日志
    log_slave_updates=1
    ## slave设置为只读(具有super权限的用户除外)
    read_only=1
    
    

    9. 重启从节点

    [root@basenode conf]# docker restart mysql-slave
    mysql-slave
    [root@basenode conf]# 
    
    

    10.在主数据库中查看主从同步状态

    mysql> show master status;
    +-----------------------+----------+--------------+------------------+-------------------+
    | File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +-----------------------+----------+--------------+------------------+-------------------+
    | mall-mysql-bin.000001 |      154 |              | mysql            |                   |
    +-----------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    mysql> 
    

    11. 进入从数据库

    [root@basenode conf]# docker  exec -it mysql-slave /bin/bash
    root@230880d2ab8c:/# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.36-log MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql> 
    
    

    12. 在从节点上执行同步命令

    change master to master_host='宿主机ip', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;

    mysql> change master to master_host='192.168.1.180', master_user='slave', master_password='123456', master_port=13307, master_log_file='mall-mysql-bin.000001', master_log_pos=154, master_connect_retry=30;  
    Query OK, 0 rows affected, 2 warnings (0.00 sec)
    
    mysql> 
    
    

    13. 在从节点上开启主从同步

    mysql> start slave;
    
    

    14 查看 从节点的状态

    mysql> show slave status \G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.180
                      Master_User: slave
                      Master_Port: 13307
                    Connect_Retry: 30
                  Master_Log_File: mall-mysql-bin.000001
              Read_Master_Log_Pos: 154
                   Relay_Log_File: mall-mysql-relay-bin.000002
                    Relay_Log_Pos: 325
            Relay_Master_Log_File: mall-mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 0
                       Last_Error: 
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 154
                  Relay_Log_Space: 537
                  Until_Condition: None
                   Until_Log_File: 
                    Until_Log_Pos: 0
               Master_SSL_Allowed: No
               Master_SSL_CA_File: 
               Master_SSL_CA_Path: 
                  Master_SSL_Cert: 
                Master_SSL_Cipher: 
                   Master_SSL_Key: 
            Seconds_Behind_Master: 0
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 101
                      Master_UUID: 9eedd539-7601-11ec-93ac-0242ac110005
                 Master_Info_File: /var/lib/mysql/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
    1 row in set (0.00 sec)
    
    ERROR: 
    No query specified
    
    mysql> 
    
    

    主要出现 两个都位yes 就表示成功
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

    15 测试成功

    docker-mysql 主从.png

    相关文章

      网友评论

        本文标题:docker mysql主从安装

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