学习记录: 在VMware上的CentOS7中, 通过docker完成mysql的主从复制

环境准备

VMware, 版本信息如下:
在这里插入图片描述
CentOS7信息如下:
在这里插入图片描述
docker版本信息如下:

[root@localhost ~]# docker version 
Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.45
 Go version:        go1.21.11
 Git commit:        5650f9b
 Built:             Wed Jun  5 11:32:04 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          26.1.4
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.11
  Git commit:       de5c9cf
  Built:            Wed Jun  5 11:31:02 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.33
  GitCommit:        d2d58213f83a351ca8f528a95fbd145f5654e957
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

mysql版本信息如下:

bash-5.1# mysql --version 
mysql  Ver 9.0.1 for Linux on x86_64 (MySQL Community Server - GPL)

正片开始

mysql主从复制,这里准备做一个一主一从, 来吧 展示.

主库配置

docker创建并启动主库mysql容器:

[root@localhost ~]# docker run -d --name mysql-master -v /var/mysql-master/data:/var/lib/mysql -v /var/mysql-master/config/my.cnf:/etc/my.cnf -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 mysql:latest
8d27c837037a787654c2306805b554d4228cfe967327af60c4fd29bd7b991cc1
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
8d27c837037a   mysql:latest   "docker-entrypoint.s…"   9 seconds ago   Up 8 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master

my.cnf配置文件内容:

[root@localhost config]# cat my.cnf
[mysqld]
server-id=1
read-only=0

进入mysql-master容器,进行主库相关配置:

# 进入容器
[root@localhost ~]# docker exec -it 8d27c837037a bash
# 登录mysql
bash-5.1# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 9.0.1 MySQL Community Server - GPL

Copyright (c) 2000, 2024, 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.

# 创建用户和密码, 分别是 'test' 和 'test@123456', %表示可以从任意主机登录
mysql> CREATE USER 'test'@'%' IDENTIFIED BY 'test@123456';
Query OK, 0 rows affected (0.02 sec)

# 为 'test'@'%' 用户分配主从复制权限
mysql> GRANT REPLICATION SLAVE ON *.* TO 'test'@'%';
Query OK, 0 rows affected (0.00 sec)

# 通过指令, 查看二进制日志坐标, ps: 老版本的指令是 show master status; 在新版本中使用会报错; 新版本的指令是: show binary log status; 查询后, 表中的File和Position将在从库中用到.
mysql> show binary log status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      691 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

从库配置

docker创建并启动从库mysql容器:

[root@localhost mysql-slave]# docker run -d --name mysql-slave -v /var/mysql-slave/data:/var/lib/mysql -v /var/mysql-slave/config/my.cnf:/etc/my.cnf -e MYSQL_ROOT_PASSWORD=123456 -p 3308:3306 mysql:latest
0a1cbb3e6f93f4df4f2ed3dbc6c3cb08db8cbe70fdbf7288e8a1c3f4d148ae35
[root@localhost mysql-slave]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS                                                  NAMES
0a1cbb3e6f93   mysql:latest   "docker-entrypoint.s…"   5 seconds ago    Up 4 seconds    33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
8d27c837037a   mysql:latest   "docker-entrypoint.s…"   19 minutes ago   Up 19 minutes   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master

my.cnf文件配置:

[root@localhost config]# cat my.cnf
[mysqld]
server-id=2
read-only=1

进入容器,进行从库配置:

# 进入容器
[root@localhost config]# docker exec -it 0a1cbb3e6f93 bash
# 登录
bash-5.1# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 9.0.1 MySQL Community Server - GPL

Copyright (c) 2000, 2024, 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> CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.34.131', SOURCE_USER='test', SOURCE_PASSWORD='test@123456', SOURCE_LOG_FILE='binlog.000002', SOURCE_LOG_POS=691, SOURCE_PORT=3307;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

# 开启同步
mysql> start replica;
Query OK, 0 rows affected (0.01 sec)

# 查看同步状态(ps: 只要看到Replica_IO_Running: Yes和Replica_SQL_Running: Yes就表示主从同步已经配置完成)
mysql> show replica status \G;
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 192.168.34.131
                  Source_User: test
                  Source_Port: 3307
                Connect_Retry: 60
              Source_Log_File: binlog.000002
          Read_Source_Log_Pos: 691
               Relay_Log_File: 0a1cbb3e6f93-relay-bin.000002
                Relay_Log_Pos: 325
        Relay_Source_Log_File: binlog.000002
           Replica_IO_Running: Yes
          Replica_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_Source_Log_Pos: 691
              Relay_Log_Space: 543
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Source_SSL_Allowed: No
           Source_SSL_CA_File:
           Source_SSL_CA_Path:
              Source_SSL_Cert:
            Source_SSL_Cipher:
               Source_SSL_Key:
        Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Source_Server_Id: 1
                  Source_UUID: 24d2ed53-860c-11ef-9c85-0242ac110002
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Source_Retry_Count: 10
                  Source_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Source_SSL_Crl:
           Source_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Source_TLS_Version:
       Source_public_key_path:
        Get_Source_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

测试

在主库上创建数据库, 创建表, 插入数据等操作, 然后查看从库会不会同步更新.
主库命令如下:

mysql> create database tb_user;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tb_user            |
+--------------------+
5 rows in set (0.00 sec)

mysql> use tb_user;
Database changed
mysql> create table user1(
    -> id int,
    -> name varchar(10)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-------------------+
| Tables_in_tb_user |
+-------------------+
| user1             |
+-------------------+
1 row in set (0.00 sec)

mysql> insert into user1 values(1, 'zxc');
Query OK, 1 row affected (0.01 sec)

mysql> insert into user1 values(2, 'liudehua');
Query OK, 1 row affected (0.01 sec)

mysql> select * from user1;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zxc      |
|    2 | liudehua |
+------+----------+
2 rows in set (0.00 sec)

从库查看数据:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tb_user            |
+--------------------+
5 rows in set (0.00 sec)

mysql> use tb_user;
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
mysql> show tables;
+-------------------+
| Tables_in_tb_user |
+-------------------+
| user1             |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from user1;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zxc      |
|    2 | liudehua |
+------+----------+
2 rows in set (0.00 sec)

搞定, 完成一主一从配置~~~~~~~~~~~~~~~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值