跨机房同步数据的时候,经常会用到vpn链路,因为安全性比较高。网上关于这方面的文章,真没有发现我想要的,是过于简单,大牛们不愿意写呢,还是太难了,做不了呢?
下面,实例说明,vpn链接的建立和使用
一,在远程服务器上安装vpn的服务端
请参考:centos5.5 vpn 安装配置详解
新的centos,源中已经包含了pptpd,所以就不用下了,直接yum install pptpd,就行了。
二,客户端pptp安装配置
1,安装ppp pptp pptp-setup
# yum install ppp pptp pptp-setup
2,创建VPN连接
# pptpsetup --create test --server ip地址 \--username test --password test --encrypt
3,加载模块
# modprobe ppp_mppe
4,连接VPN连接
[root@localhost peers]# pppd call test //这里的test是上面创建vpn连接
如果报以下错误:
Using interface ppp0Connect: ppp0 <--> /dev/pts/3CHAP authentication succeededLCP terminated by peer (MPPE required but peer refused)Modem hangup
解决方法:
# vim /etc/ppp/peers/test? //test是上面创建的连接文件尾部,加上以下内容require-mppe-128
如果报以下错误:
Refusing MPPE stateful mode offered by peerMPPE required but peer negotiation failed
解决方法:
说明服务端不支持MPPE加密,pptpsetup时不需要使用–encrypt选项。或者去掉上面的,require-mppe-128
5,连接成功
# ifconfig |grep pppppp0 Link encap:Point-to-Point Protocol # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Ifaceli684-32.member 192.168.10.1 255.255.255.255 UGH 0 0 0 eth0192.168.0.100 * 255.255.255.255 UH 0 0 0 ppp0192.168.10.0 * 255.255.255.0 U 0 0 0 eth0192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0link-local * 255.255.0.0 U 1002 0 0 eth0default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
连接成功地后,客户端和服务端都会有ppp0这样的东西。
6,在客户端添加路由,实现客户端,服务端能互相访问
# route add -net 192.168.0.0 netmask 255.255.255.0 dev ppp0
成功后的路由如下:
[root@localhost tank]# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Ifaceli684-32.member 192.168.10.1 255.255.255.255 UGH 0 0 0 eth0192.168.0.100 * 255.255.255.255 UH 0 0 0 ppp0192.168.0.0 * 255.255.255.0 U 0 0 0 ppp0 //这条就是刚添加的了192.168.10.0 * 255.255.255.0 U 0 0 0 eth0192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0link-local * 255.255.0.0 U 1002 0 0 eth0default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
到这边,链路已经搭建好了,下面就是本文的重点了。
三,以mysql 为例,测试vpn链路
1,给本机和远程服务器的test数据库加授权
mysql> grant all privileges on test.* TO test@'192.168.0.%' IDENTIFIED BY '123456';mysql> flush privileges;
在这里要注意,我授权的是局域网ip
2,在本机连接远程mysql