Mysql常见集群方式

  • Mysql-MMM(mysql主主复制管理器)
  • MHA(Mysql高可用方面是一个相对成熟的方案)
  • InnoDB Cluster(支持自动Failover,强一致性,读写分离,读库高可用,读请求负载均衡,推荐方案)

主从同步

创建Master实例并启动

1
2
3
4
5
6
docker run -p 3307: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

参数说明:

1
2
3
-p 3307:3306:将容器的3306映射到主机的3307端口
-v 挂载
-e 初始化root用户密码

修改master基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vim /mydata/mysql/master/conf/my.cnf

##################################
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection=uft8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
# skip-name-resolve 一定要加,不然连接mysql会超级慢。
skip-name-resolve
##################################

添加master主从复制部分配置

1
2
3
4
5
6
7
8
9
10
11
12
server_id=1
log-bin=mysql-bin
read-only=0
binlog-do-db=待同步的数据库1
binlog-do-db=待同步的数据库2
binlog-do-db=待同步的数据库3


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

重启master

创建Slave实例并启动

1
2
3
4
5
6
docker run -p 3317: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

修改slaver基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vim /mydata/mysql/slaver/conf/my.cnf

##################################
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection=uft8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
##################################

添加master主从复制部分配置

1
2
3
4
5
6
7
8
9
10
11
server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=待同步的数据库1
binlog-do-db=待同步的数据库2
binlog-do-db=待同步的数据库3

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

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

  • 进入master容器

docker exec -it mysql-master /bin/bash

  • 进入mysql内部(mysql -uroot -p)
1
2
3
4
#授权root可远程访问(主从无关,为了方便我们远程连接mysql)

grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;
  • 添加用来同步的用户
1
2
3
-- 核心 授权一个专门用来复制的; 'backup'@'%':任何主机都可以
-- 运行后,master就有一个授权用户
GRANT REPLICATION SLAVE ON *.* TO 'backup'@'%' IDENTIFIED BY '123456';
  • 查看master状态
1
show master status;

配置slaver同步master数据

  • 进入slaver容器

docker exec -it mysql-slaver-01 /bin/bash

  • 进入mysql内部(mysql -uroot -p)
1
2
3
# 授权root可以远程访问(主从无关,为了方便我们远程连接mysql)

grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; flush privileges;
  • 设置主库连接
1
change master to master_host='<master_ip>',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3307;
  • 启动从库同步
1
start slave;
  • 查看从库状态
1
show slave status;