一、 Docker安装MySQL搭建主从
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
docker run -p 3306:3306 很多 -d --name haha mysql:5.7
Docker启动容器的数据部分一定挂载出来
1、创建Master
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/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always \
-d mysql:5.7
# 参数说明 /etc/my.cnf 任意的xxx.cnf 只要放在 /etc/mysql/conf.d 目录下,mysql也能认为是他的配置文件
# -p 3307: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用户的密码
#修改master基本配置
vim /mydata/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会超级慢
#添加master主从复制部分配置
server_id=1
log-bin=mysql-bin
read-only=0 #主库是写库
#可以同步的库,master只需要记录哪些库的二进制日志
binlog-do-db=hello
binlog-do-db=world
#不用同步的库
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
重启docker restart 容器ID|容器name
2、创建Slaver01
docker run -p 3308:3306 --name mysql-slaver-01 \
-v /mydata/mysql/slaver01/log:/var/log/mysql \
-v /mydata/mysql/slaver01/data:/var/lib/mysql \
-v /mydata/mysql/slaver01/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always \
-d mysql:5.7
#修改slave基本配置
vim /mydata/mysql/slaver01/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
#添加master主从复制slaver01部分配置
server_id=2
read-only=1
relay-log=mysql-relay
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
3、创建Slaver02
docker run -p 3309:3306 --name mysql-slaver-02 \
-v /mydata/mysql/slaver02/log:/var/log/mysql \
-v /mydata/mysql/slaver02/data:/var/lib/mysql \
-v /mydata/mysql/slaver02/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always \
-d mysql:5.7
#修改slave基本配置
vim /mydata/mysql/slaver02/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
#添加master主从复制slaver02部分配置
server_id=3
read-only=1
relay-log=mysql-relay
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
4、master授权slaver链接
1、进入master容器
docker exec -it mysql-master /bin/bash
2、进入mysql内部 (mysql –uroot -p)
- 1)、授权root可以远程访问( 主从无关,为了方便我们远程连接mysql)
grant all privileges on . to 'root'@'%' identified by 'root' with grant option;
flush privileges; - 2)、添加用来同步的用户
GRANT REPLICATION SLAVE ON . TO 'slave'@'%' IDENTIFIED BY '123456';
3、查看master状态
show master status\G;
5、slaver01同步master数据
1、进入slaver容器
docker exec -it mysql-slaver-01 /bin/bash
2、进入mysql内部(mysql –uroot -p)
- 1)、授权root可以远程访问( 主从无关,为了方便我们远程连接mysql)
grant all privileges on . to 'root'@'%' identified by 'root' with grant option;
flush privileges;
- 2)、设置主库连接
change master to master_host='192.168.119.159',master_user='slave',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3307;
- 3)、启动从库同步
start slave;
3、查看从库状态
show slave status\G;
6、slaver02同步master数据
1、进入slaver容器
docker exec -it mysql-slaver-02 /bin/bash
2、进入mysql内部(mysql –uroot -p)
- 1)、授权root可以远程访问( 主从无关,为了方便我们远程连接mysql)
grant all privileges on . to 'root'@'%' identified by 'root' with grant option;
flush privileges;
- 2)、设置主库连接
change master to master_host='192.168.119.159',master_user='slave',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3307;
- 3)、启动从库同步
start slave;
3、查看从库状态
show slave status\G;
至此主从配置完成
标签:log,mydata,utf8,MySQL,db,master,mysql,集群,主从 From: https://www.cnblogs.com/qbbit/p/16625040.html