一、测试环境
笔记本操作系统: win10 8G内存
虚拟机: vmware
虚拟机操作系统(宿主机): centos7
docker版本: 1.13.1
mysql版本: 5.5
二、安装docker、拉取mysql镜像、使用镜像创建mysql容器
1.在windows上安装vmware和linux操作系统
以前模拟多台服务器时,我一般会在虚拟机中开多个centos系统,那样的话,不但笔记本卡的要命,而且需要自己安装各种环境和服务,特别麻烦。现在借助docker,可以直接拉取远程的镜像,还可以耗用极少的资源创建多台服务器(容器),同时,docker容器可以实现秒级启动,极大的方便了我们的测试
2.安装docker
$ yum install -y docker
修改镜像地址,编辑/etc/docker/daemon.json
{
"registry-mirrors": ["https://uulfe4bd.mirror.aliyuncs.com"]
}
启动docker
$ systemctl start docker //centos7及以后版本
$ service docker start //centos6及之前的版本
3.拉取mysql镜像
mysql版本 5.5
$ docker pull registry.cn-hangzhou.aliyuncs.com/huati/mysql
查看所有镜像
$ docker images
删除镜像
$ docker rmi [image ID]
启动mysql镜像并将容器的3306端口映射到宿主机的33061端口
这一步是为了在windows上通过navitcat连接宿主机的33061端口即可访问mysql容器中的数据库
$ docker run -p 33061:3306 -td [container ID]
启动第二个mysql容器
$ docker run -p 33062:3306 -td [container ID]
查看所有容器以及状态,UP表示容器已经启动,0.0.0.0:33062->3306/tcp 为端口映射关系
$ docker ps -a
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
094ea693a956 23a1b36821fb "/data1/startup.sh" About an hour ago Up 43 minutes 0.0.0.0:33062->3306/tcp mysql_2
c2db34ba4e9a e04972ef39d2 "/usr/lib/mycat/bi..." 2 hours ago Exited (137) About an hour ago mycat
459cc55239f6 23a1b36821fb "/data1/startup.sh" 4 hours ago Up About an hour 0.0.0.0:33061->3306/tcp mysql_1
9dfefbcdb186 3db94df3e006 "/usr/sbin/init" 9 days ago Exited (137) 2 days ago centos7
39d9178a9fdd hello-world "/hello" 11 days ago Exited (0) 2 days ago hello-world
为容器重命名
$ docker rename [old name] [new name]
删除容器
$ docker rm [container name]
关闭容器命令
$ docker stop [container name]
启动容器命令
docker start [container name]
进入容器
利用该命令进入容器后,并不能使用systemctl命令重启mysql服务,暂时未查找解决方案,当我修改mysql配置时,会关闭容器后再次开启容器,来使新配置生效......
$ docker exec -it [container name] /bin/bash
或
$ docker exec -it 容器名称 bash
退出容器
$ exit
三、实现mysql主从复制
修改主从数据库的数据库密码
从上文中拉取的mysql镜像,运行后无需密码即可使用root,一般情况下,我们应该设置数据库的访问密码
分别在主从数据库中执行
mysql> grant all privileges on *.* to 'root'@'%' identified by '1234' with grant option;
mysql> flush privileges;
执行后,需要密码才能继续访问数据库
分别在主从数据库创建测试主从同步的数据库和表
CREATE DATABASE master-slave-test;
USE master-slave-test;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
修改 /etc/my.cnf,其中有一项为server-id,主从的server-id不能相同
修改主库配置
$ vim /etc/my.cnf
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
server-id = 1
port = 3306
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
default-storage-engine = InnoDB
log-bin = mysql-bin
# master-slave ====== start ====
log-bin-index = mysql-bin.index
relay-log = mysql-relay
relay-log-index = mysql-relay.index
expire-logs-days = 10
max-binlog-size = 100M
max_binlog_cache_size = 8M
log-slave-updates = 1
binlog_cache_size = 4M
# use MIXED binlog
binlog_format = MIXED
#binlog_format = ROW
#多加注意:多个db要换行写,不能用逗号隔开
#replicate-do-db = db%.%
replicate-do-db =master-slave-test
#replicate-ignore-db = mysql.%
# ignore tables
replicate-wild-ignore-table = mysql.%
sync_binlog = 1
relay_log_recovery = 1
log_slave_updates = 1
skip-name-resolve
sql_mode=STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 32M
# master-slave ====== end====
修改从库配置
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
#主库和从库的server-id不能相同
server-id = 2
port = 3306
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
default-storage-engine = InnoDB
log-bin = mysql-bin
# master-slave ====== start ====
log-bin-index = mysql-bin.index
relay-log = mysql-relay
relay-log-index = mysql-relay.index
expire-logs-days = 10
max-binlog-size = 100M
max_binlog_cache_size = 8M
log-slave-updates = 1
binlog_cache_size = 4M
# use MIXED binlog
binlog_format = MIXED
#binlog_format = ROW
#多加注意:多个db要换行写,不能用逗号隔开
#replicate-do-db = db%.%
replicate-do-db =master-slave-test
#replicate-ignore-db = mysql.%
# ignore tables
replicate-wild-ignore-table = mysql.%
sync_binlog = 1
relay_log_recovery = 1
log_slave_updates = 1
skip-name-resolve
sql_mode=STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 32M
# master-slave ====== end====
配置时候注意几个坑:
!!!Replication-do-db的坑,如果多个库则使用多行Replication-do-db进行配置
!!!Replication-ignore-db的坑,如果忽略多个库则使用多行Replication-ignore-db进行配置
重启docker容器,使mysql配置生效
重启后,进入主库
在msater中查看主机状态
mysql> show master status \G;
记录主机日志所在位置的数字以及bin log文件名,一会儿在从库执行命令时会用到
重启后,进入从库
1. stop slave;
2. CHANGE MASTER TO MASTER_HOST='172.17.0.2',MASTER_USER='repl',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=614;
master_log_file和master_log_pos的值为刚才从主库查到的
这样配置表示从当前时间开始,往后的数据进行同步
另外还可以配置将之前的主库数据也同步到从库,我暂时没找到怎么配
3. start slave;
在slave中查看从机状态命令:
mysql> show slave status \G;
出现类似以下结果表示主从复制配置成功
File: mysql-bin.000006
Position: 107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec)
最后,在主库user表中添加一些数据,看看效果吧
网友评论