Ubuntu下安装配置与使用couchdb

date: 2020.02.08; modification:2020.02.09

目录:

1 数据库操作

2 创建数据库

curl -X PUT http://127.0.0.1:5984/数据库名

3 创建用户

curl -X PUT http://localhost:5984/_users/org.couchdb.user:jane \
     -H "Accept: application/json" \
     -H "Content-Type: application/json" \
     -d '{"name": "jane", "password": "apple", "roles": [], "type": "user"}'

4 配置

4.1 启用https

创建证书:

sudo mkdir /etc/couchdb/ssl
cd /etc/couchdb/ssl
sudo su  # 需要su 到 root, 否则下面的命令用sudo会报无权限
openssl genrsa > privkey.pem
openssl req -new -x509 -key privkey.pem -out couchdb.pem -days 1095
chmod 600 privkey.pem couchdb.pem
chown couchdb privkey.pem couchdb.pem

配置https:

sudo vim /etc/couchdb/local.ini

对于1.6.0版本, 在[daemons]段和[ssl]段加入如下内容:

[daemons]
httpsd = {couch_httpd, start_link, [https]}
[ssl]
enable = true
cert_file = /etc/couchdb/ssl/couchdb.pem
key_file = /etc/couchdb/ssl/privkey.pem

其中httpsd字段作用为使能https.

验证配置结果:

sudo service couchdb restart
sudo netstat -lnp | grep 984     # 此命令应该可以看到couch https默认的端口6984在监听

curl -k https://127.0.0.1:6984/
如果需要账号密码:
curl -u 账号:密码 -k https://127.0.0.1:6984/
或
curl  -k https://账号:密码@127.0.0.1:6984/

得到类似信息:
{"couchdb":"Welcome","uuid":"28fee2b350f118ccaa235e1cdcf51e86","version":"1.6.0","vendor":{"name":"Ubuntu","version":"15.10"}}
说明配置成功.

另外, 如果写web程序浏览器访问, 并且自建证书的话, 则需要和web站点的https用的ssl证书为同一份, 否则浏览器会报证书错误.