Docker 中怎么实现MySQL主主复制,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
Install Docker Engine on CentOS
Master
新建目录data(XXXX/master/data),映射docker数据库目录
新建配置(XXXX/master/my.cnf)
[mysqld] skip-host-cache skip-name-resolve datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock secure-file-priv=/var/lib/mysql-files user=mysql pid-file=/var/run/mysqld/mysqld.pid # replication # server id 唯一 server_id=1 # STATEMENT, ROW, or MIXED binlog_format=ROW log_bin=/var/lib/mysql/master-bin auto-increment-increment=2 auto-increment-offset=1 slave-skip-errors=all # 不进行复制的数据库 replicate-ignore-db=information_schema replicate-ignore-db=mysql replicate-ignore-db=performance_schema replicate-ignore-db=sys
Replica
新建目录data(XXXX/rep/data),映射docker数据库目录
新建配置(XXXX/rep/my.cnf)
[mysqld] skip-host-cache skip-name-resolve datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock secure-file-priv=/var/lib/mysql-files user=mysql pid-file=/var/run/mysqld/mysqld.pid # replication # server id 唯一 server_id=2 # STATEMENT, ROW, or MIXED binlog_format=ROW log_bin=/var/lib/mysql/rep-bin auto-increment-increment=2 auto-increment-offset=1 slave-skip-errors=all # 不进行复制的数据库 replicate-ignore-db=information_schema replicate-ignore-db=mysql replicate-ignore-db=performance_schema replicate-ignore-db=sys
docker network create mysql_net
mysql服务连接到新增的网络mysql_net
mysql服务可以通过主机名互相访问
Master
docker container run --name mysql_master \ --network mysql_net \ --hostname mysql_master \ --publish 33306:3306 \ --env MYSQL_ROOT_PASSWORD=123456 \ --mount type=bind,src=/xxxx/master/my.cnf,dst=/etc/my.cnf \ --mount type=bind,src=/xxxx/master/data,dst=/var/lib/mysql \ -d mysql/mysql-server:8.0 --character-set-server=utf8 --collation-server=utf8_general_ci --explicit_defaults_for_timestamp
Replica
docker container run --name mysql_rep \ --network mysql_net \ --hostname mysql_rep \ --publish 43306:3306 \ --env MYSQL_ROOT_PASSWORD=123456 \ --mount type=bind,src=/xxx/repl/my.cnf,dst=/etc/my.cnf \ --mount type=bind,src=/xxx/repl/data,dst=/var/lib/mysql \ -d mysql/mysql-server:8.0 --character-set-server=utf8 --collation-server=utf8_general_ci --explicit_defaults_for_timestamp
参数说明
XXX
:替换为有效的路径
network
:指定为新增的网络mysql_net
hostname
:服务主机名
publish
:端口映射([docker host Port]:[container Port])
MYSQL_ROOT_PASSWORD
: MySQL默认密码
mount
:挂载配置文件及数据库文件目录
查看MySQL服务是否正常启动
# 查看Docker container进程 docker container ps -a | grep mysql # 查看启动日志 docker container logs mysql_master docker container logs mysql_rep
新增具复制权限的用户
登录主数据库
# 登入 mysql_master docker container exec -it mysql_master bash # 连接数据库,密码为配置文件中指定的123456 mysql -u root -p # 新增复制用户并赋复制权限 CREATE USER 'dev'@'%' IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'dev'@'%';
登录备数据库
# 登入 mysql_rep docker container exec -it mysql_rep bash # 连接数据库,密码为配置文件中指定的123456 mysql -u root -p # 新增复制用户并赋复制权限 CREATE USER 'dev'@'%' IDENTIFIED BY '123456'; GRANT ALL ON *.* TO 'dev'@'%';
分别查看主从服务数据库状态
# 主数据库 mysql> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000005 | 445 | | | | +-------------------+----------+--------------+------------------+-------------------+ # 备数据库 mysql> SHOW MASTER STATUS; +----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+-------------------+ | rep-bin.000002 | 155 | | | | +----------------+----------+--------------+------------------+-------------------+
配置数据库复制
配置备数据库mysql_rep
# MASTER_HOST:主数据库服务名 # MASTER_LOG_FILE: 日志文件名称 # MASTER_LOG_POS: 日志文件当前位置 mysql> CHANGE MASTER TO MASTER_HOST='mysql_master', MASTER_USER='dev', MASTER_PASSWORD='123456', MASTER_LOG_FILE='master-bin.000003', MASTER_LOG_POS=692;
配置主数据库mysql_master
# MASTER_HOST: 从数据库服务名 # MASTER_LOG_FILE: 日志文件名称 # MASTER_LOG_POS: 日志文件当前位置 mysql> CHANGE MASTER TO MASTER_HOST='mysql_rep', MASTER_USER='dev', MASTER_PASSWORD='123456', MASTER_LOG_FILE='rep-bin.000001', MASTER_LOG_POS=155;
启动复制
备数据库
mysql> start slave;
主数据库
mysql> start slave;
查看数据库状态
mysql> show master status; mysql> show slave status;
看完上述内容,你们掌握Docker 中怎么实现MySQL主主复制的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。