
Apache CouchDB is an open-source NoSQL document-oriented database that supports multiple formats and protocols. It uses JSON to store data with an HTTP API endpoint for interacting with the databases. In addition, CouchDB also includes the Fauxton web interface to interactively manage databases on the server with either a single-node or multi-node cluster setup.
This article explains how to install CouchDB on a Ubuntu 22.04 server with a single-node cluster. You will also use the Fauxton Web interface and HTTP API to perform basic database tasks such as document creation, query data, and use views.
Before you begin:
Install required Ubuntu dependency packages.
Download and add the Apache CouchDB repository GPG key to your server keyrings.
Add the CouchDB repository to your server sources.
Update the server to synchronize all repository packages.
Install CouchDB on the server.
Reply to the installation prompts with the following information.
standalone127.0.0.1admin user profile.Press Enter to save your CouchDB configurations and apply them on your server.
Send an HTTP request to the CouchDB port 5984 to verify that the installation is successful.
Your output should look like the one below.
Set Up Nginx as a reverse proxy to securely access the CouchDB Fauxton web interface using a domain on your server. Follow the steps below to configure Nginx as a reverse proxy to securely access CouchDB.
Create a new Nginx virtual host configuration to use with CouchDB. For example, couchdb.conf.
Add the following contents to the file. Replace couchdb.example.com with your actual domain.
Save and close the file.
The above Nginx configuration creates a new virtual host profile that forwards requests from your domain HTTP port 80 to the backend CouchDB port 5984 on your server.
Link the configuration to the /sites-enabled directory to activate the virtual host profile.
Test the Nginx configuration for errors.
Restart Nginx to apply the configuration changes.
Uncomplicated Firewall (UFW) is active on Ubuntu servers by default, configure the application to allow connection requests to the HTTP port 80. In addition, secure the CouchDB web interface with trusted SSL certificates to allow HTTPS connections on the server port 443 as described in the steps below.
View the firewall status and verify that it's active.
Allow connections to the HTTP port 80.
Allow the HTTPS port 443.
Restart UFW to save the firewall table changes.
Install the Certbot Let's Encrypt client to generate SSL certificates.
Generate a new SSL certificate for your domain couchdb.example.com. Replace hello@example.com with your actual email.
Test the Certbot auto-renewal process to verify that the certificate is automatically renewed upon expiry.
Access your CouchDB domain _utils/ path using a web browser such as Chrome.
Verify that the CouchDB login page displays in your browser. Log in to the server using the following information:
adminYour admin user password you created during installationClick Create Database on the top right navigation bar within your Fauxton CouchDB Interface.
Enter a new database name in the Database name field and click Create to apply and open the new database on your server.
Click Create Document within the database interface to open the JSON editor.
Add a comma at the end of the "_id" value and apply a new document value. For example, "test":"hello world".
Click Create Document to apply the new document.
You can query CouchDB documents after creation using functions such as Mango Query. A Mango query consists of two parts, the index and the selector. An index specifies the fields to perform a query while the selector defines the actual query parameters. Follow the steps below to create additional sample documents with information about cars and run example queries to retrieve data.
Click Create Document within your database interface to open the JSON editor again.
Replace all default values and paste the following document information about cars.
Click Create Document to apply the new resource on your server.
Repeat the process and create 3 additional documents with the following JSON data.
Click Run A Query with Mango on the left database navigation menu to create a new Mango index and perform a new document query.
Click manage indexes below the JSON editor to create a new Mango query.
Replace the Index JSON editor contents with the following values.
Click Create Index to create a new Mango index to improve the query performance.
Click edit query to modify the Mango query.
Replace all JSON editor contents with the following data query data that selects all documents with the field manufacturer set to the value Tesla, Inc..
Click Run Query to perform the new Mango query.
Verify that a new JSON data section is available with the query result that contains 2 documents with the manufacturer set to Tesla, Inc..
Click manage indexes again and replace the JSON editor contents with the following values.
Click Create index to save the new Mango index.
Click edit query and replace the JSON editor contents with the following values to run a query that selects all documents that have year field values between 2015 and 2025.
The above query selects all cars manufactured between the years 2015 and 2025. The variable $gte means "greater than or equal to" while $lte means "less than or equal to”.
Click Run query to start the query and verify that the response includes three cars with 2015 and 2025 range values.
Click Databases on the main navigation bar to open your CouchDB databases.
Select your example database that includes your documents about cars.
Click the + symbol next to the Design Documents and select New View from the list of options.
Enter your new document name and index name in the respective fields. For example, example-doc and example-test-view, then add the following code in the Map function field.
The above function checks if the document contains the manufacturer and range fields. Then, it outputs the manufacturer as the key and the vehicle's range as the value.
Click the Reduce drop-down and select CUSTOM from the list of options to open a new function field.
Add the following function to the Custom Reduce function field to calculate and return the average range for each car manufacturer.
Click Create Document and then Build Index to apply the new view.
Click Table on the top navigation menu to modify the document view with a table format.
Verify that the view returns all cars with the key set to the respective name and the value to their range because the view is executed without the reduce function enabled.
Click Options on the top right bar, select the Reduce option and click Run Query to modify the view results.
Verify that the view displays all car manufacturers and average range values calculated with the reduce function.
You can access CouchDB using the HTTP API to send database requests to the backend server. Follow the steps below to use the Curl utility with the -u option to prompt you for the administrator password for every API request to the CouchDB localhost port 5984.
Get a list of all databases.
Output:
Create a new database such as test_database.
Output:
Delete a database. For example, test_database.
Retrieve database information.
Create a new document in a database. Replace the -d option value with your JSON document data.
Get a document's JSON data stored. Replace test_database,76ca238ae15974746eeafe5c0b0302c1 with your database and document ID values respectively.
Delete a document. Replace 76ca238ae15974746eeafe5c0b0302c1 with your document ID and 1-b9a5713ff0a5b6eee4347d4040a7f6bf with your actual revision number.
Get all documents in a database such as test_database.
Compact a database.
Get active tasks.
You have deployed Apache CouchDB on a Ubuntu 22.04 server and managed database resources. You accessed the Fauxton web interface and used the CouchDB HTTP API endpoints to perform database operations such as document creation, and data queries, and used views with map and reduce functions. For more information, visit the CouchDB documentation.
0 Comments
Be the first to comment and share your perspective with the community.