如何在Ubuntu 16.04 AWS EC2实例上运行两个不同端口的CouchDB 2.1实例
Hey there! Let's walk through setting up two separate CouchDB 2.1 instances on your Ubuntu 16.04 AWS EC2 instance, each running on a different port. Here's a step-by-step guide that should get you sorted:
First, let's get your system updated and install the base CouchDB 2.1 package:
- Update your package list and upgrade existing packages:
sudo apt-get update && sudo apt-get upgrade -y - Install required dependencies for adding external repositories:
sudo apt-get install -y apt-transport-https ca-certificates - Add CouchDB's official GPG key to verify package integrity:
curl https://apache.bintray.com/couchdb-deb/bintray-pubkey.asc | sudo apt-key add - - Add the CouchDB repository to your system:
echo "deb https://apache.bintray.com/couchdb-deb xenial main" | sudo tee /etc/apt/sources.list.d/couchdb.list - Install CouchDB 2.1.1 (we'll specify the exact version to avoid unexpected updates):
sudo apt-get update && sudo apt-get install -y couchdb=2.1.1
During installation, you'll be prompted to configure the first instance:
- Set the bind address to
0.0.0.0(so it's accessible from your EC2 instance's network) - Keep the default port
5984unless you have a reason to change it - Set an admin username and password (make sure to note these down!)
We need a separate set of directories and config files for the second instance to avoid conflicts:
- Create dedicated directories for data, config, and logs:
sudo mkdir -p /var/lib/couchdb2 /etc/couchdb2 /var/log/couchdb2 - Copy the default CouchDB config files to the new config directory:
sudo cp /etc/couchdb/*.ini /etc/couchdb2/ - Set proper ownership so the
couchdbuser can access these directories:sudo chown -R couchdb:couchdb /var/lib/couchdb2 /etc/couchdb2 /var/log/couchdb2
Now edit the second instance's config file (/etc/couchdb2/local.ini) with these key changes:
- Change the port: Look for the
portfield under the[httpd]section and set it to5985(or any other unused port) - Update data directory: Modify
database_dirunder[couchdb]to/var/lib/couchdb2 - Update log path: Under
[log], changefileto/var/log/couchdb2/couchdb.log - Generate a unique UUID: Run
uuidgenin your terminal to create a new UUID, then replace theuuidfield under[couchdb]with this value (prevents conflicts between instances) - Optional: Set a different admin: If you want separate admin credentials for the second instance, update the
adminfield under[admins]
To manage the second instance easily (start/stop/auto-start on boot), create a systemd service file:
- Create the service file:
sudo nano /etc/systemd/system/couchdb2.service - Paste the following content into the file:
[Unit] Description=CouchDB 2.1 Instance (Port 5985) Documentation=https://couchdb.apache.org/docs/2.1.0/ After=network.target [Service] Type=simple User=couchdb Group=couchdb ExecStart=/usr/bin/couchdb -c /etc/couchdb2/local.ini Restart=always RestartSec=5 [Install] WantedBy=multi-user.target - Save and exit, then reload systemd to recognize the new service:
sudo systemctl daemon-reload - Start the second instance and enable it to launch on boot:
sudo systemctl start couchdb2 && sudo systemctl enable couchdb2
Let's confirm everything works as expected:
- Check the first instance (port 5984):
You should get a JSON response like this:curl http://localhost:5984/{"couchdb":"Welcome","version":"2.1.1","git_sha":"non-git","uuid":"your-first-uuid","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}} - Check the second instance (port 5985):
You'll get a similar response, but with the unique UUID you set earlier.curl http://localhost:5985/
If you need to access these instances from outside your EC2 instance, make sure your AWS security group allows inbound traffic on ports 5984 and 5985. You can then test access using your EC2 public IP:
curl http://<your-ec2-public-ip>:5984/ curl http://<your-ec2-public-ip>:5985/
- Port conflicts: If either instance fails to start, check if the port is in use with
netstat -tulpn | grep 598 - Web interface: Access the Fauxton web UI for each instance at
http://<your-ip>:5984/_utilsandhttp://<your-ip>:5985/_utils(use your admin credentials to log in) - Resource limits: Ensure your EC2 instance has enough CPU and memory to handle both instances—start with a t2.medium if you're unsure
- Backup: Regularly back up both data directories (
/var/lib/couchdband/var/lib/couchdb2) or use CouchDB's built-in backup tools
内容的提问来源于stack exchange,提问作者shikhar bansal

