基于Docker的Mysql主备搭建的实现步骤
作者:tuacy
本文主要介绍了基于Docker的Mysql主备搭建的实现步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Docker mysql主从配置。我们会在一台centos的虚拟机上,配置mysql主备
前期规划:
- docker mysql版本 mysql:5.7
- docker网络配置,我们会创建名称为mysql的网络,子网掩码为 172.88.0.0/16
- docker mysql主对应的容器名称:mysql-master, ip地址:172.88.0.11,映射端口:3307,文件卷配置(配置文件和数据文件持久化)mysql配置文件地址:/usr/local/docker/mysql/slave/conf,mysql数据文件地址:/usr/local/docker/mysql/slave/data
- docker mysql从对应的容器名称:mysql-slave, ip地址: 172.88.0.12,映射端口:3308,文件卷配置(配置文件和数据文件持久化)mysql配置文件地址:/usr/local/docker/mysql/master/conf,mysql数据文件地址:/usr/local/docker/mysql/master/data
配置过程:
网络创建(两个mysql之间是需要通信的)
# 创建网络mysql,子网掩码172.88.0.0/16 [root@localhost conf]# docker network create mysql --subnet 172.88.0.0/16 # 查看网络是否创建成功:有mysql则表示创建成功 [root@localhost conf]# docker network ls NETWORK ID NAME DRIVER SCOPE b03fab4e1371 bridge bridge local 0abda6b25a0e host host local 844e3f85e00e mysql bridge local c2bbcb4013a7 none null local
拉取docker mysql镜像,这里我们使用的是mysql:5.7版本
# 拉取docker镜像,mysql:5.7版本 [root@localhost ~]# docker pull mysql:5.7
启动两个mysql容器,mysql-master,mysql-slave
# 记得修改下conf的配置文件 # chmod -R 777 /usr/local/docker/mysql/master/conf # chmod -R 777 /usr/local/docker/mysql/slave/conf # 启动容器,一个对应主,一个对应从 # mysql master [mysql-master, 3307,172.88.0.11,/usr/local/docker/mysql/master/data,/usr/local/docker/mysql/master/conf] [root@localhost /]# docker run --name mysql-master -p 3307:3306 -v /usr/local/docker/mysql/master/data:/var/lib/mysql -v /usr/local/docker/mysql/master/conf:/etc/mysql/ -e MYSQL_ROOT_PASSWORD=123456 -d --net mysql --ip 172.88.0.11 mysql:5.7 # mysql slave [mysql-slave, 3308,172.88.0.12,/usr/local/docker/mysql/slave/data,/usr/local/docker/mysql/slave/conf] [root@localhost /]# docker run --name mysql-slave -p 3308:3306 -v /usr/local/docker/mysql/slave/data:/var/lib/mysql -v /usr/local/docker/mysql/slave/conf:/etc/mysql/ -e MYSQL_ROOT_PASSWORD=123456 -d --net mysql --ip 172.88.0.12 mysql:5.7 # 查看mysql-master,mysql-slave两个容器是否正确启动 [root@localhost /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 205cd234c29b mysql:5.7 "docker-entrypoint.s…" 9 seconds ago Up 7 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp mysql-slave cdcc14b2b2a2 mysql:5.7 "docker-entrypoint.s…" 49 seconds ago Up 46 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
mysql主备配置
# 修改配置文件 # mysql-master配置文件修改,因为我们做了docker文件卷处理,所以我们直接在宿主机/usr/local/docker/mysql/master/conf下操作即可 [root@localhost ~]# cd /usr/local/docker/mysql/master/conf/ [root@localhost conf]# vi my.cnf # 在my.cnf里面输入如下信息 [mysqld] ## 同一局域网内注意要唯一 server-id=100 ## 开启二进制日志功能,可以随便取(关键) log-bin=mysql-bin # 进入mysql-master容器里面重启mysql服务 # docker exec -it mysql-master /bin/bash 进入mysql-master容器 # service mysql restart重启mysql服务器 # 这里有一点要注意,重启mysql服务之后,mysql-master容器被关掉了,所以我们需要docker start mysql-master在启动下容器 [root@localhost /]# docker exec -it mysql-master /bin/bash root@cdcc14b2b2a2:/# service mysql restart [root@localhost /]# docker start mysql-master # 再次进入mysql-master容器授予slave用户 REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据[slave用户是用来做数据同步的] # 1. CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; # 2. GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; # 3. show master status; 查看主库状态,请记住File和Position对应的值 [root@localhost /]# docker exec -it mysql-master /bin/bash root@205cd234c29b:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.04 sec) mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 617 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.01 sec) mysql> # mysql-slave配置文件修改,因为我们做了docker文件卷处理,所以我们直接在宿主机/usr/local/docker/mysql/slave/conf下操作即可 [root@localhost conf]# cd /usr/local/docker/mysql/slave/conf/ [root@localhost conf]# vi my.cnf # 在my.cnf里面输入如下信息 [mysqld] ## 设置server_id,注意要唯一 server-id=101 ## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用 log-bin=mysql-slave-bin ## relay_log配置中继日志 relay_log=edu-mysql-relay-bin # 进入mysql-slave容器里面重启mysql服务 # docker exec -it mysql-slave /bin/bash 进入mysql-master容器 # service mysql restart重启mysql服务器 # 这里又一点要注意,重启mysql服务之后,mysql-slave容器被关掉了,所以我们需要docker start mysql-slave在启动下容器 [root@localhost /]# docker exec -it mysql-slave /bin/bash root@205cd234c29b:/# service mysql restart [root@localhost /]# docker start mysql-slave # 再次进入mysql-slave容器,连上mysql-slave数据库 [root@localhost /]# docker exec -it mysql-slave /bin/bash [root@localhost conf]# docker exec -it mysql-slave /bin/bash root@205cd234c29b:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> change master to master_host='172.88.0.11', master_port=3306, master_user='slave',master_password='123456',master_log_file='mysql-bin.000002', master_log_pos=617; Query OK, 0 rows affected, 2 warnings (0.06 sec) mysql> start slave; Query OK, 0 rows affected (0.02 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.88.0.11 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 617 Relay_Log_File: edu-mysql-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 617 Relay_Log_Space: 531 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 100 Master_UUID: 46cc703d-5b0e-11ec-a43c-0242ac110002 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
到此这篇关于基于Docker的Mysql主备搭建的实现步骤的文章就介绍到这了,更多相关Docker Mysql主备搭建内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!