docker高级——mysql主从复制

新建master实例

[root@localhost ~]# 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
d492fa0e47453dc4868728dc005b5aee251e1758b2fee2c4ca411393c2608a35
[root@localhost ~]# cd /mydata/mysql-master/conf/
[root@localhost conf]# vi my.cnf
[root@localhost conf]# cat 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
[root@localhost conf]# 
[root@localhost conf]# docker restart mysql-master
mysql-master
[root@localhost conf]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS                                                  NAMES
d492fa0e4745   mysql:5.7     "docker-entrypoint.s…"   5 minutes ago       Up 12 seconds      33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
2c0405e33fd9   redis:6.0.8   "docker-entrypoint.s…"   43 minutes ago      Up 37 minutes      0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              myredis
7e477af1061c   mysql:5.7     "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
f5bbe411a1ed   registry      "/entrypoint.sh /etc…"   5 hours ago         Up 5 hours         0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost conf]# docker exec -it mysql-master bash
root@d492fa0e4745:/# mysql -uroot -proot
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 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> CREATE USER 'slave'@'%'IDENTIFIED BY '123456'; 
Query OK, 0 rows affected (0.16 sec)
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%'; 
Query OK, 0 rows affected (0.01 sec)

mysql>

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

新建slave实例

[root@localhost redis]# docker run -p 3308: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=root \
> -d mysql:5.7
8cc3572385e4ab234afb30df90b4dd1f7b25a631d35afa0e67e88081833a2ba6
[root@localhost redis]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS                                                  NAMES
8cc3572385e4   mysql:5.7     "docker-entrypoint.s…"   20 seconds ago      Up 11 seconds      33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
d492fa0e4745   mysql:5.7     "docker-entrypoint.s…"   12 minutes ago      Up 7 minutes       33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
2c0405e33fd9   redis:6.0.8   "docker-entrypoint.s…"   50 minutes ago      Up 44 minutes      0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              myredis
7e477af1061c   mysql:5.7     "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
f5bbe411a1ed   registry      "/entrypoint.sh /etc…"   5 hours ago         Up 5 hours         0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost redis]# cd /mydata/mysql-slave/conf/
[root@localhost conf]# vi my.cnf
[root@localhost conf]# docker restart mysql-slave
mysql-slave
[root@localhost conf]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS                                                  NAMES
8cc3572385e4   mysql:5.7     "docker-entrypoint.s…"   7 minutes ago       Up 7 seconds       33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
d492fa0e4745   mysql:5.7     "docker-entrypoint.s…"   20 minutes ago      Up 14 minutes      33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
2c0405e33fd9   redis:6.0.8   "docker-entrypoint.s…"   57 minutes ago      Up 52 minutes      0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              myredis
7e477af1061c   mysql:5.7     "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
f5bbe411a1ed   registry      "/entrypoint.sh /etc…"   5 hours ago         Up 5 hours         0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost conf]# cat  my.cnf
[mysqld]
## 设置server id,同一局域网中需要唯
server_id=102
# 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
log-bin=mall-mysgl-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
[root@localhost conf]#

在从数据库中配置主从复制
docker高级——mysql主从复制_第1张图片

change master to
master_host=‘192.168.253.128’,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.253.128',master_user='slave',master_password='123456',master_port=3307,master_log_file='mall-mysql-bin.000001',master_log_pos=617,master_connect_retry=30; 
Query OK, 0 rows affected, 2 warnings (0.23 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.253.128
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 30
              Master_Log_File: mall-mysql-bin.000001
          Read_Master_Log_Pos: 617
               Relay_Log_File: mall-mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mall-mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 617
              Relay_Log_Space: 154
              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: NULL
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: 0
                  Master_UUID: 
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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>
##开启主从同步
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.253.128
                  Master_User: slave
                  Master_Port: 3307
                Connect_Retry: 30
              Master_Log_File: mall-mysql-bin.000001
          Read_Master_Log_Pos: 617
               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: 617
              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: ab8c77b2-d944-11ed-9f7d-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> 

主库

mysql> create table userinfo (id int primary key auto_increment NOT NULL,username varchar(20),tel varchar(20));                       
Query OK, 0 rows affected (0.11 sec)

mysql> insert into userinfo values (0,'jl2','17854289303');
Query OK, 1 row affected (0.07 sec)

mysql> insert into userinfo values (0,'','17854289303');
Query OK, 1 row affected (0.01 sec)

mysql> select * from userinfo;
+----+----------+-------------+
| id | username | tel         |
+----+----------+-------------+
|  1 | jl2      | 17854289303 |
|  2 |          | 17854289303 |
+----+----------+-------------+
2 rows in set (0.04 sec)

mysql> 

查看从库

mysql> use db1;
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> select * from userinfo;
+----+----------+-------------+
| id | username | tel         |
+----+----------+-------------+
|  1 | jl2      | 17854289303 |
|  2 |          | 17854289303 |
+----+----------+-------------+
2 rows in set (0.04 sec)

mysql> 

你可能感兴趣的:(docker,mysql,docker,数据库)