banner



How To Repair Couch Open Couchdb

Introduction

Apache CouchDB, like Redis, Cassandra, and MongoDB, is a NoSQL database. CouchDB stores data as JSON documents which are non-relational in nature. This allows users of CouchDB to shop data in ways that look very similar to their real world counterparts.

You can manage CouchDB from the command line or from a web interface called Futon. Futon can be used to perform authoritative tasks similar creating and manipulating databases, documents, and users for CouchDB.

Goals

By the end of this article, you will:

  • Accept CouchDB installed on a Droplet running Ubuntu 14.04
  • Take Futon installed on the aforementioned server
  • Have secured the CouchDB installation
  • Access CouchDB using Futon from your local machine, using a secure tunnel
  • Know how to add together an admin user to CouchDB
  • Perform CRUD operations with CouchDB using Daybed
  • Perform Crud operations with CouchDB from the control line

Prerequisites

Delight complete the following prerequisites:

  • Ubuntu 14.04 Droplet
  • You are logged in to your server every bit a non-root user with administrative privileges (Initial Server Setup with Ubuntu 14.04)

Note: While this tutorial assumes a non-root user, you tin execute the steps below equally the root user as well, in instance you don't want to create a sudo user. Please notation that if you do employ a non-root user, you lot'll be asked for your password the starting time fourth dimension you execute a command with sudo.

Stride 1 — Preparing the Server

Before nosotros can install CouchDB, nosotros need to ensure that the server is set for it.

Begin past updating the system:

                      
  1. sudo apt-get update

Install the software that allows y'all to manage the source repositories:

                      
  1. sudo apt-go install software-properties-common -y

Note: The -y flag tells the apt-go control to presume a Yes response to all the prompts that might come during the installation process. You can drib this flag if you prefer responding manually to the prompts.

Add the PPA that will assist us fetch the latest CouchDB version from the appropriate repository:

                      
  1. sudo add-apt-repository ppa:couchdb/stable -y

Alarm: Great care should be taken while adding a new Personal Bundle Annal (PPA) to your server. Since anyone tin create a PPA, in that location'south no guarantee that it can be trusted or that information technology is secure. In this example, the above PPA is an official one, maintained by the Apache CouchDB team.

Now that we have added a new PPA, let's update the system so that it has the latest package information:

                      
  1. sudo apt-get update

We are now ready to install CouchDB and Futon.

Step 2 — Installing CouchDB

If yous previously had CouchDB installed on this server, begin past removing the existing version:

                      
  1. sudo apt-get remove couchdb couchdb-bin couchdb-common -yf

Note: If you lot have a fresh Droplet, you can ignore this step.

Now install CouchDB:

                      
  1. sudo apt-get install couchdb -y

This will install CouchDB and Futon on your server.

By default, CouchDB runs on localhost and uses the port 5984. Yous can call up this basic information by running curl from the command line:

                      
  1. gyre localhost:5984

Notation: If yous don't have curlicue installed, you can use the sudo apt-get install curl command to install it.

You lot should get something similar to the following:

                      

Output

{"couchdb":"Welcome","uuid":"b9f278c743b5fc0b971c4e587d77582e","version":"1.6.i","vendor":{"name":"Ubuntu","version":"xiv.04"}}

Y'all can now create a new database with the ringlet -10 PUT command:

                      
  1. curl -10 PUT localhost:5984/new_database

The results should await like this:

                      

Output

{"ok":true}

Step 3 — Securing the CouchDB Installation

By default, some of the files and directories created when CouchDB is installed belong to the root user and group. While this is fine (admitting non appropriate) during evolution, it could be a security risk in product.

When CouchDB is installed, it creates a user and a group named couchdb. In this section we will change the ownership and permission of the CouchDB files to the couchdb user and group.

Irresolute the ownership controls what the CouchDB process can admission, and irresolute the permissions controls who can access the CouchDB files and directories.

Before changing the ownership and permissions, stop CouchDB:

                      
  1. sudo stop couchdb

Change the ownership of the /usr/lib/couchdb, /usr/share/couchdb, and /etc/couchdb directories, and the /usr/bin/couchdb executable file, such that their possessor is couchdb and they belong to the couchdb group.

                      
  1. sudo chown -R couchdb:couchdb /usr/lib/couchdb /usr/share/couchdb /etc/couchdb /usr/bin/couchdb

At present, change the permissions of the /usr/lib/couchdb, /usr/share/couchdb, and /etc/couchdb directories, and the /usr/bin/couchdb executable file, such that the couchdb user and the couchdb group have complete access (to the CouchDB installation) while no other user has access to these files and directories.

                      
  1. sudo chmod -R 0770 /usr/lib/couchdb /usr/share/couchdb /etc/couchdb /usr/bin/couchdb

All that's left to do is restart CouchDB:

                      
  1. sudo start couchdb

CouchDB should now be up and running without any of its files or directories belonging to either the root user or the root group.

Step 4 — Accessing Daybed

CouchDB offers a user-friendly spider web-based control panel chosen Futon. We're going to access information technology from your local workstation, tunneling the traffic through an SSH connectedness to your server. This ways that simply users with an SSH login to your server tin can access the Futon command panel.

To connect securely to CouchDB, without making information technology publicly available, you lot tin create an SSH tunnel from your local port 5984 to the remote server's port 5984.

You lot tin can employ the following control, run from your local reckoner, to gear up up the tunnel:

                      
  1. ssh -L5984:127.0.0.i:5984 sammy@your_server_ip

Annotation: Remember to supersede sammy with your username and your_server_ip with the IP address of your Droplet.

While the connexion is open, you can access Futon from your favorite web browser, using port 5984. Visit this URL to display the helpful Futon folio:

          http://localhost:5984/_utils                  

Futon Home Page Screenshot

Past default, all CouchDB users who access Daybed do so with administrative rights. This is announced in the bottom right corner:

Welcome to Admin Party! Everyone is admin. Fix this

You tin change this by clicking on the little Prepare this link and creating new administrators.

Step 5 — Calculation an Admin User

Now that we have CouchDB upwardly and running, let's outset using it.

Earlier an admin user is created, all users can admission CouchDB with administrative privileges (although they require SSH access to the server kickoff).

Information technology'southward a skillful do to create an admin account for CouchDB, to prevent accidental or unauthorized data loss.

To do this, click the Fix this link that appears in the lesser right corner of Futon. This volition bring up a screen that allows yous to create a CouchDB admin user, every bit follows:

Admin User Creation Screen

Enter the desired username and password:

Enter a username, such as "admin", and a password

After entering your new CouchDB username and a secure countersign, click the Create push button. This will create the new admin user. The message in the lesser right corner of Futon will confirm this past showing a bulletin similar to the following:

Welcome admin! Setup more admins or Change password or Logout

Note: The cosmos of an admin user prevents unauthorized users from deleting and modifying databases, design documents, and the CouchDB configuration. Even so, information technology doesn't prevent them from creating or accessing documents.

Exist careful about handing out SSH access to your server.

That's it! Our CouchDB server is now fully configured.

To larn more than nearly using the database, keep reading.

Performing CRUD Operations from Futon

Futon has a very elementary only useful user interface which allows you to perform basic CRUD operations (create, read, update, and delete).

In this department, we will create a new database named todos, add together a new document to it, and then call up, update, and delete this certificate.

Notation: If yous have created an admin user, yous volition take to exist logged in equally the administrator to create a new database.

Make sure you withal have your SSH tunnel open up. If not, open your connection to the server from your local reckoner with this control:

                      
  1. ssh -L5984:127.0.0.one:5984 sammy@your_server_ip

Let's begin by visiting the Futon page at http://localhost:5984/_utils/.

Note: This section assumes that CouchDB is being accessed using an SSH tunnel that was set up equally described in in the Accessing Futon section in a higher place. If your setup is different, make certain you access Futon at the correct URL.

Create a Database and Document

Futon Homepage

To create a new database chosen todos, click the Create Database link on the screen. This will bring up a dialog every bit follows:

New Database Dialog; enter Database Name

Enter the proper noun of the database and click the Create push.

This will create a new database named todos and take you lot to a folio where y'all can start creating and modifying documents in the newly created database.

"todos" Database Page

Create Document

To create a new certificate, click the New Document link on the page.

This will open up up a screen with a new document. This document volition accept only the _id field. You can change the value of this field if you need to or you can leave it as is.

Click the Add Field link; double-click the null value to update it

Click the Add Field link to add a new field to this document.

As tin exist seen above, we take added two fields named todo and done. By default, new fields take a null value.

Double-click the value to change it.

In this example, we have double-clicked the value fields of todo and done and accept entered the values Task one and simulated respectively.

Once you have entered the values, either press the ENTER key or click the little green check mark next the field to save its contents. (Failing to do this will leave the value of the field every bit null.) This should wait as follows:

Create and Save Field and Values

To salve the document, click the Save Document link. After the certificate is saved, y'all will run into that a _rev field has been added to it as follows:

_rev field

Read a Certificate

Click on the todos link (in the top bar next to the Overview link) to view the newly-created document, equally the only document in the todos database.

Todos Database Page

Click on the key of the document (the ID) in the table to admission the document details folio.

Edit a Document

On this folio, you can edit and update the document fields as follows:

Document Details Page

To edit a field value, double-click it and start editing.

You lot can delete whatever field (apart from the _id and _rev fields), add new fields, or change the values of existing fields. In this instance, we have changed the value of the washed field from faux to true every bit follows:

Change Field Value: "done" field from "false" to "true"

After you are satisfied with the changes, click the Save Document link to update the document. In one case you exercise so, yous'll find that the value of the _rev field has been updated also.

Save the changes

Delete a Certificate

To delete a document, you tin click the Delete Document link which volition prompt y'all for confirmation:

Are you sure you want to delete this document?

Ostend by pressing the Delete push.

Futon will delete the document and take you lot to the todos database folio, which should at present be empty, confirming that the document has indeed been deleted.

Todos Database Page

Performing CRUD Operations from the Command Line

This section volition illustrate how we can perform bones Grime (create, read, update, and delete) operations on a CouchDB database from the command line using curl.

Make sure you withal accept your SSH tunnel open. If not, open your connectedness to the server from your local computer with this control:

                      
  1. ssh -L5984:127.0.0.1:5984 sammy@your_server_ip

Note: This department will utilize the database new_database that was created in Step 2 (Installing CouchDB) higher up. This department will also presume that we are accessing CouchDB using an SSH tunnel as described in the Accessing Futon pace above. If your setup is different, brand certain you replace the URL, PORT, and the database names accordingly while executing the commands used below.

Create a Database

If you didn't already create the database new_database, please do so at present. This control should be executed from your local workstation:

                      
  1. curl -X PUT http://localhost:5984/new_database -u "admin:countersign"
  2. { "ok":true}

Since we added an admin user to CouchDB, we at present have to send the admin username and password when creating a new database.

The results should look like this:

                      

Output

{"ok":true}

Create a Certificate

Permit'southward begin by creating a new certificate.

                      
  1. curlicue -X Post -d '{"todo":"task 1", "done":false}' http://localhost:5984/new_database -H "Content-Type:awarding/json"

This command creates a new document in the new_database database.

The -X flag indicates that we are performing an HTTP Postal service performance. The -H flag followed past the header sets the content type of this asking as application/json since we are POSTing a JSON document. Finally, the JSON document itself is included, along with the -d flag.

The response of this operation is as follows:

                      

Output

{"ok":true,"id":"803da996e1524591ce773d24400004ff","rev":"1-2fc1d70532433c39c9f61480607e3681"}

The "ok":truthful part of this response indicates that the performance was successful. The response includes the fields id and rev, which represent the certificate ID and the document revision respectively. Both these fields will be required in case this document needs to be modified or deleted.

In this example, the document ID was generated past CouchDB considering nosotros didn't supply it with the control. If required, we can create a certificate with a unique ID that we have generated.

Create with Specified ID

Create a document with the ID random_task:

                      
  1. ringlet -10 Mail -d '{"_id":"random_task", "todo":"task ii", "done":simulated}' http://localhost:5984/new_database -H "Content-Type:application/json"

This control creates a new certificate with the ID gear up to random_task. The response to this command is equally follows:

                      

Output

{"ok":truthful,"id":"random_task","rev":"1-bceeae3c4a9154c87db1649473316e44"}

Create Multiple Documents

In addition to creating single documents, we can likewise create documents in bulk.

                      
  1. curl -10 POST -d '{"docs": [{"todo":"task 3", "washed":false}, {"todo":"chore 4", "done":false}]}' http://localhost:5984/new_database/_bulk_docs -H "Content-Type:awarding/json"

This command will create two documents as specified in the Post body. There are two slight differences, equally compared to the unmarried-certificate inserts:

  1. While inserting a single certificate, the Postal service body was merely a standard JSON object. In example of bulk inserts, the POST body comprises an object with a docs field. This field holds the assortment of documents that are to be inserted.
  2. While inserting a single document, the POST request was sent to the URL pointing to the database (http://localhost:5984/new_database). The request for majority inserts, notwithstanding, POSTs to the http://localhost:5984/new_database/_bulk_docs URL.

The response of the bulk insert operation is as follows:

                      

Output

[{"ok":true,"id":"803da996e1524591ce773d24400007df","rev":"1-778fd61f8f460d0c1df1bb174279489d"},{"ok":true,"id":"803da996e1524591ce773d2440001723","rev":"1-dc9e84861bba58e5cfefeed8f5133636"}]

Read a Document

Retrieving a certificate from a CouchDB database is a simple matter of issuing an HTTP Get command. Let's try to retrieve ane of the documents we created above: the 1 called random_task.

                      
  1. curl -X Go http://localhost:5984/new_database/random_task

Note that the URL includes the ID (random_task) of the document which is existence retrieved. The response to this GET request, as shown beneath, contains the entire certificate forth with the _id and the _rev fields, which tin can be used to update or delete this document.

                      

Output

{"_id":"random_task","_rev":"ane-bceeae3c4a9154c87db1649473316e44","todo":"task ii","done":false}

Edit a Certificate

While trying to update a document, information technology is important to include the _rev field. CouchDB volition turn down any update request which doesn't include a _rev field. Since CouchDB updates the entire document, and not just parts of it, the unabridged document must be sent in the request body during an update operation.

To update the document that was created with the ID random_task, we need to issue an HTTP PUT request as follows:

                      
  1. curl -X PUT -d '{"_rev":"1-bceeae3c4a9154c87db1649473316e44", "todo":"task two", "done":true}' http://localhost:5984/new_database/random_task

Be sure to replace the _rev value with the string you received in the previous output.

This modifies the document and updates the done field to true. The response to this asking is every bit follows:

                      

Output

{"ok":true,"id":"random_task","rev":"2-4cc3dfb6e76befd665faf124b36b7f1c"}

As can be seen in the response, the rev field for this item document changes after it has been updated. Any futurity asking to update or delete this document will now take to employ the newest rev value.

Delete a Document

Permit'south use this new rev value to delete this document using an HTTP DELETE request every bit follows:

                      
  1. gyre -X DELETE http://localhost:5984/new_database/random_task?rev= ii-4cc3dfb6e76befd665faf124b36b7f1c

Just similar the GET & PUT requests higher up, the DELETE request uses the URL that points to the document. However, it as well includes an boosted query parameter in the URL. This parameter, rev, should have the latest _rev value for the delete operation to be successful.

In this particular instance, we use the value that was returned after the update functioning in the previous step. The response to the above request is shown below.

                      

Output

{"ok":truthful,"id":"random_task","rev":"3-07d6cde68be2a559497ec263045edc9d"}

Restarting, Stopping, and Starting the CouchDB Service

Starting, stopping and restarting the CouchDB service is quite straightforward. Complete these steps from the server.

Restart

To restart a running CouchDB instance, execute the following command:

                      
  1. sudo restart couchdb

This command will restart a running CouchDB instance and display the process ID of the new instance. In case there is no instance of CouchDB running, executing this command will requite a message like

                      

Output

restart: Unknown instance:

Stop

To stop a running CouchDB instance, execute the post-obit command:

                      
  1. sudo stop couchdb

Executing this command will stop any running CouchDB instance and provide a confirmation bulletin similar this:

                      

Output

couchdb stop/waiting

Kickoff

To get-go CouchDB, execute the following control:

                      
  1. sudo showtime couchdb

If CouchDB wasn't already running, executing this command will start CouchDB and provide a confirmation message similar this:

                      

Output

couchdb start/running, process 12345

On the other hand, if in that location was a CouchDB instance already running so executing the to a higher place command volition upshot in a bulletin like this:

                      

Output

outset: Task is already running: couchdb

Status

In instance you desire to check the status of CouchDB, you can do and so using the post-obit command:

                      
  1. sudo status couchdb

If CouchDB is running, this will give a message similar to the following:

                      

Output

couchdb start/running, process 12345

If CouchDB is not running, checking the condition volition result in something like:

                      

Output

couchdb end/waiting

Conclusion

Y'all now have a fully functional setup of CouchDB on your Droplet, which you can securely administer from your local machine using Futon or the command line.

How To Repair Couch Open Couchdb,

Source: https://www.digitalocean.com/community/tutorials/how-to-install-couchdb-and-futon-on-ubuntu-14-04

Posted by: nelsoncauseveras.blogspot.com

0 Response to "How To Repair Couch Open Couchdb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel