1.安装master
1.1.拉取镜像
docker pull mysql:5.7
1.2.修改配置
创建外部存放Mysql的目录,
mkdir /data/mysql
创建配置目录和数据目录
cd /data/mysql
mkdir conf.d
mkdir mysql.conf.d
mkdir log
mkdir data
新建my.cnf配置文件,将该配置文件放到/data/mysql目录。
配置文件内容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
## 设置server_id,注意要唯一 server-id=101
### 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
server-id=101
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
default-time_zone = '+8:00'
#日志自动过时清理天数
expire_logs_days = 7
#要给从机同步的库
#binlog-do-db=hl
#不给从机同步的库(这里不同步mysql、information_schema、performance_schema、sys库)
#binlog-ignore-db=mysql
#binlog-ignore-db=information_schema
#binlog-ignore-db=performance_schema
#binlog-ignore-db=sys
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
配置中启动了log-bin这个配置,该配置会让MySQL开启写二进制日志功能,以便于salve读取该日志进行数据同步。binlog-do-db配置用于指定需要同步的库,如果不写,默认为同步全部库。binlog-ignore-db配置用于指定忽略哪些库不同步。请注意server-id这个配置,在进行主从的时候,这个值不能重复,也就是master与slave不用配置相同的server-id。
expire_logs_days这个自动清除日志的天数建议设置小一些,否则在data文件下将会堆积大量的二进制日志文件bin-log。这种文件十分占内存!!!!
1.3.启动容器
此时Master所需的挂载和配置就已经完成,只需要使用命令启动容器即可:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=xxxx -m 512M --memory-swap=1024M -v /data/mysql/data:/var/lib/mysql -v /data/mysql/my.cnf:/etc/mysql/my.cnf -v /data/mysql/conf.d:/etc/mysql/conf.d -v /data/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d -d mysql:5.7
参数解释:
参数 | 说明 |
---|---|
--name | 指定容器的别名 |
-p | 端口映射,[宿主端口:容器端口],当访问主机的3306端口时,会自动映射到容器里的3306端口 |
--v | 容器挂载点,[挂载宿主目录:容器目录],用于容器内部的文件与宿主的文件互通 |
--e | 向容器设置环境变量,这里设置MySQL的密码为root |
容器启动完成之后,这时可以用工具连接数据库,连接数据库之后,创建一个账号,用户slave从库同步数据.
执行命令:
CREATE USER 'slave'@'%' IDENTIFIED BY 'xxxx';
然后进行授权,执行命令:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
FLUSH PRIVILEGES;
参数解释:
参数 | 说明 |
---|---|
REPLICATION SLAVE | 拥有此权限可以查看从服务器,从主服务器读取二进制日志 |
REPLICATION CLIENT | 拥有此权限可以查询master server、slave server状态 |
使用命令查看master状态:
SHOW MASTER STATUS
参数解释:
参数 | 说明 |
---|---|
file | 指定是当前log-bin日志文件,每次安装可能都不一样 |
Position | 数据同步的位置 |
Binlog-Do_DB | 要同步数据的数据库,默认为空,表示全部同步 |
Binlog-Ignore_DB | 忽略不同步的库,默认为空,表示都不忽略 |
master安装到此结束,现在等待slave来连接进行通信,同步数据。
2.安装Slave
安装slave的步骤基本和master一致,只是需要改几个配置
2.1.拉取镜像
docker pull mysql:5.7
2.2.修改配置
创建外部存放Mysql的目录,
mkdir /data/mysql
创建配置目录和数据目录
mkdir conf.d
mkdir mysql.conf.d
mkdir log
mkdir data
新建my.cnf配置文件,将该配置文件放到/data/mysql目录。
配置文件内容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
## 设置server_id,注意要唯一 server-id=102
server-id=102
##关闭 log-bin
#log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
default-time_zone = '+8:00'
#日志自动过时清理天数
expire_logs_days = 7
#要给从机同步的库
#binlog-do-db=hl
#不给从机同步的库(这里不同步mysql、information_schema、performance_schema、sys库)
#binlog-ignore-db=mysql
#binlog-ignore-db=information_schema
#binlog-ignore-db=performance_schema
#binlog-ignore-db=sys
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
该文件内容与master的几乎一致,只是把log-bin配置关闭,因为slave不需要开启binlog日志,server-id值改为与master不一致的就行.
2.3.启动容器
此时slave所需的挂载和配置就已经完成,只需要使用命令启动容器即可:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=xxxx -m 512M --memory-swap=1024M -v /data/mysql/data:/var/lib/mysql -v /data/mysql/my.cnf:/etc/mysql/my.cnf -v /data/mysql/conf.d:/etc/mysql/conf.d -v /data/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d -d mysql:5.7
2.4.创建主从关系
连接到salve数据库库,然后执行命令:
CHANGE MASTER TO master_host = '124.71.8.46', master_user = 'slave', master_password = 'xxxx', master_port = 3306, master_log_file = 'mysql-bin.000001', master_log_pos = 154, master_connect_retry = 30;
参数解释:
参数 | 说明 |
---|---|
master_host | master主机地址 |
master_user | 主从连接的用户名,刚才master创建的 |
master_password | 主从连接的密码,刚才master创建的 |
master_host | master的端口 |
master_log_file | master的bin-log文件 |
master_log_pos | master数据同步的位置 |
master_log_file和master_log_pos可以在master上输入SHOW MASTER STATUS;指令查询到,这个两个参数必须与master上一致。
然后执行命令,启动slave功能:
START SLAVE;
查看slave状态,SHOW SLAVE STATUS;
SHOW SLAVE STATUS;
看到Slave_IO_State显示Waiting for master to send event表示主从已经建立成功,此时MySQL主从配置到此完毕。
在master新建一个数据库test,刷新slave后发现,slave已经同步完成。代表主从数据同步成功