Apache CouchDB Cheatsheet
■h1 títuloApache CouchDB - Base de datos del documento "Clase de inscripción" Apache Couch DB es una base de datos NoSQL de código abierto que utiliza JSON para documentos, HTTP para API y JavaScript para MapReducir consultas. Proporciona semántica ACID y eventual consistencia a través del control de concurrencia multi-versión. ▪/p] ■/div titulada
########################################################################################################################################################################################################################################################## Copiar todos los comandos
########################################################################################################################################################################################################################################################## Generar PDF seleccionado/button
■/div titulada ■/div titulada
Cuadro de contenidos
- Instalación
- Basic Operations
- Database Management
- Document Operations
- Vers and Queries
- Mango Queries
- Replicación
- Seguridad
- Clustering
- Performance Tuning
- Monitoreo
- Las mejores prácticas
Instalación
Instalación Ubuntu/Debian
# 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 Instalación
# 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 Instalación
# 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
Instalación manual
# 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
Operaciones básicas
HTTP API Basics
# Check CouchDB status
curl http://localhost:5984/
# 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
Configuración
# 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
Gestión de bases de datos
Creación y gestión de bases de datos
# 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
Información sobre bases de datos
``bash
Obtener información detallada de la base de datos
curl http://admin:password@localhost:5984/mydb___
Respuesta de ejemplo:
{} "db_name": "mydb", HzLT1