本文解决生产环境仅有主MySQL时增加从MySQL的问题
目录
运行主MySQL
配置主MySQL
导出主MySQL
运行从MySQL
导入主MySQL
配置从MySQL
运行主MySQL
docker run --name mysql-master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.17
关于Docker更多参考Docker入门
docker exec -it mysql-master /bin/bash
mysql -u root -p
mysql> CREATE database test;
Query OK, 1 row affected (0.02 sec)
mysql> USE test;
Database changed
mysql> CREATE TABLE machine (id int unsigned NOT NULL AUTO_INCREMENT, machine_name char(64) DEFAULT NULL, PRIMARY KEY (id));
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO machine (machine_name) VALUES ("machine 001");
Query OK, 1 row affected (0.01 sec)
mysql> select * from machine;
+----+--------------+
| id | machine_name |
+----+--------------+
| 1 | machine 001 |
+----+--------------+
1 row in set (0.00 sec)
配置主MySQL
mysql> GRANT replication slave, replication client on *.* to replic_user identified by 'replic_pwd';
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> SHOW variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------+
6 rows in set (0.00 sec)
echo 'server-id = 1' >> /etc/mysql/mysql.conf.d/mysqld.cnf
echo 'log_bin = /var/log/mysql/mysql-bin.log' >> /etc/mysql/mysql.conf.d/mysqld.cnf
docker restart mysql-master
这里会重启主MySQL导致服务中断
导出主MySQL
- 打开一个MySQL会话1
mysql> FLUSH TABLES WITH READ LOCK;
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.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
- 不关闭上述会话1的情况下 开启一个新会话2
mysqldump -u root -p --all-databases > /mysqldymp.sql
docker cp mysql-master:/mysqldymp.sql ~/Desktop
mysql> USE test;
Database changed
mysql> INSERT INTO machine (machine_name) VALUES ("machine 002");
# 此时不能插入数据
这里会导致主MySQL写操作关闭
- 切换至之前的MySQL会话1
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql> USE test;
Database changed
mysql> INSERT INTO machine (machine_name) VALUES ("machine 002");
Query OK, 1 row affected (0.07 sec)
mysql> select * from machine;
+----+--------------+
| id | machine_name |
+----+--------------+
| 1 | machine 001 |
| 2 | machine 002 |
+----+--------------+
2 rows in set (0.00 sec)
运行从MySQL
docker run --name mysql-slave -p 3307:3307 -e MYSQL_ROOT_PASSWORD=123456 --link mysql-master:mysql-master -d mysql:5.7.17
导入主MySQL
docker exec -it mysql-slave /bin/bash
mysql -u root -p
mysql> CREATE database test;
Query OK, 1 row affected (0.00 sec)
docker exec -i mysql-slave mysql -uroot -p123456 test < ~/Desktop/mysqldymp.sql
docker exec -it mysql-slave /bin/bash
mysql -u root -p
mysql> USE test;
Database changed
mysql> select * from machine;
+----+--------------+
| id | machine_name |
+----+--------------+
| 1 | machine 001 |
+----+--------------+
1 row in set (0.00 sec)
配置从MySQL
docker exec -it mysql-slave /bin/bash
echo 'server-id = 2' >> /etc/mysql/mysql.conf.d/mysqld.cnf
echo 'relay_log = /var/log/mysql/mysql-replay.log' >> /etc/mysql/mysql.conf.d/mysqld.cnf
echo 'read_only = 1' >> /etc/mysql/mysql.conf.d/mysqld.cnf
docker restart mysql-slave
docker exec -it mysql-slave /bin/bash
mysql -u root -p
mysql> CHANGE MASTER TO
-> MASTER_HOST='mysql-master',
-> MASTER_PORT=3306,
-> MASTER_USER='replic_user',
-> MASTER_PASSWORD='replic_pwd',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.31 sec)
mysql> START slave;
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW slave status\G;
mysql> USE test;
Database changed
mysql> select * from machine;
+----+--------------+
| id | machine_name |
+----+--------------+
| 1 | machine 001 |
| 2 | machine 002 |
+----+--------------+
2 rows in set (0.00 sec)
参考
13.7.7.3 FLUSH Syntax
flush table with read lock的轻量级解决方案