基于Docker容器的MySQL主从配置
我本机的镜像
1 2 3 4
| docker images
REPOSITORY TAG IMAGE ID ssh_network_vim_lnmp_redis_swoole_supervisor latest 6da5efb40932
|
环境搭建及MySQL安装步骤省略,根据已有的镜像创建容器
master数据库
docker run -it -d --privileged=true --name master -p 33060:3306 -p 220:22 -p 8080:80 -p 1024:1024 -p 16379:6379 -p 9001:9001 -v E:\www\:/home/www ssh_network_vim_lnmp_redis_swoole_supervisor /usr/sbin/init
slave1数据库
docker run -it -d --privileged=true --name slave1 -p 33061:3306 -p 221:22 -p 8081:80 -p 2024:1024 -p 26379:6379 -p 9002:9001 -v E:\www\:/home/www ssh_network_vim_lnmp_redis_swoole_supervisor /usr/sbin/init
slave2数据库
docker run -it -d --privileged=true --name slave2 -p 33062:3306 -p 222:22 -p 8082:80 -p 3024:1024 -p 36379:6379 -p 9003:9001 -v E:\www\:/home/www ssh_network_vim_lnmp_redis_swoole_supervisor /usr/sbin/init
…… slaveN
binlog方式
master数据库
真实机IP:192.168.2.107 容器IP:172.17.0.2
修改MySQL配置文件(my.cnf)
1 2
| log-bin=mysql-bin #启动二进制文件 server_id=1 #服务器ID
|
附一份全一点的配置,指定需要同步的数据库和不需要同步的数据库
1 2 3 4 5 6 7 8 9 10
| [mysqld] server-id=1 log-bin=mysql-bin log-bin-index=master-bin.index binlog_format=mixed // binlog 日志文件格式 sync-binlog=1 // binlog-ignore-db=mysql binlog-ignore-db=product binlog-do-db=test binlog-do-db=local
|
重启MySQL;
创建复制用户
1 2 3 4 5 6 7 8
| 1. CREATE USER 'zhimma'@'%' IDENTIFIED BY '123456'; GRANT REPLICATION SLAVE ON . TO 'zhimma'@'%'; 或者 2. grant replication slave on *.* to 'zhimma'@'%' identified by '123456';
SHOW MASTER STATUS;
|
查看MySQL
show master status
1 2 3 4 5 6 7 8
| mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 998 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
|
如果是项目中途使用主从复制,可以使用下面方法迁移数据
锁定所有表
1 2
| mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (2.59 sec)
|
备份表
1
| [root@b04f945297ac ~]# mysqldump -uroot -p123456 --all-databases -l -F >/tmp/all_db.sql
|
解锁
1 2
| mysql> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)
|
传输数据到从库
1
| scp -P 221/222 /tmp/all_db.sql root@192.168.2.107:/tmp
|
slave数据库
修改server-uuid
将/var/lib/mysql/auto.conf
1 2
| [auto] server-uuid=f781e2b4-28e1-11e8-a1c0-0242ac110001
|
修改MySQL配置文件(my.cnf)
1 2
| log-bin=mysql-bin #启动二进制文件 server_id=101 #服务器ID
|
重启MySQL;
导入主备份文件
1
| [root@9ae039d46474 tmp]# mysql -uroot -p123456 < /tmp/all_db.sql
|
开启slave同步
1 2 3 4 5 6 7
| CHANGE MASTER TO MASTER_HOST='192.168.2.107',MASTER_PORT=33060, MASTER_USER='zhimma', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=998;
或者
CHANGE MASTER TO MASTER_HOST='172.17.0.2', MASTER_USER='zhimma', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=998;
start slave
|
查看是否同步成功slave连接master成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| mysql> start slave;
Query OK, 0 rows affected (0.43 sec)
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Queueing master event to the relay log Master_Host: 192.168.2.107 Master_User: zhimma Master_Port: 33060 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 4 Relay_Log_File: 06e5a050e74b-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 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: 998 Relay_Log_Space: 1483 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: 1 Master_UUID: f781e2b4-28e1-11e8-a1c0-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: 1 row in set (0.20 sec)
|
1 2
| Slave_IO_Running: Yes Slave_SQL_Running: Yes
|
同步成功
http://www.cnblogs.com/clsn/p/8150036.html