最近在搭建couchdb集群时,遇到不少坑,如果你参照官方文档搭建集群时会不成功,至少我是这样的。在这里以ubuntu 16.04配置couchdb集群为例,写个总结,以备遗忘。
环境安装
sudo apt-get --no-install-recommends -y install
build-essential pkg-config erlang
libicu-dev libmozjs185-dev libcurl4-openssl-dev
下载 apache-couchdb-2.0.0.tar.gz
下载apache-couchdb-2.0.0.tar.gz并解压,解压后的目录COUCHDB_HOME
编译安装couchDB
cd COUCHDB_HOME
./configure
make release
安装成功后的couchdb发布在COUCHDB_HOME/rel/couchdb.
集群配置
注意:所有的集群节点设置为相同的用户名和密码。
修改配置:COUCHDB_HOME/rel/couchdb/etc/local.ini为:
[chttpd]
bind_address = 0.0.0.0
[admins]
admin = password # 用户名和密码
修改配置:COUCHDB_HOME/rel/couchdb/etc/vm.agrs,假设本节点的ip为192.168.199.236,则修改为:
-name couchdb@192.168.199.236
修改配置:COUCHDB_HOME/rel/couchdb/releases/2.0.0/sys.config为:
- [
- {lager, [
- {error_logger_hwm, 1000},
- {error_logger_redirect, true},
- {handlers, [
- {lager_console_backend, [debug, {
- lager_default_formatter,
- [
- date, " ", time,
- " [", severity, "] ",
- node, " ", pid, " ",
- message,
- " "
- ]
- }]}
- ]},
- {inet_dist_listen_min, 9100},
- {inet_dist_listen_max, 9200}
- ]}
- ].
启动couchdb
- COUCHDB_HOME/rel/couchdb/bin/couchdb
访问http://192.168.199.236:5984/,如返回:
- {
- couchdb: "Welcome",
- version: "2.0.0",
- vendor: {
- name: "The Apache Software Foundation"
- }
- }
则表示启动成功。
集群节点添加
如果添加192.168.199.189,在192.168.199.189上按照上述步骤走一遍,将ip更换为192.168.199.189即可。
在192.168.199.236节点上添加192.168.199.189节点:
- curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"password", "port": 5984, "remote_node": "192.168.199.189", "remote_current_user": "admin", "remote_current_password": "password" }'
- curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "add_node", "host":"192.168.199.189", "port": "5984", "username": "admin", "password":"password"}'
- curl -X POST -H "Content-Type: application/json" http://admin:password@127.0.0.1:5984/_cluster_setup -d '{"action": "finish_cluster"}'
访问http://127.0.0.1:5984/_membership时能看到如下返回的数据:
- {
- all_nodes: [
- "couchdb@192.168.199.189",
- "couchdb@192.168.199.236"
- ],
- cluster_nodes: [
- "couchdb@192.168.199.189",
- "couchdb@192.168.199.236"
- ]
- }
这表示集群搭建成功。或如果在192.168.199.236上的couchdb中添加一个数据库表,如果能在192.168.199.189上的couchdb中看到,表示安装成功。