Couchbase Cheat Sheet
Couchbase is a NoSQL database that provides high-performance, scalability, and flexibility. This cheatsheet is a quick reference guide to commonly used commands and operations in Couchbase.
Installation and Setup
Category | Command | Description |
---|
Install | wget https://packages.couchbase.com/releases/7.0.0/couchbase-server-community_7.0.0-ubuntu20.04_amd64.deb | Download Couchbase Server (Example for Ubuntu) |
Install | sudo dpkg -i couchbase-server-community_7.0.0-ubuntu20.04_amd64.deb | Install Couchbase Server |
Access | http://localhost:8091 | Access Couchbase Web Console |
Cluster | couchbase-cli cluster-init | Create a new Cluster |
Cluster | couchbase-cli server-add | Add a Node to the Cluster |
Bucket | couchbase-cli bucket-create | Create a Bucket |
Data Operations
Category | Command | Description |
---|
Insert | cbimport json -c couchbase://localhost -u Administrator -p password -b mybucket -f lines -d file.json | Insert a Document |
Insert | cbdocloader -u Administrator -p password -n localhost:8091 -b mybucket -s 1000 -t 4 -d /opt/couchbase/samples -g key::%2F%2F | Load Documents in Bulk |
Query | cbq -u Administrator -p password -e="SELECT * FROM mybucket WHERE field = 'value'" | Query with N1QL |
Index | cbq -u Administrator -p password -s="CREATE PRIMARY INDEX ON mybucket" | Create a Primary Index |
Index | cbq -u Administrator -p password -s="DROP INDEX mybucket.index_name" | Drop an Index |
Indexing and Query Optimization
Category | Command | Description |
---|
Index | cbq -u Administrator -p password -s="CREATE INDEX idx_name ON mybucket(field)" | Create a Secondary Index |
Index | cbq -u Administrator -p password -e="EXPLAIN SELECT * FROM mybucket WHERE field = 'value'" | Explain Query Execution Plan |
Index | couchbase-cli index-status -c localhost:8091 -u Administrator -p password | View Index Status |
Index | couchbase-cli index-stat -c localhost:8091 -u Administrator -p password | View Index Statistics |
Bucket Operations
Category | Command | Description |
---|
Bucket | couchbase-cli bucket-list -c localhost:8091 -u Administrator -p password | List Buckets |
Bucket | couchbase-cli bucket-flush -c localhost:8091 -u Administrator -p password -b mybucket | Flush a Bucket |
Bucket | couchbase-cli bucket-edit -c localhost:8091 -u Administrator -p password --bucket=mybucket --ramsize=200 | Set Bucket Quota |
Bucket | couchbase-cli bucket-delete -c localhost:8091 -u Administrator -p password --bucket=mybucket | Delete a Bucket |
User Management
Category | Command | Description |
---|
User | couchbase-cli user-manage -c localhost:8091 -u Administrator -p password --set --rbac-username=myuser --rbac-password=mypassword --rbac-name="My User" --roles=admin | Create a User |
User | couchbase-cli user-manage -c localhost:8091 -u Administrator -p password --list | List Users |
User | couchbase-cli user-manage -c localhost:8091 -u Administrator -p password --delete --rbac-username=myuser | Remove a User |
User | couchbase-cli user-manage -c localhost:8091 -u Administrator -p password --set --rbac-username=myuser --roles=admin --auth-domain=external | Create External User |
Security
Category | Command | Description |
---|
Security | couchbase-cli ssl-manage -c localhost:8091 -u Administrator -p password --secure-server=enable --cluster-cert-info=/path/to/cert.pem | Enable SSL |
Security | couchbase-cli setting-rbac -c localhost:8091 -u Administrator -p password --set --rbac-username=admin --rbac-password=admin | Set Up RBAC |
Security | couchbase-cli firewall-rules -c localhost:8091 -u Administrator -p password --allow-ports=8093,8094 | Configure Firewall Rules |
Monitoring and Maintenance
Category | Command | Description |
---|
Monitor | couchbase-cli server-list -c localhost:8091 -u Administrator -p password | View Cluster Status |
Monitor | cbstats localhost:11210 | Monitor Node Performance |
Backup | cbbackupmgr backup -c localhost:8091 --username Administrator --password password --bucket mybucket --repo mybackup | Backup Data |
Restore | cbbackupmgr restore -c localhost:8091 --username Administrator --password password --bucket mybucket --repo mybackup --archive /path/to/backup | Restore Data |
Cluster Management
Category | Command | Description |
---|
Rebalance | couchbase-cli rebalance -c localhost:8091 -u Administrator -p password | Rebalance Cluster |
Failover | couchbase-cli failover -c localhost:8091 -u Administrator -p password --server-failover=server1 | Failover Node |
Re-add | couchbase-cli server-readd -c localhost:8091 -u Administrator -p password --server-add=server1 | Re-add Failed Node |
Decom. | couchbase-cli server-remove -c localhost:8091 -u Administrator -p password --server-remove=server1 | Decommission Node |
SDKs and Integration
…
SDKs and Integration
Category | Command | Description |
---|
Java SDK | compile group: 'com.couchbase.client', name: 'java-client', version: '3.0.10' | Add Couchbase Java SDK Dependency |
Python SDK | pip install couchbase | Install Couchbase Python SDK |
Spring | compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-couchbase', version: '2.6.0' | Add Couchbase Spring Boot Starter Dependency |
Node.js | npm install couchbase | Install Couchbase Node.js SDK |
Troubleshooting
Category | Command | Description |
---|
Logs | cat /opt/couchbase/var/lib/couchbase/logs/error.log | View Couchbase Server Logs |
Health | couchbase-cli server-info -c localhost:8091 -u Administrator -p password | Check Node Health |
Telnet | telnet localhost 8091 | Check Couchbase Server Connectivity |
Stats | cbstats localhost:11210 connections | Display Connection Information |
Best Practices
Category | Command | Description |
---|
Data Mod. | Understand your queries and design documents accordingly. | Data Modeling Best Practice |
Indexing | Create indexes based on frequently queried fields. | Indexing Best Practice |
N1QL Opt. | Use covered indexes to improve query performance. | N1QL Query Optimization Best Practice |
Backup | Regularly schedule backups and test restoration procedures. | Backup and Recovery Best Practice |
Advanced Topics
Category | Command | Description |
---|
FTS | cbft | Couchbase Full-Text Search Command Line Tool |
Eventing | couchbase-cli eventing-consumer -c localhost:8091 -u Administrator -p password --name=myfunction --uuid=myuuid | Eventing Function Status |
GSI | couchbase-cli create-xdcr -c localhost:8091 -u Administrator -p password --xdcr-cluster-name=remote_cluster --xdcr-hostname=remote_host --xdcr-username=remote_user --xdcr-password=remote_password | Set Up XDCR |
Arch. | cbbackupmgr archive -a /path/to/archive.zip | Archive Couchbase Backup |