docker mysql主从配置

1.先启动一个主容器,执行以下指令(挂载文件夹不需要创建,会自动创建)

docker run --name mysql3307 -p 3307:3306 --privileged=true -e MYSQL_ROOT_PASSWORD=root -v /Users/guokai/Documents/mysql/3307/cnf:/etc/mysql/conf.d -v  /Users/guokai/Documents/mysql/3307/data/:/var/lib/mysql -v /Users/guokai/Documents/mysql/3307/logs/:/var/log/mysql -d mysql:5.7

2.启动一个从容器

docker run --name mysql3308 -p 3308:3306 --privileged=true -e MYSQL_ROOT_PASSWORD=root -v /Users/guokai/Documents/mysql/3308/cnf:/etc/mysql/conf.d -v  /Users/guokai/Documents/mysql/3308/data/:/var/lib/mysql -v /Users/guokai/Documents/mysql/3308/logs/:/var/log/mysql -d mysql:5.7

3.进入到主机配置文件位置创建my.cnf:

 touch my.cnf
 /Users/guokai/Documents/mysql/3307/cnf
 /Users/guokai/Documents/mysql/3308/cnf

my.cnf内容如下

# 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
#
# 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
#datadir=/home/mysql/docker-data/3307/data
#socket=/home/mysql/docker-data/3307/mysql.sock

character_set_server=utf8
init_connect='SET NAMES utf8'

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#log-error=/home/mysql/docker-data/3307/logs/mysqld.log
#pid-file=/home/mysql/docker-data/3307/mysqld.pid
#标识符不能重复 从机需要换一个id,随便写,只要不重复就行
server-id=1283307
#开启复制功能
log-bin=mysql-bin

4.配置完成后接下来需要重启我的mysql3307和mysql3308容器,并在master主机中创建一个用户添加slave和replication权限

#连接3307mysql配置权限,我的主机是3307
#首先进入mysql容器内部 因为我宿主机是没有安装MySQL的所以才需要进入内部,你们安装了的话之间外部MySQL命令设置ip端口号就好了
docker exec -it mysql3307 bash
#连接MySQL
mysql -uroot -proot
#设置权限
GRANT REPLICATION SLAVE,FILE,REPLICATION CLIENT ON *.* TO 'repluser'@'%' IDENTIFIED BY '123456';
#刷新权限
FLUSH PRIVILEGES;

5.查看mysql binlog,记住file名称和position值,需要在从机配置

SHOW MASTER STATUS;
image.png

6.进入从机,执行以下指令


change master to master_host='192.168.30.14',master_port=3307,master_user='repluser',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=467;
#开启slave,启动SQL和IO线程
start slave;

#查看slave的状态 \G是格式化的意思
show slave status\G
image.png

7.从机通过SHOW PROCESSLIST;查看进程信息,如下图可以明确看到在等待master发送事件和从服务器已读取所有中继日志;等待更多的更新


aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAyMC81LzIzLzE3MjNmN2Q3YjEzMjMxZDg.png

主机通过SHOW PROCESSLIST;查看进程信息,如下图可以明确看到主机已将binlog日志发送给从机,等待更多的更新操作
aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAyMC81LzIzLzE3MjNmODBlNmJiYmVhOTM.png

至此主从其实已经搭建完

你可能感兴趣的:(docker mysql主从配置)