本文引用: http://blog.csdn.net/dream_broken/article/details/77838579
想搭建个mysql主从复制的环境,但是资源有限,不想创建多个虚拟机,就用docker来吧。
centos7下安装docker的方法,博文“docker学习2--centos7下安装”。
docker pull 从官方下载mysql镜像,速度非常慢,所以就从daocloud下载镜像吧,速度很快。
docker pull daocloud.io/library/mysql:5.7
运行起来,端口分别对应3306,3307
3306为主,3307为从
进入3306,并查看容器的系统版本
发现用的是debian版本的linux,查找下my.cnf在哪
查看my.cnf,发现里面又指向/etc/mysql/mysql.conf.d/mysql.cnf
cat /etc/mysql/mysql.conf.d/mysqld.cnf
看到
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-
links is recommended to prevent assorted security risks
symbolic-links=0
这是我们熟悉的mysql配置了,我们指定下编码和主服务配置
使用vi的时候,提示not found
root@b5ab3b336918:/etc/mysql/mysql.conf.d# vi /etc/mysql/mysql.conf.d/mysqld.cnf
bash: vi: command not found
apt-get install vim失败。。。不想折腾了,把mysql.conf.d/mysql.cnf的内容复制到window本地来,修改好后,上传到虚拟机,然后docker run 的时候,使用-v 命令,把mysql.cnf映射到mysql容器吧
退出容器,删除容器mysql3306,mysql3307
虚拟机上准备好/mysql-3306.cnf,是主库的配置
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8
default-storage-engine=INNODB
sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
server-id=1
log_bin=/var/log/mysql/binlog
从库配置/mysql-3307.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8
default-storage-engine=INNODB
sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
server-id=2
log_bin=/var/log/mysql/binlog
innodb_file_per_table=ON
skip_name_resolve=ON
binlog-format=row
log-slave-updates=true
relay_log=/var/log/mysql/relay.log
server-id是唯一的,主从不能相同。
执行下边命令,运行两个mysql镜像
docker run --name mysql3306 -v /mysql-3306.cnf:/etc/mysql/mysql.conf.d/mysql.cnf -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d daocloud.io/library/mysql:5.7
docker run --name mysql3307 -v /mysql-3307.cnf:/etc/mysql/mysql.conf.d/mysql.cnf -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d daocloud.io/library/mysql:5.7
使用navicat连接3306,3307
主库3306上查询下SHOW MASTER STATUS
从库3307上,停止slave,并配置主库master的连接
配置主库连接,使用root用户
CHANGE MASTER TO MASTER_HOST='192.168.174.136',MASTER_USER='root', MASTER_PASSWORD='123456',MASTER_LOG_FILE='binlog.000003',MASTER_LOG_POS=154;
注意:MASTER_LOG_FILE='binlog.000003',MASTER_LOG_POS=154;要和主库3306上面show master的时候得到的数据一致
从库3307启动start slave
查看slave状态
配置OK了,接着试试在主库3306上创建数据库、表,看看是否3307也同步
3306上执行
CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
然后navicat上刷新3307,发现3307上也创建有test库了,说明主从同步OK了,可以接着测试创建表、对表的数据进行增删改进行测试
在这从库配置的时候,还需要stop slave;change XXXX;start slave操作,那如果从库重启了,这主从同步是否仍然有效呢?停止3307,然后在3306上创建表,这是3307停止了,肯定没同步到3307,然后启动3307,再次检查数据,发现3306的表同步过来了,说明从库重启,同步功能然后存在。
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT ,
`user_name` varchar(50) NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
;