CouchDB 安装和使用

一、前言

CouchDB是用Erlang开发的面向文档的数据库系统,2010年7月14日发布了1.0版本。CouchDB不是一个传统的关系数据库,而是面向文档的数据库,其数据存储方式有点类似lucene的index文件格式,CouchDB最大的意义在于它是一个面向web应用的新一代存储系统,事实上,CouchDB的口号就是:下一代的Web应用存储系统。

CouchDB 可以安装在大部分 POSIX 系统上,包括 Linux® 和 Mac OS X。Version 2.2.0开始正式支持Windows (x64)。CouchDB 可以从源文件安装,也可以使用包管理器安装(比如在 Mac OS X 上使用 MacPorts)。

CouchDB 是一个顶级 Apache Software Foundation 开源项目,根据 Apache 许可 V2.0 发布。这个开源许可允许在其他软件中使用这些源代码,并根据需要进行修改,但前提是遵从版权需知和免责声明。与许多其他开源许可一样,这个许可允许用户根据需求使用、修改和分发该软件。不一定由同一个许可包含所有修改,因为我们仅维护一个 Apache 代码使用许可需知。

数据库引擎选型-db-engines

Editorial information provided by DB-Engines
NameCouchDB info XMongoDB X
DescriptionA native JSON - document store inspired by Lotus Notes, scalable from globally distributed server-clusters down to mobile phones.One of the most popular document stores available both as a fully managed cloud service and for deployment on self-managed infrastructure
Primary database modelDocument storeDocument store
Secondary database modelsSpatial DBMS infoSpatial DBMS Search engine info Time Series DBMS info
DB-Engines Ranking inforanking trendTrend ChartScore16.80Rank#40 Overall#6 Document storesScore487.35Rank#5 Overall#1 Document stores
Websitecouchdb.apache.orgwww.mongodb.com
Technical documentationdocs.couchdb.org/­en/­stabledocs.mongodb.com/­manual
DeveloperApache Software Foundation infoMongoDB, Inc
Initial release20052009
Current release3.1.1, September 20205.0, July 2021
License infoOpen Source infoOpen Source info
Cloud-based only infonono info
DBaaS offerings (sponsored links) infoMongoDB Atlas: Global multi-cloud database with unmatched data distribution and mobility across AWS, Azure, and Google Cloud, built-in automation for resource and workload optimization, and so much more.ScaleGrid for MongoDB: Fully managed hosting for MongoDB database on AWS, Azure and DigitalOcean with high availability and SSH access on the #1 multi-cloud DBaaS.
Implementation languageErlangC++
Server operating systemsAndroid BSD Linux OS X Solaris WindowsLinux OS X Solaris Windows
Data schemeschema-freeschema-free info
Typing infonoyes info
XML support infono
Secondary indexesyes infoyes
SQL infonoRead-only SQL queries via the MongoDB Connector for BI
APIs and other access methodsRESTful HTTP/JSON APIproprietary protocol using JSON
Supported programming languagesC C# ColdFusion Erlang Haskell Java JavaScript Lisp Lua Objective-C OCaml Perl PHP PL/SQL Python Ruby SmalltalkActionscript info C C# C++ Clojure info ColdFusion info D info Dart info Delphi info Erlang Go Groovy info Haskell Java JavaScript Lisp info Lua info MatLab info Perl PHP PowerShell info Prolog info Python R info Ruby Rust Scala Smalltalk info Swift
Server-side scripts infoView functions in JavaScriptJavaScript
Triggersyesyes info
Partitioning methods infoSharding infoSharding info
Replication methods infoMulti-source replication Source-replica replicationMulti-Source deployments with MongoDB Atlas Global Clusters Source-replica replication
MapReduce infoyesyes
Consistency concepts infoEventual ConsistencyEventual Consistency Immediate Consistency info
Foreign keys infonono info
Transaction concepts infono infoMulti-document ACID Transactions with snapshot isolation
Concurrency infoyes infoyes
Durability infoyesyes info
In-memory capabilities infonoyes info
User concepts infoAccess rights for users can be defined per databaseAccess rights for users and roles

二、软件下载及安装

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://couchdb.apache.org/repo/couchdb.repo
sudo yum install -y couchdb

三、系统配置

1.防火墙设置

Port NumberProtocolRecommended bindingUsage
5984tcpAs desired, by default localhostStandard clustered port for all HTTP API requests
4369tcpAll interfaces by defaultErlang port mapper daemon (epmd)
Random above 1024 (see below)tcpAutomaticCommunication with other CouchDB nodes in the cluster
  • 单机
firewall-cmd --zone=public --add-port=5984/tcp --permanent
firewall-cmd --reload
  • 集群
firewall-cmd --zone=public --add-port=5984/tcp --permanent
firewall-cmd --zone=public --add-port=4369/tcp --permanent
firewall-cmd --zone=public --add-port=9100-9200/tcp --permanent
firewall-cmd --reload

注意:9100-9200范围端口(默认1024以上的随机端口)需要额外配置etc/vm.args

ports-and-firewalls

2.关闭SeLinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
getenforce

四、软件配置

1.修改couchdb配置文件

local.ini文件中行注释使用;开头

cd /opt/couchdb/etc
vim local.ini
  • 开放对外访问

[chttpd]属性组增加如下内容

port = 5984
bind_address = 0.0.0.0
  • 增加admin超级管理员(注意:此处必须配置,否则服务无法正常启动!),CouchDB支持Basic认证、Cookie认证、代理认证、JWT认证。

[admins]属性组增加如下内容,然后重启couchdb服务,再次查看该属性发现已被成功加密

admin = 123456

加密结果

[admins]
admin = -pbkdf2-b09c0c717e94dbde090764cffab07be6c0137449,04549b1e4d812eb9d6c8e2f255765657,10
  • 修改数据目录
mkdir -p /home/couchdb
chown -R couchdb:couchdb /home/couchdb

默认数据目录和view索引目录为/var/lib/couchdb,可通过在[couchdb]属性组增加如下内容

[couchdb]
database_dir = /home/couchdb
view_index_dir = /home/couchdb
  • 日志目录

默认配置在/opt/couchdb/etc/default.d/10-filelog.ini(默认值/var/log/couchdb/couchdb.log),可在local.ini配置文件中覆盖该配置。

cat /var/log/couchdb/couchdb.log

2.修改erlang启动配置文件(集群模式)

  • 集群端口范围设置
cd /opt/couchdb/etc
vim vm.args

追加如下内容

-kernel inet_dist_listen_min 9100
-kernel inet_dist_listen_max 9200
  • 集群节点名称设置,注意节点名称在集群中必须唯一,因此不可使用couchdb@127.0.0.1,可使用具体IP或者域名。
-name couchdb@node1

五、启动和关闭服务

1.启动

sudo systemctl start couchdb

2.状态

sudo systemctl status couchdb

3.关闭

sudo systemctl stop couchdb

4.开机启动

sudo systemctl enable couchdb

六、API使用

CouchDB API通过以_开头,api 文档

curl http://localhost:5984/

用户界面

1.数据库列表

curl http://192.168.63.17:5984/_all_dbs

2.UUID

curl http://192.168.63.17:5984/_uuids?count=5

API接口太多就不一一列举了 😺

参考:

官方文档

Logo

更多推荐