docker中mysql配置主从

创建文件夹&编辑my.cnf内容

mkdir -p /opt/docker/mysql-8.0/master/cnf
mkdir -p /opt/docker/mysql-8.0/master/data
vim /opt/docker/mysql-8.0/master/cnf/mysql.cnf

[mysqld]
## 设置server_id,注意要唯一
server-id=1
## 开启binlog
log-bin=mysql-bin
## binlog缓存
binlog_cache_size=1M
## binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed


mkdir /opt/docker/mysql-8.0/slave1/cnf -p
mkdir /opt/docker/mysql-8.0/slave1/data -p
vim /opt/docker/mysql-8.0/slave1/cnf/mysql.cnf

[mysqld]
## 设置server_id,注意要唯一
server-id=2
## 开启binlog,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
## 如果需要同步函数或者存储过程
log_bin_trust_function_creators=true
## binlog缓存
binlog_cache_size=1M
## binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062


mkdir /opt/docker/mysql-8.0/slave2/cnf -p
mkdir /opt/docker/mysql-8.0/slave2/data -p
vim /opt/docker/mysql-8.0/slave2/cnf/mysql.cnf

[mysqld]
## 设置server_id,注意要唯一
server-id=3
## 开启binlog,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
## 如果需要同步函数或者存储过程
log_bin_trust_function_creators=true
## binlog缓存
binlog_cache_size=1M
## binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062

启动docker容器,本次版本为8.0

docker run -itd -p 3101:3306 --name master -v /opt/docker/mysql-8.0/master/cnf:/etc/mysql/conf.d \
     -v /opt/docker/mysql-8.0/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0
	 
docker run -itd -p 3102:3306 --name slaver -v /opt/docker/mysql-8.0/slave1/cnf:/etc/mysql/conf.d \
     -v /opt/docker/mysql-8.0/slave1/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0
	 
docker run -itd -p 3103:3306 --name slaver1 -v /opt/docker/mysql-8.0/slave2/cnf:/etc/mysql/conf.d \
     -v /opt/docker/mysql-8.0/slave2/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0
首先主服务器上查看master_log_file、master_log_pos两个参数,然后切换到从服务器上进行主服务器的连接信息的设置
[root@128 /]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d1719ff15b21   postgres:latest   "docker-entrypoint.s…"   44 minutes ago   Up 43 minutes   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp              postgres
b4e746e59cfc   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3103->3306/tcp, :::3103->3306/tcp   slaver1
59d0ba2f9ba1   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3102->3306/tcp, :::3102->3306/tcp   slaver
fce67889be45   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3101->3306/tcp, :::3101->3306/tcp   master
[root@128 /]# docker exec -it fce67889be45 bash
bash-4.4# 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 25
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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 master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |   197972 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

  查看master ipaddess

[root@128 /]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d1719ff15b21   postgres:latest   "docker-entrypoint.s…"   45 minutes ago   Up 45 minutes   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp              postgres
b4e746e59cfc   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3103->3306/tcp, :::3103->3306/tcp   slaver1
59d0ba2f9ba1   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3102->3306/tcp, :::3102->3306/tcp   slaver
fce67889be45   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3101->3306/tcp, :::3101->3306/tcp   master
[root@128 /]# docker inspect --format='{{.NetworkSettings.IPAddress}}' master
172.17.0.3

 配置从slave:

[root@128 /]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d1719ff15b21   postgres:latest   "docker-entrypoint.s…"   46 minutes ago   Up 46 minutes   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp              postgres
b4e746e59cfc   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3103->3306/tcp, :::3103->3306/tcp   slaver1
59d0ba2f9ba1   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3102->3306/tcp, :::3102->3306/tcp   slaver
fce67889be45   mysql:8.0         "docker-entrypoint.s…"   18 hours ago     Up 18 hours     33060/tcp, 0.0.0.0:3101->3306/tcp, :::3101->3306/tcp   master
[root@128 /]# docker exec -it 59d0ba2f9ba1 bash
bash-4.4# 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 22
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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 master to master_host='172.17.0.3',master_user='root',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=197972 ;

 

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (4.63 sec)

mysql> stare slave;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'stare slave' at line 1
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.86 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.17.0.3
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 197972
               Relay_Log_File: edu-mysql-relay-bin.000003
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000003
             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: 197972
              Relay_Log_Space: 198524
              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: 1
                  Master_UUID: 2c7d7555-81d7-11ed-80f1-0242ac110003
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0.02 sec)

  查看其中的参数

Slave_IO_Running: Yes
Slave_SQL_Running: Yes  

则显示为配置成功

 
 
 
posted @ 2022-12-23 11:12  diligently  阅读(194)  评论(0)    收藏  举报