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

CategoryCommandDescription
Installwget https://packages.couchbase.com/releases/7.0.0/couchbase-server-community_7.0.0-ubuntu20.04_amd64.debDownload Couchbase Server (Example for Ubuntu)
Installsudo dpkg -i couchbase-server-community_7.0.0-ubuntu20.04_amd64.debInstall Couchbase Server
Accesshttp://localhost:8091Access Couchbase Web Console
Clustercouchbase-cli cluster-initCreate a new Cluster
Clustercouchbase-cli server-addAdd a Node to the Cluster
Bucketcouchbase-cli bucket-createCreate a Bucket

Data Operations

CategoryCommandDescription
Insertcbimport json -c couchbase://localhost -u Administrator -p password -b mybucket -f lines -d file.jsonInsert a Document
Insertcbdocloader -u Administrator -p password -n localhost:8091 -b mybucket -s 1000 -t 4 -d /opt/couchbase/samples -g key::%2F%2FLoad Documents in Bulk
Querycbq -u Administrator -p password -e="SELECT * FROM mybucket WHERE field = 'value'"Query with N1QL
Indexcbq -u Administrator -p password -s="CREATE PRIMARY INDEX ON mybucket"Create a Primary Index
Indexcbq -u Administrator -p password -s="DROP INDEX mybucket.index_name"Drop an Index

Indexing and Query Optimization

CategoryCommandDescription
Indexcbq -u Administrator -p password -s="CREATE INDEX idx_name ON mybucket(field)"Create a Secondary Index
Indexcbq -u Administrator -p password -e="EXPLAIN SELECT * FROM mybucket WHERE field = 'value'"Explain Query Execution Plan
Indexcouchbase-cli index-status -c localhost:8091 -u Administrator -p passwordView Index Status
Indexcouchbase-cli index-stat -c localhost:8091 -u Administrator -p passwordView Index Statistics

Bucket Operations

CategoryCommandDescription
Bucketcouchbase-cli bucket-list -c localhost:8091 -u Administrator -p passwordList Buckets
Bucketcouchbase-cli bucket-flush -c localhost:8091 -u Administrator -p password -b mybucketFlush a Bucket
Bucketcouchbase-cli bucket-edit -c localhost:8091 -u Administrator -p password --bucket=mybucket --ramsize=200Set Bucket Quota
Bucketcouchbase-cli bucket-delete -c localhost:8091 -u Administrator -p password --bucket=mybucketDelete a Bucket

User Management

CategoryCommandDescription
Usercouchbase-cli user-manage -c localhost:8091 -u Administrator -p password --set --rbac-username=myuser --rbac-password=mypassword --rbac-name="My User" --roles=adminCreate a User
Usercouchbase-cli user-manage -c localhost:8091 -u Administrator -p password --listList Users
Usercouchbase-cli user-manage -c localhost:8091 -u Administrator -p password --delete --rbac-username=myuserRemove a User
Usercouchbase-cli user-manage -c localhost:8091 -u Administrator -p password --set --rbac-username=myuser --roles=admin --auth-domain=externalCreate External User

Security

CategoryCommandDescription
Securitycouchbase-cli ssl-manage -c localhost:8091 -u Administrator -p password --secure-server=enable --cluster-cert-info=/path/to/cert.pemEnable SSL
Securitycouchbase-cli setting-rbac -c localhost:8091 -u Administrator -p password --set --rbac-username=admin --rbac-password=adminSet Up RBAC
Securitycouchbase-cli firewall-rules -c localhost:8091 -u Administrator -p password --allow-ports=8093,8094Configure Firewall Rules

Monitoring and Maintenance

CategoryCommandDescription
Monitorcouchbase-cli server-list -c localhost:8091 -u Administrator -p passwordView Cluster Status
Monitorcbstats localhost:11210Monitor Node Performance
Backupcbbackupmgr backup -c localhost:8091 --username Administrator --password password --bucket mybucket --repo mybackupBackup Data
Restorecbbackupmgr restore -c localhost:8091 --username Administrator --password password --bucket mybucket --repo mybackup --archive /path/to/backupRestore Data

Cluster Management

CategoryCommandDescription
Rebalancecouchbase-cli rebalance -c localhost:8091 -u Administrator -p passwordRebalance Cluster
Failovercouchbase-cli failover -c localhost:8091 -u Administrator -p password --server-failover=server1Failover Node
Re-addcouchbase-cli server-readd -c localhost:8091 -u Administrator -p password --server-add=server1Re-add Failed Node
Decom.couchbase-cli server-remove -c localhost:8091 -u Administrator -p password --server-remove=server1Decommission Node

SDKs and Integration

SDKs and Integration

CategoryCommandDescription
Java SDKcompile group: 'com.couchbase.client', name: 'java-client', version: '3.0.10'Add Couchbase Java SDK Dependency
Python SDKpip install couchbaseInstall Couchbase Python SDK
Springcompile group: 'org.springframework.boot', name: 'spring-boot-starter-data-couchbase', version: '2.6.0'Add Couchbase Spring Boot Starter Dependency
Node.jsnpm install couchbaseInstall Couchbase Node.js SDK

Troubleshooting

CategoryCommandDescription
Logscat /opt/couchbase/var/lib/couchbase/logs/error.logView Couchbase Server Logs
Healthcouchbase-cli server-info -c localhost:8091 -u Administrator -p passwordCheck Node Health
Telnettelnet localhost 8091Check Couchbase Server Connectivity
Statscbstats localhost:11210 connectionsDisplay Connection Information

Best Practices

CategoryCommandDescription
Data Mod.Understand your queries and design documents accordingly.Data Modeling Best Practice
IndexingCreate indexes based on frequently queried fields.Indexing Best Practice
N1QL Opt.Use covered indexes to improve query performance.N1QL Query Optimization Best Practice
BackupRegularly schedule backups and test restoration procedures.Backup and Recovery Best Practice

Advanced Topics

CategoryCommandDescription
FTScbftCouchbase Full-Text Search Command Line Tool
Eventingcouchbase-cli eventing-consumer -c localhost:8091 -u Administrator -p password --name=myfunction --uuid=myuuidEventing Function Status
GSIcouchbase-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_passwordSet Up XDCR
Arch.cbbackupmgr archive -a /path/to/archive.zipArchive Couchbase Backup