docker pull mysql:5.7
查看镜像下载情况
docker images
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
参数说明:
-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 基本配置:
vi /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
#同步的数据库有这些
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
然后保存文件即可。
重启 mysql-master 容器:
docker restart mysql-master
创建并运行 mysql-slave 容器:
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
修改配置:
vi /mydata/mysql/slaver/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 主从复制部分配置
server-id=2
log-bin=mysql-bin
#从库只读 read-only=1
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
然后保存文件即可。
重启 mysql-master 容器:
docker restart mysql-slaver-01
进入 master 容器:
docker exec -it mysql-master /bin/bash
进入 mysql 内部:
mysql -uroot -p
由于我们在启动容器的时候已经为它设置了初始密码: root, 所以我们的初始密码就是 root
登陆了mysql 之后,我们创建一个远程连接用户,以便于我们使用工具来操作mysql:
#1、授权 root 可以远程访问( 主从无关,为了方便我们远程连接 mysql)
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
#2、重新加载权限表
flush privileges;
#3、添加用来同步的用户
GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';
# 4、查看master 的相关信息
show master status\G;
然后 连续 两次的 exit ,退出mysql ; 再退出 该容器;
exit;
1、进入 mysql-slaver-01 容器
docker exec -it mysql-slaver-01 /bin/bash
进入 mysql 内部:
mysql -uroot -p
由于我们在启动容器的时候已经为它设置了初始密码: root, 所以我们的初始密码就是 root
登陆了mysql 之后,我们创建一个远程连接用户,以便于我们使用工具来操作mysql:
#1、授权 root 可以远程访问( 主从无关,为了方便我们远程连接 mysql)
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
#2、重新加载权限表
flush privileges;
#3、设置主库连接,这里的 “mysql-bin.000001” 就是我在上面让大家记来下的binlog
change master to
master_host='192.168.56.10',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154,master_port=3307;
#4、启动从库同步
start slave;
#5、查看从库状态
show slave status\G;
我们 使用 navicat 分别来连接这两个数据库
在master 中 创建数据库(!!!提示:一定要执行语句来创建,直接使用工具创建无法进行同步)
创master 中 建数据库gulimall_ums:
CREATE DATABASE gulimall_ums;
创建一个表:
DROP TABLE IF EXISTS `ums_member`;
CREATE TABLE `ums_member` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`level_id` bigint(20) NULL DEFAULT NULL COMMENT '会员等级id',
`username` char(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名',
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码',
`nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '昵称',
`mobile` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '手机号码',
`email` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`header` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '头像',
`gender` tinyint(4) NULL DEFAULT NULL COMMENT '性别',
`birth` date NULL DEFAULT NULL COMMENT '生日',
`city` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '所在城市',
`job` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '职业',
`sign` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '个性签名',
`source_type` tinyint(4) NULL DEFAULT NULL COMMENT '用户来源',
`integration` int(11) NULL DEFAULT NULL COMMENT '积分',
`growth` int(11) NULL DEFAULT NULL COMMENT '成长值',
`status` tinyint(4) NULL DEFAULT NULL COMMENT '启用状态',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '注册时间',
`social_uid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '社交用户的唯一id',
`access_token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '访问令牌',
`expires_in` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '访问令牌的时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '会员' ROW_FORMAT = Dynamic;
然后分别刷新两个连接可以发现,从库中也已经有了该数据库和该表:
插入一条数据:
insert into ums_member (id,username) VALUES (1,"海绵宝宝ID");
快去看看有没有数据吧!!(撒花)