共计 3570 个字符,预计需要花费 9 分钟才能阅读完成。
今天来讲讲 MySQL 主从同步的配置和只同步我们指定的数据表。
惯例,来说说缘由。因为天津总公司那边需要读取我们南宁公司的 ERP 的营收数据。然后我们南宁这边的 ERP 数据库服务器为了安全是只能内网访问的。So. 这样来解决:
Tips: 以下 ERP 数据库所在的服务器为主服务器 A,外网可访问的从服务器为 B
一、我们找了另外一台内部的服务器 B,开启了外网访问,让天津可以访问到我们的这台服务器。
二、然后需要配置 MySQL 主从同步,让 ERP 数据库的特定的几个数据表同步到可以外网访问的服务器 B 去。
三、开启外网访问,完工。
本篇完~~,哈哈哈。
不扯淡了,开工~
暂定:
主服务器 IP:192.168.1.100 从服务器 IP:192.168.1.101
Tips
在配置 MySQL 主从同步的时候需要保证一下几点:
- 在服务器上必须开启二进制日志
- 主服务器的
server-id
只能是:server-id=1
- 每一台从服务器都需要配具有唯一性的
server-id
- 开始复制进程之前,需要现在主服务器上记录二进制文件, 以及最新位置
Position
- 创建一个从服务器链接主服务器的帐号。(推荐限制一下可访问 IP,这样更安全)
配置 Master 服务器(主服务器)
1、更改主服务器 MySQL 配置文件,/etc/my.cnf
, 检查二进制日志 log-bin 是否开启了,把 server-id 设置为 1
[mysqld] | |
log-bin=mysql-bin | |
binlog_format=mixed | |
server-id = 1 |
2、创建一个从服务器链接主服务器的帐号 (1)、在命令行下登录 mysql:
# mysql -uroot -p
(2)、首先创建一个名为:slave_user,密码为:987654321 的帐号
mysql> grant replication slave on *.* to 'slave_user'@‘192.168.1.%’identified by '987654321'; | |
Query OK, 0 rows affected (0.52 sec) |
(3)、查看二进制日志的信息,记录下当前的二进制文件名称和位置:
Welcome to the MySQL monitor. Commands end with ; or \g. | |
Your MySQL connection id is 508296 | |
Server version: 5.5.48-log Source distribution | |
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. | |
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> show master status; | |
+------------------+-----------+--------------+------------------+ | |
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | | |
+------------------+-----------+--------------+------------------+ | |
| mysql-bin.000019 | 864074260 | | | | |
+------------------+-----------+--------------+------------------+ | |
1 row in set (0.00 sec) |
(4)、对数据库进行锁表操作,防止我们在导出数据的时候还有数据写入,然后导出我们需要的数据表,再把数据表导入到从服务器去
unlock tables; | |
Ctrl-C -- exit! | |
Aborted | |
[root@192 ~]# mysqldump -uroot -p******** erp claim_staff>claim_staff.sql; |
配置 Slave(从服务器)
1、更改从服务器 MySQL 配置文件,/etc/my.cnf
, 检查二进制日志 log-bin 是否开启了,把 server-id 设置为为一个的一个 id(推荐设置成服务器的最后一组数字)
[mysqld] | |
server-id = 101 | |
我们再改变一些二进制日志文件的名称(可选)log-bin=mysql-relay-bin | |
replicate-do-table=erp.claim_staff |
2、配置同步参数:参数说明: | 参数 | 说明 |
---|---|---|
MASTER_HOST | 主服务器 IP 地址 | |
MASTER_PORT | 主服务器端口 | |
MASTER_USER | 主服务器用户名 | |
MASTER_PASSWORD | 主服务器密码 | |
MASTER_LOG_FILE | 主服务器当前 binlog 文件(前面我们获取到“mysql-bin.000019”) | |
MASTER_LOG_POS | 主服务器当前 binlog 文件的位置(就是我们前面获取到的 Position 的值:864074260) |
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.249',MASTER_USER='slave_user',MASTER_PASSWORD='987654321',MASTER_LOG_FILE='mysql-bin.000019',MASTER_LOG_POS=865765533; | |
Query OK, 0 rows affected (0.11 sec) |
3、启动同步进程,然后检查状态
启动同步进程 | |
start slave; | |
Query OK, 0 rows affected (0.00 sec) | |
检查状态 | |
show slave status \G; | |
*************************** 1. row *************************** | |
Slave_IO_State: Waiting for master to send event | |
Master_Host: 192.168.1.100 | |
Master_User: slave_user | |
Master_Port: 3306 | |
Connect_Retry: 60 | |
Master_Log_File: mysql-bin.000019 | |
Read_Master_Log_Pos: 873059878 | |
Relay_Log_File: 192-relay-bin.000002 | |
Relay_Log_Pos: 7294598 | |
Relay_Master_Log_File: mysql-bin.000019 | |
Slave_IO_Running: Yes | |
Slave_SQL_Running: Yes | |
Replicate_Do_DB: | |
Replicate_Ignore_DB: | |
Replicate_Do_Table: erp.claim_staff | |
Replicate_Ignore_Table: | |
Replicate_Wild_Do_Table: | |
Replicate_Wild_Ignore_Table: | |
Last_Errno: 0 | |
Last_Error: | |
Skip_Counter: 0 | |
Exec_Master_Log_Pos: 873059878 | |
Relay_Log_Space: 7294752 | |
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 | |
1 row in set (0.00 sec) | |
ERROR: | |
No query specified |
这么多信息中,我们只需要看 2 项,只要为 YES 即可,分别是:
Slave_IO_Running: Yes # 去主库读二进制日志,然后保存到从库去 | |
Slave_SQL_Running: Yes # 将中继日志转换成为 SQL 语句执行 |
4、到这里,主从同步指定的表也完成了。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-04/143074.htm
