准备资料
1、mysql.config
2、init.d 子节点初始化SQL
1、准备 mysqld.cnf
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#sql-mode=NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
sql-mode=NO_ENGINE_SUBSTITUTION
#user=mysql
#default-storage-engine=INNODB
character-set-server=utf8mb4
default-time_zone = '+8:00'
max_connections=100000
server_id=1
log_bin=mysql-bin
## 需要主从复制的数据库 根据自己的需求定义
binlog-do-db=nacos_config
binlog-do-db=abc_config
## 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql
## 为每个session分配的内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M
## 主从复制的格式(mixed,statement,row,默认格式是statement。建议是设置为row,主从复制时数据更加能够统一)
binlog_format=row
## 二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
skip-name-resolve
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
2、init.d 子节点初始化SQL
change master to master_host='mysql-n1',master_port=3306,master_user='root',master_password='a123456',master_log_file='mysql-bin.000003',master_log_pos=0;
start slave;
编写stack-compose
version: '3.7'
services:
mysql-n1:
image: mysql:8.0.21
hostname: mysql-n1
ports:
- 3306:3306
user: root
restart: always
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=a123456
- TZ=Asia/Shanghai
volumes:
# 和docker-compose 不一样的是要提前创建,否则会报错,如果
- ./data/mysql-n1/db:/var/lib/mysql
- ./data/mysql-n1/logs:/var/log/mysql/
- ./init.d/master:/docker-entrypoint-initdb.d/:ro
- ./config:/etc/mysql/conf.d/:rw
command:
- "--server-id=1"
- "--default-authentication-plugin=mysql_native_password"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_general_ci"
- "--explicit_defaults_for_timestamp=true"
- "--lower_case_table_names=1"
- "--binlog-ignore-db=mysql"
- "--binlog_format=row"
- "--log-bin=mysql-bin"
- "--sync_binlog=1"
- "--expire_logs_days=7"
- "--slave_skip_errors=1062"
networks:
- test-overlay
deploy:
placement:
constraints:
- node.hostname == n1
- node.role == manager
mysql-n2:
image: mysql:8.0.21
hostname: mysql-n2
user: root
restart: always
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=a123456
- TZ=Asia/Shanghai
volumes:
- ./data/mysql-n2/db:/var/lib/mysql
- ./data/mysql-n2/logs:/var/log/mysql/
- ./init.d/slave/:/docker-entrypoint-initdb.d/:ro
command:
- "--server-id=2"
- "--default-authentication-plugin=mysql_native_password"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_general_ci"
- "--explicit_defaults_for_timestamp=true"
- "--lower_case_table_names=1"
- "--binlog-ignore-db=mysql"
- "--binlog_format=row"
- "--log-bin=mysql-bin"
- "--sync_binlog=1"
- "--expire_logs_days=7"
- "--slave_skip_errors=1062"
depends_on:
- mysql-n1
networks:
- test-overlay
deploy:
placement:
constraints:
- node.hostname == n2
- node.role == worker
mysql-n3:
image: mysql:8.0.21
hostname: mysql-n3
user: root
restart: always
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=a123456
- TZ=Asia/Shanghai
volumes:
- ./data/mysql-n3/db:/var/lib/mysql
- ./data/mysql-n3/logs:/var/log/mysql/
- ./init.d/slave:/docker-entrypoint-initdb.d/:ro
- ./config:/etc/mysql/conf.d/:rw
command:
- "--server-id=3"
- "--default-authentication-plugin=mysql_native_password"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_general_ci"
- "--explicit_defaults_for_timestamp=true"
- "--lower_case_table_names=1"
- "--binlog-ignore-db=mysql"
- "--binlog_format=row"
- "--log-bin=mysql-bin"
- "--sync_binlog=1"
- "--expire_logs_days=7"
- "--slave_skip_errors=1062"
depends_on:
- mysql-n1
- mysql-n2
networks:
- test-overlay
deploy:
placement:
constraints:
- node.hostname == n3
- node.role == worker
networks:
test-overlay:
driver: overlay
#false-统自动创建网桥名,格式为: 目录名_网桥名,默认为false; true-使用外部创建的网桥,需需要自己手动创建
external: true
node.hostname 代表宿主机的名称
node.role 代表节点的权限启动 docker stack deploy --with-registry-auth -c stack-compose.yml mysql