docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker-compose 部署mariadb 主从复制

docker-compose 部署mariadb 主从复制的实现步骤

作者:玄德公笔记

本文主要介绍了在Docker中快速搭建MariaDB主从架构,包括配置主库、启动从库、执行连接指令,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧

虽然和mysql大差不差,但是差别就是要踩的坑。

1. 主库配置

services:
  mariadb-master:
    image: harbocto.xxx.com.cn/public/`mariadb:11.5.2
    container_name: mariadb-master
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: liubei#161
      MYSQL_DATABASE: xishu
      MYSQL_USER: repl
      MYSQL_PASSWORD: guanyu#160
    command:
      --server-id=1
      --log-bin=mysql-bin
      --binlog-format=row
      # --binlog-do-db=xishu #指定库,可选
    volumes:
      - ./data:/var/lib/mysql
docker-compose up -d
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
GRANT REPLICATION CLIENT ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;

2. 启动从库

services:
  mariadb-master:
    image: harbocto.xxx.com.cn/public/mariadb:11.5.2
    container_name: mariadb-slave
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: liubei#161
      MYSQL_DATABASE: xishu
      MYSQL_USER: repl
      MYSQL_PASSWORD: guanyu#160
    command:
      --server-id=1
      --log-bin=mysql-bin
      --binlog-format=row
    volumes:
      - ./data:/var/lib/mysql
docker-compose up -d

3. 主从配置

3.1 查看主库状态

docker exec -it mariadb-master mariadb -u root -pliubei#161
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      342 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

根据主库状态,拼接从库连接主库的命令

CHANGE MASTER TO
    MASTER_HOST='10.10.239.32',
    MASTER_PORT=3306,
    MASTER_USER='repl',
    MASTER_PASSWORD='guanyu#160',
    MASTER_LOG_FILE='mysql-bin.000002',
    MASTER_LOG_POS=342;

3.2 从库配置

docker exec -it mariadb-slave mariadb -u root -pliubei#161
CHANGE MASTER TO
    MASTER_HOST='10.10.xxx.32',
    MASTER_PORT=3306,
    MASTER_USER='repl',
    MASTER_PASSWORD='guanyu#160',
    MASTER_LOG_FILE='mysql-bin.000002',
    MASTER_LOG_POS=342;
START SLAVE;
SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
                Slave_IO_State: Waiting to reconnect after a failed registration on master
                   Master_Host: 10.10.xxx.32
                   Master_User: repl
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000002
           Read_Master_Log_Pos: 342
                Relay_Log_File: relay-bin.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File: mysql-bin.000002
              Slave_IO_Running: Connecting
             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: 342
               Relay_Log_Space: 256
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: Yes
            Master_SSL_CA_File:
            Master_SSL_CA_Path:
               Master_SSL_Cert:
             Master_SSL_Cipher:
                Master_SSL_Key:
         Seconds_Behind_Master: NULL
 Master_SSL_Verify_Server_Cert: Yes
                 Last_IO_Errno: 1597
                 Last_IO_Error: Master command COM_REGISTER_SLAVE failed: Access denied for user 'repl'@'%' (using password: YES) (Errno: 1045)
                Last_SQL_Errno: 0
                Last_SQL_Error:
   Replicate_Ignore_Server_Ids:
              Master_Server_Id: 1
                Master_SSL_Crl:
            Master_SSL_Crlpath:
                    Using_Gtid: No
                   Gtid_IO_Pos:
       Replicate_Do_Domain_Ids:
   Replicate_Ignore_Domain_Ids:
                 Parallel_Mode: optimistic
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
          Replicate_Rewrite_DB:
1 row in set (0.000 sec)

4. 测试

USE xishu;
CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100));
INSERT INTO users (name) VALUES ('liubei'), ('guanyu'), ('zhangfei');
MariaDB [(none)]> use xishu
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [xishu]> select * from users;
+----+----------+
| id | name     |
+----+----------+
|  1 | liubei   |
|  2 | guanyu   |
|  3 | zhangfei |
+----+----------+
3 rows in set (0.000 sec)

到此这篇关于docker-compose 部署mariadb 主从复制的实现步骤的文章就介绍到这了,更多相关docker-compose 部署mariadb 主从复制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文