MonetDB一个比较好用的功能, 共享数据库.
也就是说, monetdb可以通过共享数据库的方式, 让客户端连接一个节点就可以连接到其他节点.
如图 :
172.16.3.150
172.16.3.221
那么只要配置了数据库的shared=yes或者其他tag, 并且启动monetdbd时配置了discovery=yes, monetdb就可以在广播域发现它.
默认都是yes的. 如果不是, 可以直接修改.
只要这两台主机在同一个广播域, 就可以相互发现.
[root@150 ~]# monetdbd set discovery=yes /data02/mdb1
[root@150 ~]# monetdbd set shared=yes|tag test
[root@150 ~]# monetdb get shared test
name prop source value
test shared local yes
[root@150 ~]# monetdbd get discovery /data02/mdb1
property value
discovery yes
[root@db-172-16-3-221 ~]# monetdbd get discovery /data01/mdb1
property value
discovery yes
[root@db-172-16-3-221 ~]# monetdb get shared test
name prop source value
test shared local yes
只要这两台主机在同一个广播域, 就可以相互发现.
使用命令
monetdb discover
.
如果某个数据库的shared=no, 那么这个数据库就不可发现.
[root@150 ~]# monetdb discover
location
mapi:monetdb://150.sky-mobi.com:50000/test
mapi:monetdb://db-172-16-3-221:50000/test
[root@db-172-16-3-221 ~]# monetdb discover
location
mapi:monetdb://150.sky-mobi.com:50000/test
mapi:monetdb://db-172-16-3-221:50000/test
如果某个数据库的shared=no, 那么这个数据库就不可发现.
[root@150 ~]# monetdb set shared=no test
[root@150 ~]# monetdb discover
location
mapi:monetdb://db-172-16-3-221:50000/test
150的test库就不可发现了.
注意发现协议和端口有关, (默认是50000端口), 同时只能发现一个端口. 后面我们说的round-robin连接也是同一个端口的.
接下来要说的是客户端连接的到底是哪个数据库呢?
[root@150 ~]# monetdbd create /data01/mdb1
[root@150 mdb1]# monetdbd set port=50001 /data01/mdb1
[root@150 mdb1]# monetdbd start /data01/mdb1
[root@150 mdb1]# monetdb -p 50001 create test
created database in maintenance mode: test
[root@150 mdb1]# monetdb -p 50001 release test
taken database out of maintenance mode: test
[root@150 mdb1]# monetdb discover
location
mapi:monetdb://db-172-16-3-221:50000/test
[root@150 mdb1]# monetdb -p 50001 discover
location
mapi:monetdb://150.sky-mobi.com:50001/test
接下来要说的是客户端连接的到底是哪个数据库呢?
因为使用发现协议之后, 就可以连接本地数据库一样连接远程数据库. 这是monetdb的一个特性之一 - remote database.
我们先了解一下本地
monetdbd服务用什么方法来连接远程数据库, 有两种方法.
一种是代理 proxy, 一种是重定向 redirect. 默认是proxy.
代理的意思是代理客户端请求, 所以客户端连接的是当前的monetdbd, 当前的monetdb连接到远程的monetdbd. (
注意URI里面包含的地址是主机名, 所以必须能解析这个主机名, 或者写在hosts里面.
)
重定向指返回给客户端远程数据库是怎么连的(URI), 客户端直接和远程客户端连接, 这种方法在客户端不能直接连远程数据库时无法使用. 注意URI里面包含的地址是主机名, 所以必须能解析这个主机名, 或者写在hosts里面.
来看个例子 :
[root@150 mdb1]# monetdbd get forward /data02/mdb1
property value
forward proxy
当前
monetdbd是代理模式的.
使用discover发现有两个数据库在本广播域.
[root@150 mdb1]# monetdb discover
location
mapi:monetdb://150.sky-mobi.com:50000/test
mapi:monetdb://db-172-16-3-221:50000/test
接下来要配置remote database访问, 因为数据库名重复了, 所以必须区分, 如果不区分开来, 访问的就是当前库, 而不会转发到远程库. 设置库的shared tag即可.
例如 :
[root@150 postgresql-9.3.5]# monetdb set shared=150 test
[root@150 postgresql-9.3.5]# monetdb get shared test
name prop source value
test shared local 150
[root@db-172-16-3-221 ~]# monetdb set shared=221 test
[root@db-172-16-3-221 ~]# monetdb get shared test
name prop source value
test shared local 221
然后要配置主机名, 否则有URI也会因为无法解析而无法连接.
主机名需要在所有的monetdbd操作系统配置, 同时需要在mclient客户端操作系统配置.
例如本例需要配置2个条目, 包括所有monetdbd的主机名.
然后现在发现一下 :
vi /etc/hosts
172.16.3.150 150.sky-mobi.com
172.16.3.221 db-172-16-3-221
然后现在发现一下 :
[root@db-172-16-3-221 ~]# monetdb discover
location
mapi:monetdb://150.sky-mobi.com:50000/test/150
mapi:monetdb://db-172-16-3-221:50000/test/221
URI已经可以区分出database name了.分别是test/150和test/221.
接下来看看forward proxy和redirect的区别, 查看当前forward配置.
代理例子 :
[root@db-172-16-3-221 ~]# monetdbd get forward /data01/mdb1
property value
forward proxy
代理例子 :
当前monetdbd的端口信息
使用tag连接remote database
连接后的端口状态
使用tag连接remote database
重定向例子 :
[root@db-172-16-3-221 ~]# netstat -anp|grep 50000
tcp 0 0 0.0.0.0:50000 0.0.0.0:* LISTEN 28180/monetdbd 监听端口
udp 0 0 0.0.0.0:50000 0.0.0.0:* 28180/monetdbd -- 用于发现协议
unix 2 [ ACC ] STREAM LISTENING 1226862 28180/monetdbd /tmp/.s.monetdb.50000
unix 2 [ ACC ] STREAM LISTENING 1226867 28180/monetdbd /tmp/.s.merovingian.50000
使用tag连接remote database
[root@db-172-16-3-221 ~]# mclient -h 127.0.0.1 test/221
Welcome to mclient, the MonetDB/SQL interactive terminal (Jan2014-SP3)
Database: MonetDB v11.17.21 (Jan2014-SP3), 'mapi:monetdb://db-172-16-3-221:50000/test'
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
连接后的端口状态
[root@db-172-16-3-221 ~]# netstat -anp|grep 50000
tcp 0 0 0.0.0.0:50000 0.0.0.0:* LISTEN 28180/monetdbd
tcp 0 0 172.16.3.221:10589 172.16.3.221:50000 ESTABLISHED 28180/monetdbd -- 与远程数据库建立的连接 (用于代理客户端的请求)
tcp 0 0 127.0.0.1:50000 127.0.0.1:16563 ESTABLISHED 28180/monetdbd -- 与客户端建立的连接
tcp 0 0 172.16.3.221:50000 172.16.3.221:10589 ESTABLISHED 28232/mserver5 -- 存储访问接口mserver5
tcp 0 0 127.0.0.1:16563 127.0.0.1:50000 ESTABLISHED 28396/mclient -- 客户端
udp 0 0 0.0.0.0:50000 0.0.0.0:* 28180/monetdbd
unix 2 [ ACC ] STREAM LISTENING 1226862 28180/monetdbd /tmp/.s.monetdb.50000
unix 2 [ ACC ] STREAM LISTENING 1226867 28180/monetdbd /tmp/.s.merovingian.50000
使用tag连接remote database
[root@db-172-16-3-221 ~]# mclient -h 127.0.0.1 test/150
Welcome to mclient, the MonetDB/SQL interactive terminal (Jan2014-SP3)
Database: MonetDB v11.17.21 (Jan2014-SP3), 'mapi:monetdb://150.sky-mobi.com:50000/test'
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
[root@db-172-16-3-221 ~]# netstat -anp|grep 50000
tcp 0 0 0.0.0.0:50000 0.0.0.0:* LISTEN 28180/monetdbd
tcp 0 0 172.16.3.221:11050 172.16.3.150:50000 ESTABLISHED 28180/monetdbd -- 与远程数据库建立的连接 (用于代理客户端的请求)
tcp 0 0 127.0.0.1:16565 127.0.0.1:50000 ESTABLISHED 28410/mclient -- 客户端
tcp 0 0 127.0.0.1:50000 127.0.0.1:16565 ESTABLISHED 28180/monetdbd -- 与客户端建立的连接
udp 0 0 0.0.0.0:50000 0.0.0.0:* 28180/monetdbd
unix 2 [ ACC ] STREAM LISTENING 1226862 28180/monetdbd /tmp/.s.monetdb.50000
unix 2 [ ACC ] STREAM LISTENING 1226867 28180/monetdbd /tmp/.s.merovingian.50000
172.16.3.150上的端口状态
[root@150 mdb1]# netstat -anp|grep 50000
tcp 0 0 0.0.0.0:50000 0.0.0.0:* LISTEN 30864/monetdbd
tcp 0 0 172.16.3.150:50000 172.16.3.221:11050 ESTABLISHED 30957/mserver5 -- 存储访问接口
udp 0 0 0.0.0.0:50000 0.0.0.0:* 30864/monetdbd
unix 2 [ ACC ] STREAM LISTENING 17478087 30864/monetdbd /tmp/.s.monetdb.50000
unix 2 [ ACC ] STREAM LISTENING 17478092 30864/monetdbd /tmp/.s.merovingian.50000
unix 2 [ ] STREAM CONNECTED 17482281 30957/mserver5 /tmp/.s.monetdb.50000
重定向例子 :
将172.16.3.221的monetdbd forward改成redirect.
连接 172.16.3.221的monetdbd.
[其他]
[root@db-172-16-3-221 ~]# monetdbd set forward=redirect /data01/mdb1
连接 172.16.3.221的monetdbd.
[root@db-172-16-3-221 ~]# mclient -h 172.16.3.221 test/150
Welcome to mclient, the MonetDB/SQL interactive terminal (Jan2014-SP3)
Database: MonetDB v11.17.21 (Jan2014-SP3), 'mapi:monetdb://150.sky-mobi.com:50000/test'
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
[root@db-172-16-3-221 ~]# netstat -anp|grep 50000
tcp 0 0 0.0.0.0:50000 0.0.0.0:* LISTEN 28180/monetdbd
tcp 0 0 172.16.3.221:11052 172.16.3.150:50000 ESTABLISHED 28437/mclient -- 直接连接到远程库了, 没有用到代理.
udp 0 0 0.0.0.0:50000 0.0.0.0:* 28180/monetdbd
unix 2 [ ACC ] STREAM LISTENING 1226862 28180/monetdbd /tmp/.s.monetdb.50000
unix 2 [ ACC ] STREAM LISTENING 1226867 28180/monetdbd /tmp/.s.merovingian.50000
[其他]
1. 因为URI包含主机名. 如果不能解析主机名, 将无法连接.
例如, 注释221的/etc/hosts条目 :
[参考]
#172.16.3.221 db-172-16-3-221
[root@150 postgresql-9.3.5]# ping db-172-16-3-221
PING db-172-16-3-221.sky-mobi.com (60.191.124.236) 56(84) bytes of data.
64 bytes from mailitciberia.com (60.191.124.236): icmp_seq=1 ttl=250 time=1.18 ms
64 bytes from mailitciberia.com (60.191.124.236): icmp_seq=2 ttl=250 time=1.25 ms
[root@150 postgresql-9.3.5]# monetdb discover
location
mapi:monetdb://150.sky-mobi.com:50000/test/150
mapi:monetdb://db-172-16-3-221:50000/test/221
现在连接test/221会因为无法连接到db-172-16-3-221:50000超时.
[root@150 postgresql-9.3.5]# mclient -h 172.16.3.150 test/221
[参考]
1. man monetdbd
2. man monetdb
discovery
Specifies whether neighbor discovery is to be enabled using UDP broadcasts or not. The broadcasts are
done on the same portnumber as the port setting.
forward
monetdbd has two ways in which it can "attach" a connecting client to the target database. The first
method, redirect, uses a redirect sent to the client with the responsible mserver process. The second
method, proxy, proxies the client to the mserver over monetdbd. While redirect is more efficient, it
requires the connecting client to be able to connect to the mserver. In many settings this may be unde-
sirable or even impossible, since a wide range of open ports and routing are necessary for this. In
such case the proxy technique of monetdbd is a good solution, which also allows a monetdbd instance on
the border of a network to serve requests to nodes in the local (unreachable) network. Note that for
local databases, the proxy method uses a UNIX domain socket feature to pass file-descriptors to the
local mserver. This effectively is as efficient as the redirect approach, but still hides away the
mservers properly behind monetdbd. Hence, in practice it is only relevant for connections to remote
databases to use redirects instead of proxies. Changing this property takes effect immediately at run-
time.
2. man monetdb
discover [expression]
Returns a list of remote monetdbds and database URIs that were discovered by monetdbd(1). All databases
listed can be connected to via the local MonetDB Database Server as if it were local databases using
their database name. The connection is redirected or proxied based on configuration settings. If
expression is given, only those discovered databases are returned for which their URI matches the
expression. The expression syntax is described in the section EXPRESSIONS. Next to database URIs the
hostnames and ports for monetdbds that allow to be controlled remotely can be found in the discover list
masked with an asterisk. These entries can easily be filtered out using an expression (e.g. "mapi:mon-
etdb:*") if desired. The control entries come in handy when one wants to get an overview of available
monetdbds in e.g. a local cluster. Note that for monetdbd to announce its control port, the mero_con-
trolport setting for that monetdbd must be enabled in the configuration file.