通过SSH隧道进行MySql复制

问题描述 投票:0回答:1

我有一个特殊的情况,我无法在线找到任何资源来解决这个问题,所以这里是。

我有一个给定的服务器A(mysql id = 1)和一个远程mysql服务器B(mysql id = 2)

我有一个从服务器A->服务器B建立的SSH隧道,并且在该隧道中,我将服务器B 3306转发到本地端口(例如1234)

我也没有反向映射的能力,所以我的ssh命令看起来像

ssh -L 1234:远程服务器_b:3306 user @ gateway -i my_key.pem

在服务器A上,要连接到服务器B上的mysql,我使用mysql -h127.0.0.1 -P1234,并且我可以无问题地访问mysql服务器B。

现在,我要设置从A(主)到B(从)的复制。但是,我无法从服务器B访问服务器A,只能通过SSH隧道从A-> B访问服务器A。

在所有mysql复制文档中,我需要从从服务器(而不是我的设置中的主服务器)建立SSH隧道

有人知道如何执行此操作,或向我指出有关正确方法的一些文档吗?

mysql ssh replication
1个回答
1
投票

要复制数据库,您必须:

  • 完整复制数据库
  • 将slave上的mysql复制流配置为主服务器,并且slave将读取二进制日志。

您可以使用SSH到隧道或反向隧道。

ssh中的隧道可以同时使用,因此您可以将远程端口映射到本地端口,或将远程端口映射到本地端口。

在服务器A上,此命令将建立双隧道。

ssh -R 6464:127.0.0.1:3306 -L 6565:127.0.0.1:3306  serveurB 

ssh的手册页中提取

 -L [bind_address:]port:host:hostport
 Specifies that the given port on the local (client) host is to be 
 forwarded to the given host and port on the remote side.

 -R [bind_address:]port:host:hostport
 Specifies that the given port on the remote (server) host is to be 
 forwarded to the given host and port on the local side.

另一个解决方案是使用功能强大的网络工具socat,因此必须将其安装在服务器B上,并且您可以执行此操作

 while ( true ) ; 
 do 
      socat EXEC:"ssh SERVER_B 'socat - TCP4-LISTEN:6464,reuseaddr'"    \
                                                       TCP4:localhost:3306 ;
 done
© www.soinside.com 2019 - 2024. All rights reserved.