MySQL 集群

发布于:2024-12-21 ⋅ 阅读:(12) ⋅ 点赞:(0)
1、创建 实例并启动

mysql-master:

    image: mysql:8.0

    container_name: mysql-master

    environment:

      # 时区上海

      TZ: Asia/Shanghai

      # root 密码

      MYSQL_ROOT_PASSWORD: cad2024

    ports:

      - "3316:3306"

    volumes:

      # 数据挂载

      - /docker/mysql_master/data/:/var/lib/mysql/

      # 配置挂载

      - /docker/mysql_master/conf/:/etc/mysql/conf.d/

    command:

      # 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)

      --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

    privileged: true

    restart: always

   

  mysql-slave:

    image: mysql:8.0

    container_name: mysql-slave

    environment:

      # 时区上海

      TZ: Asia/Shanghai

      # root 密码

      MYSQL_ROOT_PASSWORD: cad2024

    ports:

      - "3326:3306"

    volumes:

      # 数据挂载

      - /docker/mysql-slave/data/:/var/lib/mysql/

      # 配置挂载

      - /docker/mysql-slave/conf/:/etc/mysql/conf.d/

    command:

      # 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)

      --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

    privileged: true

    restart: always

2、修改 master 基本配置 

vim /docker /mysql_master /conf/my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
#注意:skip-name-resolve 一定要加,不然连接 mysql 会超级慢
#唯一id
server_id=1
#复制是通过二进制日志操作增删改,并记录执行到那个位置
log-bin=mysql-bin
#不止读
read-only=0
#需要备份的数据库模式
binlog-do-db=gulimall_ums
binlog-do-db=gulimall_pms
binlog-do-db=gulimall_oms
binlog-do-db=gulimall_sms
binlog-do-db=gulimall_wms
binlog-do-db=gulimall_admin
#不需要备份的数据库
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

3、修改 slave 基本配置 

vim /docker /mysql_ slave /conf/my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
server_id=2
log-bin=mysql-bin
#只读
read-only=1
binlog-do-db=gulimall_ums
binlog-do-db=gulimall_pms
binlog-do-db=gulimall_oms
binlog-do-db=gulimall_sms
binlog-do-db=gulimall_wms
binlog-do-db=gulimall_admin
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

 4、为 master 授权用户来他的同步数据

#给backup用户可以访问mysql并进行日志复制

CREATE USER 'backup'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'backup'@'%';

#查看 master 状态
show master status;

5、配置 slaver 同步 master 数据 

1)、告诉从mysql需要同步那个主节点
change master to
master_host='117.50.198.127 '
,master_user='backup'
,master_password='123456 '
,mas ter_log_file='mysql-bin.000001'
,master_log_pos=0
,master_port=3316;
2)、启动从库同步
start slave;
3)、查看从库状态
show slave status;


网站公告

今日签到

点亮在社区的每一天
去签到