一、简介
Apache CouchDB数据库,它类似于Redis,Cassandra和MongoDB,也是一个NoSQL数据库。 CouchDB将数据存储为非关系性的JSON文档。 这使得CouchDB的用户可以以与现实世界相似的方式来存储数据。
可以从命令行或一个叫作Futon的Web界面来管理CouchDB。 Futon可用于执行管理任务,如创建和操作CouchDB的数据库,文档和用户等。
二、地址
https://couchdb.apache.org/#download
cenos安装: 编辑 /etc/yum.repos.d/bintray-apache-couchdb-rpm.repo
[AppleScript] 纯文本查看 复制代码 [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
然后安装 yum -y install epel-release && sudo yum -y install couchdb
三、访问管理
启动后,可以通过浏览器访问页面 http://localhost:5984/_utils/index.html 来验证安装是否成功,也可以通过此页面管理CouchDB。
或者通过 curl 命令行来验证
[AppleScript] 纯文本查看 复制代码 $ curl http://localhost:5984
{"couchdb":"Welcome","version":"2.3.1","git_sha":"c298091a4","uuid":"04de5436d56ab4a1a4ace6c16555fbe4","features":["pluggable-storage-engines","scheduler"],"vendor":{"name":"The Apache Software Foundation"}}
默认CouchDB只能被通过本机访问,可以通过修改vim /opt/couchdb/etc/default.ini文件中的 bind_address 配置来实现,然后重新启动CouchDB。
[AppleScript] 纯文本查看 复制代码 将
bind_address = 127.0.0.1
改成
bind_address = 0.0.0.0
通过 CouchDB 的 API 来进行常规操作
[AppleScript] 纯文本查看 复制代码 # 创建数据库
$ curl -X PUT http://localhost:5984/mydb
{"ok":true}
# 查看所有数据库
$ curl -X GET http://localhost:5984/_all_dbs
["mydb"]
# 查看数据库信息
$ curl -X GET http://localhost:5984/mydb | json_reformat
{
"db_name": "mydb",
"purge_seq": "0-g1AAAAFTeJzLYWBg4MhgTmEQTM4vTc5ISXIwNDLXMwBCwxygFFMeC5BkeACk_gNBViIDQbUHIGrvE6N2AUTtfmLUNkDUzsevNikBSCbVE3RrkgNIXTxhdQogdfYE1SUyJMlDFGUBAD9sXo4",
"update_seq": "0-g1AAAAFTeJzLYWBg4MhgTmEQTM4vTc5ISXIwNDLXMwBCwxygFFMiQ5L8____sxIZ8ChKUgCSSfaE1TmA1MUTVpcAUldPUF0eC5BkaABSQKXziVG7AKJ2PzFqD0DU3idG7QOIWpB7swBegl6O",
"sizes": {
"file": 33992,
"external": 0,
"active": 0
},
"other": {
"data_size": 0
},
"doc_del_count": 0,
"doc_count": 0,
"disk_size": 33992,
"disk_format_version": 7,
"data_size": 0,
"compact_running": false,
"cluster": {
"q": 8,
"n": 1,
"w": 1,
"r": 1
},
"instance_start_time": "0"
}
创建文档
[AppleScript] 纯文本查看 复制代码 $ curl -X POST http://localhost:5984/mydb -d '{"name":"kongxx", "age": 30, "sex": 1}' -H "Content-Type:application/json"
{"ok":true,"id":"a10691778356d48a39f4ec6784000d2c","rev":"1-1b738f642df6eb80b3eb3e2839bbd10f"}
查看所有文档
[AppleScript] 纯文本查看 复制代码 $ curl -X GET http://localhost:5984/mydb/_all_docs
{"total_rows":1,"offset":0,"rows":[
{"id":"a10691778356d48a39f4ec6784000d2c","key":"a10691778356d48a39f4ec6784000d2c","value":{"rev":"1-1b738f642df6eb80b3eb3e2839bbd10f"}}
更新文档[AppleScript] 纯文本查看 复制代码 # 先查询一下文档[/b]$ curl -X GET http://localhost:5984/mydb/a10691778356d48a39f4ec6784000d2c
{"_id":"a10691778356d48a39f4ec6784000d2c","_rev":"1-1b738f642df6eb80b3eb3e2839bbd10f","name":"kongxx","age":30,"sex":1}
# 更新文档,需要指定 _rev
$ curl -X PUT http://localhost:5984/mydb/a10691778356d48a39f4ec6784000d2c/ -d '{"name":"kongxx", "age": 36, "sex": 1, "_rev": "1-1b738f642df6eb80b3eb3e2839bbd10f"}'
{"ok":true,"id":"a10691778356d48a39f4ec6784000d2c","rev":"2-9b552a207bbdea7e7b6ce6cb184c6f4e"}
# 更新文档后查询
$ curl -X GET http://localhost:5984/mydb/a10691778356d48a39f4ec6784000d2c
{"_id":"a10691778356d48a39f4ec6784000d2c","_rev":"2-9b552a207bbdea7e7b6ce6cb184c6f4e","name":"kongxx","age":36,"sex":1}
删除文档[AppleScript] 纯文本查看 复制代码 # 删除文档
$ curl -X DELETE http://localhost:5984/mydb/a10691778356d48a39f4ec6784000d2c?rev=2-9b552a207bbdea7e7b6ce6cb184c6f4e
{"ok":true,"id":"a10691778356d48a39f4ec6784000d2c","rev":"3-60f90d5d7f1866688cbd55833b2b8c3a"}
# 查询文档
$ curl -X GET http://localhost:5984/mydb/a10691778356d48a39f4ec6784000d2c
{"error":"not_found","reason":"deleted"}
删除数据库[AppleScript] 纯文本查看 复制代码 $ curl -X DELETE http://localhost:5984/mydb[/b]{"ok":true}
|