docker 下MySQL主从读写分离配置

主从同步机制: 同步基于耳机子机制,主服务器使用二进制来记录数据库的变动状况,从服务器通过读取和执行日志文件来保存主服务的数据一致
image

首先要保障主从的版本一致或相近

1 登陆docker,拉取镜像

docker pull mysql:7.5

2 创建 宿主机的数据卷

主数据库:
sudo mkdir /mydata/mysql/master/log
sudo mkdir /mydata/mysql/master/data
sudo mkdir /mydata/mysql/master/conf
从数据库
sudo mkdir /mydata/mysql/slaver/log
sudo mkdir /mydata/mysql/slaver/data
sudo mkdir /mydata/mysql/slaver/conf

3 启动master实例

docker run -p 3309: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 
#参数说明
  -p 3309:3306:将容器的3306端口映射到主机的3307端口
       -v /mydata/mysql/master/conf:/etc/mysql:将配置文件夹挂在到主机
       -v /mydata/mysql/master/log:/var/log/mysql:将日志文件夹挂载到主机
       -v /mydata/mysql/master/data:/var/lib/mysql/:将配置文件夹挂载到主机
       -e MYSQL_ROOT_PASSWORD=root:初始化root用户的密码

4 添加master配置

首先:vim /mydata/mysql/master/conf目录下
增加配置文件: touch my.conf
编辑: vi my.conf

[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=1
log-bin=mysql-bin
read-only=0
# 可以主从复制数据库
binlog-do-db=gmall_ums
binlog-do-db=gmall_pms
binlog-do-db=gmall_oms
binlog-do-db=gmall_sms
binlog-do-db=gmall_cms

#不参加主从复制数据库
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

5 启动slave实例

docker run -p 3316:3306 --name mysql-slaver-01 \
-v /mydata/mysql/slaver/log:/var/log/mysql \
-v /mydata/mysql/slaver/data:/var/lib/mysql \
-v /mydata/mysql/slaver/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7 

6 配置 slaver 配置

[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=gmall_ums
binlog-do-db=gmall_pms
binlog-do-db=gmall_oms
binlog-do-db=gmall_sms
binlog-do-db=gmall_cms


replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

7 从前主从容器

docker restart mysql_master
docker restart mysql_slaver_o1 
#如果重启失败,可以查看相关日志
docker logs --since 30m mysql_master 
docker logs --tail 10m  ysql_master
进入日志注销 :skip-name-resolve,然后重启
备注;查看docker日志的
Options:
–details 显示更多的信息
-f, --follow 跟踪实时日志
–since string 显示自某个timestamp之后的日志,或相对时间,如40m(即40分钟)
–tail string 从日志末尾显示多少行日志, 默认是all
-t, --timestamps 显示时间戳
–until string 显示自某个timestamp之前的日志,或相对时间,如40m(即40分钟)

8 进行master授权配置

1 进入容器
docker exec -it mysql-master /bin/bash
2  进入数据库,输入密码:root
mysql -uroot p 
3 进行授权远程连接用户
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
4 授权可以来同步的用户
   GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';
5  查看数据库状态
show master status\G;

9 进入slave 数据库配置

1 进入容器
docker exec -it mysql-slaver-01 bash
2 进入数据库
mysql -uroot -p
3 进行远程连接用户授权
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option
4设置主库连接
change master to master_host='192.168.124.130',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3309;
5 启动从库同步
start slave;
6 查看从库状态

备注:主从数据库在自己配置文件中声明需要同步哪个数据库,忽略哪个数据库等信息。并且server-id不能一样 2)、主库授权某个账号密码来同步自己的数据 3)、从库使用这个账号密码连接主库来同步数据

skip-name-resolve,日志添可能造成无法启动docker

select user,host from mysql.user where host <> 'localhost' ;

posted @ 2023-02-10 16:11  dayu2020  阅读(99)  评论(0编辑  收藏  举报