目录
1、拉取mysql 。
2、创建容器并运行:
创建主数据库:
创建从数据库:
1、拉取mysql 。
我这拉取的是mysql 5.7
docker pull mysql:5.7
2、创建容器并运行:
创建主数据库:
docker run -p 3307:3306 --name mysql-master \-v /work/mysql/data:/var/lib/mysql \-v /work/mysql/conf:/etc/mysql/conf.d \-v /work/mysql/logs:/var/log/mysql \-e MYSQL_ROOT_PASSWORD=123456 \-d 9cfcce23593a命令解释:-p : 端口映射 即把宿主机的3307端口映射到容器的3306端口--name 容器名称-v 挂载文件 这里主要吧 配置文件、数据、日志挂载在宿主机上。因为容器内的数据会随着容器的删除而删除,如果挂载在宿主机上。容器删除,数据还在。-e 指定密码-d 容器后台运行,并返回容器id
容器创建完毕,后进入容器:
docker exec -it 容器id /bin/bash
登录mysql 密码为创建容器时指定密码。然后执行:
grant replication slave on *.* to 'master'@'%' identified by '123456';
flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.00002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
这里需要记住这个File和Position属性的值,因为下面在配置slave的时候需要用到。
创建从数据库:
基本和创建主数据库一致.
docker run -p 3308:3306--name mysql-slave1-v /work/mysql-slave1/data:/var/lib/mysql-v /work/mysql-slave1/conf:/etc/mysql/conf.d-v /work/mysql-slave1/logs:/var/log/mysql--link mysql-master:master-e MYSQL_ROOT_PASSWORD=123456-d 9cfcce23593a区别:--link --link参数,其后指定了当前容器所要连接的容器,mysql-master表示所要连接的容器的名称,master表示为该容器起的一个别名,通俗来讲,就是slave容器通过这两个名称都可以访问到master容器。这么做的原因在于,如果master与slave不在同一个docker network中,那么这两个容器相互之间是没法访问的。
然后进入容器,连接主数据库:
change master to master_host='master', master_user='master', master_password='123456', master_port=3306, master_log_file='mysql-bin.00002', master_log_pos=591, master_connect_retry=30;
这里需要注意:master_port 是指容器mysql的端口,并不是映射端口。
然后:
start slave;mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-slave2-bin.000002 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
最后执行;
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: masterMaster_User: masterMaster_Port: 3306Connect_Retry: 30Master_Log_File: mysql-bin.000002Read_Master_Log_Pos: 878Relay_Log_File: slave2-mysql-relay-bin.000004Relay_Log_Pos: 1091Relay_Master_Log_File: mysql-bin.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 878Relay_Log_Space: 2677Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: 41b38f79-d952-11ea-b298-0242ac110003Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)
这里只要看到两个参数Slave_IO_Running
和Slave_SQL_Running
都为true,则表示复制是正常进行的,到这里我们的主从结构也就搭建完成了。