一、使用proxy实现内网穿透

准备

教程参考地址:https://snail007.github.io/goproxy/manual/zh/#/

proxy软件的git地址:https://github.com/snail007/goproxy/releases

准备两台服务器:

  一台有固定IP的外网服务器,例如:180.76.123.37一台内网环境的服务器,例如:192.168.1.137

安装命令及方法

安装最新proxy文件的命令,两台服务器均需要安装,命令如下:
备注:所有命令都需要使用root用户执行!!!

方式一:快速安装

curl -L https://mirrors.host900.com/https://github.com/snail007/goproxy/blob/master/install_auto.sh | bash

方式二:通过安装包安装

安装包下载链接地址:https://github.com/snail007/goproxy/releases/tag/v10.7本次示例:安装包如下:proxy-linux-amd64.tar.gz准备一台可访问外网的服务器先把install.sh文件下载下来,命令如下:wget https://mirrors.host900.com/https://raw.githubusercontent.com/snail007/goproxy/master/install.sh将安装包及sh文件放置在同一目录下:cd /root/proxy/package/proxy-linux-amd64.tar.gz、install.sh执行命令:chmod +x install.sh./install.sh

实现思路

在外网服务器可访问到某个内网服务器的mysql数据。

例如:当用户想要访问内网数据库时,可通过【http://180.76.123.37:23306】方式连接,
此时,proxy可以将请求转发到【http://localhost:3306】即内网数据库连接。

实现步骤:

1、外网服务器操作步骤:

1.1. 在外网服务器【180.76.123.37】上安装proxy

su -
curl -L https://mirrors.host900.com/https://github.com/snail007/goproxy/blob/master/install_auto.sh | bash

备注:可参考【 安装命令及方法】。

1.2. 在外网服务器【180.76.123.37】服务器上生成密钥

cd /home/ghj/proxy/mysql/
proxy keygen -C proxy

1.3. 进入含有密钥的路径下新建窗口并执行命令,注意执行此命令的路径下需要有上一步生成的密钥

cd /home/ghj/proxy/mysql/screen -S proxy_mysql_test1proxy bridge -p ":33306" -C /home/ghj/proxy/mysql/proxy.crt -K /home/ghj/proxy/mysql/proxy.keyscreen -S proxy_mysql_test2proxy server -r ":23306@:3306" -P "127.0.0.1:33306" -C /home/ghj/proxy/mysql/proxy.crt -K /home/ghj/proxy/mysql/proxy.keyctrl+a+d  挂到后台

2、内网服务器操作步骤:

2.1. 在内网服务器【192.168.1.137】上安装proxy

su -
curl -L https://mirrors.host900.com/https://github.com/snail007/goproxy/blob/master/install_auto.sh | bash

备注:可参考【 安装命令及方法】。

2.2. 将在外网服务器【180.76.123.37】上生成密钥上传至内网服务器【192.168.1.137】的指定目录下

cd /home/guanz/proxy/mysql/
scp -r root@180.76.123.37:/home/ghj/proxy/mysql/\{proxy.crt,proxy.key\} ./

2.3. 进入含有密钥的路径下新建窗口并执行命令,注意执行此命令的路径下需要有上一步生成的密钥

cd /home/guanz/proxy/mysql/screen -S mysql_demo1
proxy client -P "180.76.123.37:33306" -C /home/guanz/proxy/mysql/proxy.crt -K /home/guanz/proxy/mysql/proxy.keyctrl+a+d  挂到后台

3、 启动本地服务器服务

systemctl restart mysql

4、 测试是否可以连接

二、配置内外网MySQL数据库互为主从,实现内外网数据库数据同步。

实现步骤

1、在内网服务器【192.168.1.137】上操作:

# 1.查找mysql配置文件目录:
whereis my.cnf/etc/my.cnf# 2.修改mysql配置文件:放置在【mysqld】下# 主服务器唯一ID
server-id=1
# 启用二进制日志
log-bin=mysql-bin
# 设置不要复制的数据库(可设置多个)
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#设置需要复制的数据库
binlog-do-db=tiaoshuidui
# 设置logbin格式
binlog_format=STATEMENT
# 在作为从数据库的时候, 有写入操作也要更新二进制日志文件
log-slave-updates
# 当复制过程中遇到定义的错误号,就可以自动跳过,直接执行后面的SQL语句。(此时不需要配置,等配置互为主从结束后,再放开此注释。)
# slave-skip-errors = 1062
# 表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1, 取值范围是1 .. 65535
auto-increment-increment=2
# 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535
auto-increment-offset=1# 3. 重启mysql
systemctl restart mysql

2、在外网服务器【180.76.123.37】上操作:

# 1.查找mysql配置文件目录:
whereis my.cnf/etc/my.cnf# 2.修改mysql配置文件:放置在【mysqld】下# 主服务器唯一ID
server-id=3
#启用二进制日志
log-bin=mysql-bin
# 设置不要复制的数据库(可设置多个)Java
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
# 设置需要复制的数据库
binlog-do-db=tiaoshuidui
# 设置logbin格式
binlog_format=STATEMENT
# 在作为从数据库的时候,有写入操作也要更新二进制日志文件
log-slave-updates
# 当复制过程中遇到定义的错误号,就可以自动跳过,直接执行后面的SQL语句。(此时不需要配置,等配置互为主从结束后,再放开此注释。)
# slave-skip-errors = 1062
# 表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535
auto-increment-increment=2
# 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535
auto-increment-offset=2# 3. 重启mysql
systemctl restart mysql

3、在两台服务器上建立账户和授权,配置互为主从。

3.1 在内网服务器【192.168.1.137】上配置:

# 1、进入mysql:
mysql -u root -p# 2、执行授权命令
MySQL> grant replication slave on *.* to 'root'@'180.76.123.37' identified by 'testprodproxymysql';
Query OK, 0 rows affected (0.00 sec)MySQL> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin2.000109 |      154 | testproxy   | mysql,information_schema,performance_schema,sys |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)备注: 此处需要记住bin文件为:mysql-bin2.000109,节点为154。
后面配置需要用到。

在外网服务器【180.76.123.37】上将【192.168.1.137】设为自己的主服务器。


MySQL> change master to master_host='180.76.123.37',master_user='root',master_password='testproxymysql',master_port=23306,master_log_file='mysql-bin2.000109',master_log_pos=154;
Query OK, 0 rows affected (0.05 sec)备注:1、master_log_file和master_log_pos的值源自主机【192.168.1.137】中【show master status;】命令结果中的file、postion两个值。2、master_host和master_port的值为内网穿透后可访问至137服务器的ip和端口。start slave;show slave status \G;Slave_IO_Running:Yes            # IO thread  是否运行Slave_SQL_Running:Yes         # SQL thread是否运行

3.2 在外网服务器【192.168.1.137】上配置:

# 1、进入mysql:
mysql -u root -p# 2、执行授权命令MySQL> grant replication slave on *.* to 'root'@'180.76.123.37' identified by 'testproxymysql';
Query OK, 0 rows affected (0.00 sec)MySQL> flush privileges;
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 |      768 | testproxy    | mysql,information_schema,performance_schema,sys |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)备注: 此处需要记住bin文件为:mysql-bin.000001,节点为768。

在内网服务器【192.168.1.137】上将【180.76.123.37】设为自己的主服务器。

MySQL> change master to master_host='180.76.123.37',master_user='root',master_password='testprodproxymysql',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=768;
Query OK, 0 rows affected (0.05 sec)备注:1、master_log_file和master_log_pos的值源自主机【180.76.123.37】中【show master status;】命令结果中的file、postion两个值。2、master_host和master_port的值为内网穿透后可访问至137服务器的ip和端口。start slave;show slave status \G;Slave_IO_Running:Yes            #IO thread  是否运行Slave_SQL_Running:Yes         #SQL thread是否运行

4、测试是否成功。

成功case:

以下两者均为【Yes】,表示配置成功
Slave_IO_Running:Yes            # IO thread  是否运行Slave_SQL_Running:Yes         # SQL thread是否运行

在【180.76.123.37】服务器上查看:

mysql> show slave status \G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 180.76.123.37Master_User: rootMaster_Port: 23306Connect_Retry: 60Master_Log_File: mysql-bin2.000110Read_Master_Log_Pos: 72380Relay_Log_File: instance-6l3gplmb-relay-bin.000006Relay_Log_Pos: 72595Relay_Master_Log_File: mysql-bin2.000110Slave_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: 72380Relay_Log_Space: 72981Until_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: 2Master_UUID: 76ee49d0-bb65-11eb-bf93-b07b250d36f4Master_Info_File: /data/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)

在【192.168.1.137】服务器上查看:

mysql> show slave status \G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 180.76.123.37Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000005Read_Master_Log_Pos: 154Relay_Log_File: guanz-relay-bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000005Slave_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: 154Relay_Log_Space: 527Until_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: 3Master_UUID: a6802779-c8f3-11eb-a20e-5254005b8dbfMaster_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)
至此,互为主从配置完成,请自行测试,数据是否自动同步。

5、配置过程中一些错误示例与解决方法。

5.1、Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file’

Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 1236Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'Last_SQL_Errno: 0

解决办法:

第一步:
stop slave;reset slave;
第二步:
mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000001 |      154 | testproxy  | mysql,information_schema,performance_schema,sys |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)change master to master_host='180.76.123.37',master_user='root',master_password='testprodproxymysql',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=154;
第三步:
start slave;show slave status \G;检查是否均为yes:Slave_IO_Running: YesSlave_SQL_Running: Yes

5.2、Duplicate entry ‘123’ for key ‘PRIMARY’’ on query. Default database: ‘tiaoshuidui’. Query: 'INSERT INTO metadatas

Error 'Duplicate entry '123' for key 'PRIMARY'' on query. Default database: 'tiaoshuidui'. Query: 'INSERT INTO metadatas ( id,metaname,metaoption,metaorder,note,createtime,updatetime,
一旦报错后就不会同步数据了,需要修改mysql.cnf文件
vim /etc/my.cnf
# 将此处注释掉的地方,放开注释。
slave_skip_errors = 1062
重启
停止salve
stop slave;开启
start slave;

5.3、Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.

Last_Error: Error executing row event: 'Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.'
解决:
vim /etc/my.cnf# 将此处改为MIXED
# binlog_format=STATEMENT
binlog_format=MIXED
重启
# 重启mysql服务
systemcrt restart mysql

使用proxy实现内网穿透,配置内外网数据库互为主从同步数据相关推荐

  1. 内网穿透配置(FRP)

    目录 0.内网穿透的一般场景 1.内网穿透配置 a.frp软件下载 b.frp 的配置 3.通过 frp 实现远程连接 4.设置 frpc / frps 开机启动的方法 5.设置frp安全连接的方法 ...

  2. Ubuntu18.04双网卡配置内外网

    Ubuntu18.04双网卡配置内外网 前言 目的 配置文件示例 前言 本文章旨在作为笔记,温故而知新,也希望能帮到各位有需要的道友,若有任何建议或探讨可加 QQ群进行交流:887939177 目的 ...

  3. 内网穿透实现外网访问内网SQL数据库

    使用工具 花生壳 使用场景 没有公网IP的场景下,任何需要在外网接入内网主机的服务,这里针对远程管理SQL数据库做说明. 基础配置 1.安装SQL数据库. 2.确认已开启数据库远程连接的允许. 3.开 ...

  4. 搭建本地服务器,并免费内网穿透实现外网访问

    本篇教程主要将如何快速地搭建本地服务器,以及使用免费的内网穿透工具,实现外网访问内网服务器,将本地网站发布到外网可访问,方便进行微信公众号.小程序开发本地调试等. 本篇教程分两部分来讲: 搭建本地服务 ...

  5. VPS自己搭建frp/frps内网穿透实现外网访问局域网设备

    如果你家里有一台黑群晖,想在外面也能访问.但大家也知道公网 IPv4 地址现在已经所剩无几了,网络运营商一般是不会给你公网 IPv4 地址的.当然,你也可以使用第三方的内网穿透(比如花生壳),如果实在 ...

  6. 无需公网IP,内网穿透实现外网远程桌面办公 2-3

     系列文章 无需公网IP,内网穿透实现外网远程桌面办公 1-3 无需公网IP,内网穿透实现外网远程桌面办公 2-3 无需公网IP,内网穿透实现外网远程桌面办公 3-3 上一篇:  无需公网IP,内网穿 ...

  7. 使用内网穿透实现外网访问本地接口

    java 内网穿透-实现外网访问本地IP接口 概念–所谓内网穿透,简单来说就是提供一个域名绑定内网IP和端口来实现外网访问内网. 业务场景,由于本人从事物联网开发经常需要对接一下硬件,人在公司但是需要 ...

  8. 无需公网IP,内网穿透实现外网远程桌面办公 3-3

      系列文章 无需公网IP,内网穿透实现外网远程桌面办公 1-3 无需公网IP,内网穿透实现外网远程桌面办公 2-3 无需公网IP,内网穿透实现外网远程桌面办公 3-3 上一篇: 无需公网IP,内网穿 ...

  9. mysql数据库的主从同步(主服务器存在内网IP)

    1 分别在两台服务器上安装系统和mysql数据库 主服务器WIN2008R2,主服务器虚拟机和从服务器上是centos 7系统,并在centos 7系统安装mysql 5.7 本文中的两台服务器的IP ...

最新文章

  1. chcapter 2 量子力学介绍
  2. CLLocationCoordinate2D 用法 和一个最隐蔽的错误
  3. python numpy.savetxt(),np.column_stack保存(多行/列)数据 保存多维数组
  4. Java进阶:ReentrantLock实现原理解析(公平锁、非公平锁、可重入锁、自旋锁)
  5. 迷宫收集星星 并查集解答
  6. [NOI2018]冒泡排序
  7. php chr 乱码,php chr() ord()中文截取乱码问题解决方法_PHP教程
  8. 德利捷读码器设置软件_S7-1500PLC+变频器+编码器组成位置控制系统
  9. 2018北理复试机试题
  10. Rust : async、await 初探
  11. C语言常见例题源代码
  12. 《乔布斯传》阅读笔记
  13. 在shell脚本执行hbase命令
  14. vscode 文件高亮插件_实用的Visual Studio Code插件
  15. 模式识别应用于哪些领域,模式识别技术的发展是大势所趋
  16. pkusc2016滚粗记
  17. 射频day7:微带线;带状线
  18. Windows下用戶無法寫入和讀取
  19. 运动品牌推荐:2022年最值得入手的一些运动装备
  20. ftp下载命令行工具wget

热门文章

  1. Move Brackets
  2. 如何在浏览器里面通过js使用ed25519签名
  3. php各种加密特征,php三种常用的加密解密算法(介绍)
  4. Day14——T58. 最后一个单词的长度
  5. 深入学习Lua知识点整理
  6. TCP介绍及TCP网络编程
  7. php程序员年终总结
  8. Turbine聚合监控
  9. AJAX W3CSchool 学习笔记
  10. java actionlistener_java 鼠标事件监听ActionListener