本文引用: http://blog.csdn.net/dream_broken/article/details/77838579
想搭建個mysql主從復制的環(huán)境臂外,但是資源有限,不想創(chuàng)建多個虛擬機益咬,就用docker來吧侈玄。
centos7下安裝docker的方法婉刀,博文“docker學習2--centos7下安裝”。
docker pull 從官方下載mysql鏡像序仙,速度非常慢,所以就從daocloud下載鏡像吧突颊,速度很快。
docker pull daocloud.io/library/mysql:5.7
運行起來,端口分別對應3306,3307
3306為主律秃,3307為從
進入3306,并查看容器的系統(tǒng)版本
發(fā)現(xiàn)用的是debian版本的linux呈昔,查找下my.cnf在哪
查看my.cnf,發(fā)現(xiàn)里面又指向/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是唯一的锦爵,主從不能相同。
執(zhí)行下邊命令奥裸,運行兩個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的時候得到的數(shù)據(jù)一致
從庫3307啟動start slave
查看slave狀態(tài)
配置OK了湾宙,接著試試在主庫3306上創(chuàng)建數(shù)據(jù)庫樟氢、表,看看是否3307也同步
3306上執(zhí)行
CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
然后navicat上刷新3307侠鳄,發(fā)現(xiàn)3307上也創(chuàng)建有test庫了埠啃,說明主從同步OK了,可以接著測試創(chuàng)建表伟恶、對表的數(shù)據(jù)進行增刪改進行測試
在這從庫配置的時候碴开,還需要stop slave;change XXXX;start slave操作,那如果從庫重啟了知押,這主從同步是否仍然有效呢叹螟?停止3307鹃骂,然后在3306上創(chuàng)建表台盯,這是3307停止了,肯定沒同步到3307畏线,然后啟動3307静盅,再次檢查數(shù)據(jù),發(fā)現(xiàn)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
;