Apache CouchDB Cheatsheet¶
Apache CouchDB - Dokumentendatenbank
Apache Couch DB ist eine Open-Source-Dokument-orientierte NoSQL-Datenbank, die JSON für Dokumente, HTTP für API und JavaScript für MapReduce Abfragen verwendet. Es bietet ACID-Semantik und eventuelle Konsistenz über Multiversion Koncurrency Control. < p>
Inhaltsverzeichnis¶
- [Installation](LINK_1__
- (LINK_1_)
- [Datenbankverwaltung](LINK_1__
- [Dokumente Operationen](LINK_1__
- Ansichten und Abfragen
- [Mango Queries](LINK_1__
- [Replikation](LINK_1
- [Sicherheit](LINK_1_
- (LINK_1)
- (LINK_1_)
- (Monitoring)(LINK_1_)
- Beste Praktiken
Installation¶
Ubuntu/Debian Installation¶
```bash
Add CouchDB repository¶
curl -L https://couchdb.apache.org/repo/keys.asc | sudo apt-key add - echo "deb https://apache.jfrog.io/artifactory/couchdb-deb/ focal main" | sudo tee /etc/apt/sources.list.d/couchdb.list
Update package index¶
sudo apt update
Install CouchDB¶
sudo apt install couchdb
Configure CouchDB during installation¶
Choose standalone or clustered mode¶
Set admin password¶
Start CouchDB service¶
sudo systemctl start couchdb sudo systemctl enable couchdb
Check status¶
sudo systemctl status couchdb
Verify installation¶
curl http://127.0.0.1:5984/ ```_
CentOS/RHEL Installation¶
```bash
Add EPEL repository¶
sudo yum install epel-release
Add CouchDB repository¶
sudo tee /etc/yum.repos.d/couchdb.repo << EOF [couchdb] name=CouchDB baseurl=https://apache.jfrog.io/artifactory/couchdb-rpm/el7/$basearch/ gpgcheck=1 repo_gpgcheck=1 gpgkey=https://couchdb.apache.org/repo/keys.asc EOF
Install CouchDB¶
sudo yum install couchdb
Start CouchDB service¶
sudo systemctl start couchdb sudo systemctl enable couchdb
Check status¶
curl http://127.0.0.1:5984/ ```_
Docker Installation¶
```bash
Pull CouchDB image¶
docker pull couchdb:latest
Run CouchDB container¶
docker run -d \ --name couchdb \ -p 5984:5984 \ -e COUCHDB_USER=admin \ -e COUCHDB_PASSWORD=password \ -v couchdb-data:/opt/couchdb/data \ couchdb:latest
Check CouchDB¶
curl http://admin:password@localhost:5984/
Docker Compose setup¶
cat > docker-compose.yml << EOF version: '3.8' services: couchdb: image: couchdb:latest container_name: couchdb ports: - "5984:5984" environment: - COUCHDB_USER=admin - COUCHDB_PASSWORD=password volumes: - couchdb-data:/opt/couchdb/data - couchdb-config:/opt/couchdb/etc/local.d restart: unless-stopped
volumes: couchdb-data: couchdb-config: EOF
docker-compose up -d ```_
Manuelle Installation¶
```bash
Download CouchDB¶
wget https://downloads.apache.org/couchdb/source/3.3.3/apache-couchdb-3.3.3.tar.gz
Extract¶
tar -xzf apache-couchdb-3.3.3.tar.gz cd apache-couchdb-3.3.3
Install dependencies (Ubuntu/Debian)¶
sudo apt install build-essential pkg-config erlang \ libicu-dev libmozjs-78-dev libcurl4-openssl-dev
Configure and compile¶
./configure make release
Install¶
sudo cp -r rel/couchdb /opt/ sudo useradd -d /opt/couchdb couchdb sudo chown -R couchdb:couchdb /opt/couchdb
Create systemd service¶
sudo tee /etc/systemd/system/couchdb.service << EOF [Unit] Description=Apache CouchDB After=network.target
[Service] Type=simple User=couchdb ExecStart=/opt/couchdb/bin/couchdb Restart=always
[Install] WantedBy=multi-user.target EOF
sudo systemctl daemon-reload sudo systemctl start couchdb sudo systemctl enable couchdb ```_
Grundgeschäfte¶
HTTP API Basics¶
```bash
Check CouchDB status¶
Get server information¶
curl http://localhost:5984/_up
Get all databases¶
curl http://localhost:5984/_all_dbs
Get server stats¶
curl http://localhost:5984/_stats
Get active tasks¶
curl http://localhost:5984/_active_tasks
Get cluster membership¶
curl http://localhost:5984/_membership
Get node information¶
curl http://localhost:5984/_node/_local/_stats
Authentication¶
curl -u admin:password http://localhost:5984/
Using session authentication¶
curl -X POST http://localhost:5984/_session \ -H "Content-Type: application/json" \ -d '{"name": "admin", "password": "password"}'
Get current user¶
curl http://localhost:5984/_session ```_
Konfiguration¶
```bash
Get configuration¶
curl http://admin:password@localhost:5984/_node/_local/_config
Get specific configuration section¶
curl http://admin:password@localhost:5984/_node/_local/_config/httpd
Set configuration value¶
curl -X PUT http://admin:password@localhost:5984/_node/_local/_config/httpd/bind_address \ -d '"0.0.0.0"'
Delete configuration value¶
curl -X DELETE http://admin:password@localhost:5984/_node/_local/_config/httpd/bind_address
Restart CouchDB¶
curl -X POST http://admin:password@localhost:5984/_node/_local/_restart
Common configuration locations¶
/opt/couchdb/etc/local.ini¶
/opt/couchdb/etc/local.d/¶
Example local.ini configuration¶
cat > /opt/couchdb/etc/local.d/custom.ini << EOF [httpd] bind_address = 0.0.0.0 port = 5984
[ssl] enable = true cert_file = /path/to/cert.pem key_file = /path/to/key.pem
[log] level = info
[couchdb] max_dbs_open = 500 EOF ```_
Datenbankverwaltung¶
Datenbanken erstellen und verwalten¶
```bash
Create database¶
curl -X PUT http://admin:password@localhost:5984/mydb
Check if database exists¶
curl -I http://admin:password@localhost:5984/mydb
Get database information¶
curl http://admin:password@localhost:5984/mydb
Get database stats¶
curl http://admin:password@localhost:5984/mydb/_stats
List all databases¶
curl http://admin:password@localhost:5984/_all_dbs
Delete database¶
curl -X DELETE http://admin:password@localhost:5984/mydb
Compact database¶
curl -X POST http://admin:password@localhost:5984/mydb/_compact
Get database security¶
curl http://admin:password@localhost:5984/mydb/_security
Set database security¶
curl -X PUT http://admin:password@localhost:5984/mydb/_security \ -H "Content-Type: application/json" \ -d '{ "admins": { "names": ["admin"], "roles": ["admin"] }, "members": { "names": ["user1"], "roles": ["user"] } }'
Get database changes¶
curl http://admin:password@localhost:5984/mydb/_changes
Get changes with filter¶
curl http://admin:password@localhost:5984/mydb/_changes?include_docs=true&limit=10
Continuous changes feed¶
curl http://admin:password@localhost:5984/mydb/_changes?feed=continuous ```_
Datenbank Information¶
```bash
Detaillierte Datenbank-Info¶
Curl http://admin:password@localhost:5984/mydb
Beispielantwort:¶
{\cHFFFF} "db_name": "mydb", NACHRICHTEN