[root@fabric-cli ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
安装rpm仓库
vi /etc/yum.repos.d/bintray-apache-couchdb-rpm.repo
[bintray--apache-couchdb-rpm]
name=bintray--apache-couchdb-rpm
baseurl=http://apache.bintray.com/couchdb-rpm/el$releasever/$basearch/
gpgcheck=0
repo_gpgcheck=0
enabled=1
安装CouchDB
sudo yum update -y && yum -y install epel-release && yum install couchdb
默认安装路径
/opt/couchdb
如果你是base系统,就要安装以下依赖,不是就略过
sudo yum install autoconf autoconf-archive automake \
curl-devel erlang-asn1 erlang-erts erlang-eunit gcc-c++ \
erlang-os_mon erlang-xmerl erlang-erl_interface help2man \
js-devel-1.8.5 libicu-devel libtool perl-Test-Harness
sudo yum install erlang-reltool
依赖,非base系统略过
Erlang OTP (>=R16B03, =<19.x)
ICU
OpenSSL
Mozilla SpiderMonkey (1.8.5)
GNU Make
GNU Compiler Collection
libcurl
help2man
Python (>=2.7) for docs
Python Sphinx (>=1.1.3)
修改配置文件,配置文件相关参数说明请参考Configuration files
[root@fabric-cli etc]# pwd
/opt/couchdb/etc
[root@fabric-cli etc]# ls -l
总用量 36
drwxr-xr-x 2 couchdb couchdb 42 5月 17 15:02 default.d
-rwxr-xr-x 1 couchdb couchdb 21567 5月 17 16:01 default.ini
drwxr-xr-x 2 couchdb couchdb 20 5月 17 15:02 local.d
-rwxr-xr-x 1 couchdb couchdb 4655 5月 17 15:52 local.ini
-rwxr-xr-x 1 couchdb couchdb 1793 11月 8 2017 vm.args
vi default.ini
...
port = 5984
bind_address = 0.0.0.0 //把127.0.0.1修改为0.0.0.0
backlog = 512
docroot = ./share/www
...
非后台启动
sudo -i -u couchdb /opt/couchdb/bin/couchdb
启动成功,访问 localhost:5984 或者nodeIp:5984
访问fauxton(http://10.4.93.141:5984/_utils/),详细请参考fauxton
可以通过fauxton对couchdb进行CRUD,具体使用教程请参考fauxton
还可以通过命令行调用couchdb API进行操作couchdb
后台启动
下载以及安装过程可以参考runit,也可以通过以下命令安装,也可以参考学习笔记
curl -s https://packagecloud.io/install/repositories/imeyer/runit/script.rpm.sh | sudo bash
yum install runit-2.1.1-7.el7.centos.x86_64
//启动
runsvdir -P /etc/service
//查看是否运行runit
[root@fabric-cli couchdb]# ps -ef | grep runsvdir
[root@fabric-cli sv]# ps -ef | grep runsvdir
root 1569 1512 0 17:21 pts/1 00:00:00 runsvdir -P /etc/service
root 1609 1495 0 17:22 pts/0 00:00:00 grep --color=auto runsvdir
//创建日志文件夹
mkdir -p /var/log/couchdb
sudo chown couchdb:couchdb /var/log/couchdb
//创建runit工作目录
mkdir -p /etc/sv/couchdb
mkdir -p /etc/sv/couchdb/logs
vi /etc/sv/couchdb/log/run
#!/bin/sh
exec svlogd -tt /var/log/couchdb
vi /etc/sv/couchdb/run
#!/bin/sh
export HOME=/opt/couchdb
exec 2>&1
exec chpst -u couchdb /opt/couchdb/bin/couchdb
sudo chmod u+x /etc/sv/couchdb/log/run
sudo chmod u+x /etc/sv/couchdb/run
sudo ln -s /etc/sv/couchdb/ /etc/service/couchdb
[root@fabric-cli sv]# sv status couchdb
run: couchdb: (pid 1571) 141s
sudo sv stop couchdb
sudo sv start couchdb
[root@fabric-cli logs]# cd /var/log/couchdb
[root@fabric-cli couchdb]# ls -l
总用量 31296
-rw-rw-r-- 1 couchdb couchdb 27942313 5月 23 17:26 couchdb.log
-rwxr--r-- 1 root root 0 5月 23 16:01 current
-rw------- 1 root root 0 5月 23 16:01 lock
[root@fabric-cli couchdb]# pwd
/var/log/couchdb
[root@fabric-cli etc]# sv start couchdb
ok: run: couchdb: (pid 1624) 0s
[root@fabric-cli ~]# curl -X PUT http://localhost:5984/qinzhao_database -u "admin:admin"
{"ok":true}
[root@fabric-cli ~]# curl -X GET http://localhost:5984/_all_dbs
["qinzhao_database","qinzhaodemo","todos","verifytestdb","verifytestdb_replicate"]
创建文档
[root@fabric-cli ~]# curl -X POST -d '{"todo":"task 1", "done":false}' http://localhost:5984/qinzhao_database -H "Content-Type:application/json"
{"ok":true,"id":"4aa6e557448d76ce68097089b4000833","rev":"1-2fc1d70532433c39c9f61480607e3681"}
用指定的ID创建
创建一个ID为random_task的文档:
[root@fabric-cli ~]# curl -X POST -d '{"_id":"random_task", "todo":"task 2", "done":false}' http://localhost:5984/qinzhao_database -H "Content-Type:application/json"
{"ok":true,"id":"random_task","rev":"1-bceeae3c4a9154c87db1649473316e44"}
创建多个文档
curl -X POST -d '{"docs": [{"todo":"task 3", "done":false}, {"todo":"task 4", "done":false}]}' http://localhost:5984/qinzhao_database/_bulk_docs -H "Content-Type:application/json"
[root@fabric-cli ~]# curl -X POST -d '{"docs": [{"todo":"task 3", "done":false}, {"todo":"task 4", "done":false}]}' http://localhost:5984/qinzhao_database/_bulk_docs -H "Content-Type:application/json"
[{"ok":true,"id":"4aa6e557448d76ce68097089b4001ac1","rev":"1-778fd61f8f460d0c1df1bb174279489d"},{"ok":true,"id":"4aa6e557448d76ce68097089b4002343","rev":"1-dc9e84861bba58e5cfefeed8f5133636"}]
读取文件
[root@fabric-cli ~]# curl -X GET http://localhost:5984/qinzhao_database/random_task
{"_id":"random_task","_rev":"1-bceeae3c4a9154c87db1649473316e44","todo":"task 2","done":false}
[root@fabric-cli ~]# curl -X GET http://localhost:5984/qinzhao_database/4aa6e557448d76ce68097089b4002343
{"_id":"4aa6e557448d76ce68097089b4002343","_rev":"1-dc9e84861bba58e5cfefeed8f5133636","todo":"task 4","done":false}
更新文档
[root@fabric-cli ~]# curl -X GET http://localhost:5984/qinzhao_database/random_task
{"_id":"random_task","_rev":"1-bceeae3c4a9154c87db1649473316e44","todo":"task 2","done":false}
[root@fabric-cli ~]# curl -X PUT -d '{"_rev":"1-bceeae3c4a9154c87db1649473316e44", "todo":"task 2", "done":true}' http://localhost:5984/qinzhao_database/random_task
{"ok":true,"id":"random_task","rev":"2-4cc3dfb6e76befd665faf124b36b7f1c"}
[root@fabric-cli ~]# curl -X GET http://localhost:5984/qinzhao_database/random_task
{"_id":"random_task","_rev":"2-4cc3dfb6e76befd665faf124b36b7f1c","todo":"task 2","done":true}
删除文档
使用HTTP DELETE请求要指定包含这个新的rev值来删除这个文档,如下所示:
[root@fabric-cli ~]# curl -X POST -d '{"_id":"qinzhaoID", "todo":"task 2", "done":false}' http://localhost:5984/qinzhao_database -H "Content-Type:application/json"
{"ok":true,"id":"qinzhaoID","rev":"1-bceeae3c4a9154c87db1649473316e44"}
[root@fabric-cli ~]# curl -X GET http://localhost:5984/qinzhao_database/qinzhaoID
{"_id":"qinzhaoID","_rev":"1-bceeae3c4a9154c87db1649473316e44","todo":"task 2","done":false}
[root@fabric-cli ~]# curl -X DELETE http://localhost:5984/qinzhao_database/qinzhaoID?rev=1-bceeae3c4a9154c87db1649473316e44
{"ok":true,"id":"qinzhaoID","rev":"2-333ecb649463a4938ffbb416916be2f8"}
[root@fabric-cli ~]# curl -X GET http://localhost:5984/qinzhao_database/qinzhaoID
{"error":"not_found","reason":"deleted"}
查看集群设置_cluster_setup
[root@fabric-cli ~]# curl -X GET http://localhost:5984/_cluster_setup -u "admin:admin"
{"state":"cluster_enabled"}
查看集群成员
[root@fabric-cli ~]# curl -X GET http://localhost:5984/_membership -u "admin:admin"
{"all_nodes":["couchdb@127.0.0.1"],"cluster_nodes":["couchdb@127.0.0.1"]}
获取调度job
[root@fabric-cli ~]# curl -X GET http://localhost:5984/_scheduler/jobs -u "admin:admin"
{"total_rows":0,"offset":0,"jobs":[]}
更多请参考CouchDB API
参考:
Installation
CouchDB快速入门
CouchDB API