首页 > 数据库 >docker中mysql配置主从

docker中mysql配置主从

时间:2022-12-23 11:22:40浏览次数:40  
标签:8.0 0.0 tcp Master mysql docker 主从

创建文件夹&编辑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  

则显示为配置成功

     

标签:8.0,0.0,tcp,Master,mysql,docker,主从
From: https://www.cnblogs.com/luo12828-foxmail/p/17000253.html

相关文章

  • docker 安装 postgres 15.1
    docker拉取镜像:dockerpullpostgres:15.1创建文件夹,以及启动images创建文件夹:mkdir-p/opt/docker/postgresdockerrun--namepostgres\-ePOSTGRES_PASS......
  • Dockerfile介绍及常用保留指令
    从本文开始,咱们将介绍docker的另外一个技术点:dockerfile.我们来看看DockerFile相关的知识点,我们将怎么学习?1:DockerFile是什么?2:DockerFile构建过程解析3:常用的保留字指令......
  • Docker 架构演进之路
    转载:https://developer.aliyun.com/article/673009前言Docker已经推出了5年,在这5年中它极大的改变了互联网产品的架构,推进了新的产品开发、测试和运维方法。但是它自身也在......
  • centos安装mysql8
    安装教程#获取MySQL8.0源wgethttps://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm#安装源rpm-ivhmysql80-community-release-el7-2.noarch.r......
  • Jenkins+Docker 一键自动化部署 SpringBoot 项目
    Jenkins+Docker一键自动化部署SpringBoot项目 本文章实现最简单全面的Jenkins+docker+springboot 一键自动部署项目,步骤齐全,少走坑路。环境:centos7+git(git......
  • #PHP #MySQL数据操作 #在线聊天 PHP实现在线聊天与MySQL的“增查删改”
     目录1.目标图2.项目简介 3.目录结构 4.建立MySQL表 5.实现过程 5.1index.php5.2data.php 5.2method.php5.3 case.php5.4main.js5.5css/style.cs......
  • MySQL 5.7中如何定位DDL操作的阻塞问题
    mysql>begin; QueryOK,0rowsaffected(0.00sec) mysql>select*fromt1; +----+------+------+------+-------+ |id|name|age |num |num01| +---......
  • Docker+Jenkins+Gitee+Maven构建后台jar包后配置SSH传送到服务器并执行指定命令
    场景Docker+Jenkins+Gitee+Maven项目配置jdk、maven、gitee等拉取代码并自动构建以及遇到的那些坑:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/12839905......
  • mysql自带的压力测试工具mysqlslap
    mysql自带的压力测试工具mysqlslap,详情如下: 重要参数: --concurrency代表并发数量,多个可以用逗号隔开,当然你也可以用自己的分隔符隔开,这个时候要用到--delimiter开关。 -......
  • MySQL的一些常用命令
    mysql创建用户:createuser‘用户名’@‘%’identifiedby'密码';   #'%'表示所有地址都可以访问flushprivileges;   #刷新权限grantallon......