Guides - Deploy a MongoDB Cluster through the Linode Marketplace
Quickly deploy a Compute Instance with many various software applications pre-installed and ready to use.
MongoDB is a database engine that provides access to non-relational, document-oriented databases. It is part of the growing NoSQL movement, along with databases like Redis and Cassandra (although there are vast differences among the many non-relational databases).
MongoDB seeks to provide an alternative to traditional relational database management systems (RDBMS). In addition to its schema-free design and scalable architecture, MongoDB provides a JSON output and specialized, language-specific bindings that make it particularly attractive for use in custom application development and rapid prototyping. MongoDB has been used in a number of large scale production deployments and is currently one of the most popular database engines across all systems.
Deploying a Marketplace App
The Linode Marketplace allows you to easily deploy an application cluster on Compute Instances using the Cloud Manager. See Get Started with Marketplace Apps for complete steps.
Log in to the Cloud Manager and select the Marketplace link from the left navigation menu. This displays the Linode Create page with the Marketplace tab pre-selected.
Under the Select App section, select the cluster app you would like to deploy. Marketplace Apps that are deployed as clusters have a cluster label next to the app’s name.
Complete the form by following the steps and advice within the Creating a Compute Instance guide. Depending on the Marketplace App you selected, there may be additional configuration options available. See the Configuration Options section below for compatible distributions, recommended plans, and any additional configuration options available for this Marketplace App.
Click the Create Linode button. Once the first Compute Instance has been provisioned and has fully powered on, wait for the software installation to complete. If the instance is powered off or restarted before this time, the other Compute Instances may never be deployed and the software installation will likely fail.
To verify that the app has been fully installed, see Get Started with Marketplace Apps > Verify Installation. Once installed, follow the instructions within the Getting Started After Deployment section to access the application and start using it.
Configuration Options
- Supported distributions: Ubuntu 22.04 LTS
- Recommended minimum plan: All plan types and sizes can be used, though consider using a High Memory Compute Instance for larger databases in a production environment.
MongoDB Options
Linode API Token (required): Your API token is used to deploy additional Compute Instances as part of this cluster. At a minimum, this token must have Read/Write access to Linodes. If you do not yet have an API token, see Get an API Access Token to create one.
Limited sudo user (required): A limited user account with sudo access is created as part of this cluster deployment. Enter your preferred username for this limited user. Please note that the password is automatically created. See Obtaining Usernames and Passwords.
Domain (required): The domain name you wish to use, such as example.com. This domain name is only used as part of the system’s hostname and when creating the TLS/SSL certificate. No domain records are created within Linode’s DNS Manager.
Add SSH Keys to all nodes? If you select yes, any SSH Keys that are added to the root user account (in the SSH Keys section), are also added to your limited user account on all deployed Compute Instances.
MongoDB cluster size: This field cannot be edited, but is used to inform you of the number of Compute Instances that are created as part of this cluster.
TLS/SSL Certificate Options
The following fields (in addition to the domain field above) are used when creating your self-signed TLS/SSL certificate.
- Country or region (required): Enter the country or region for you or your organization.
- State or province (required): Enter the state or province for you or your organization.
- Locality (required): Enter the town or other locality for you or your organization.
- Organization (required): Enter the name of your organization.
- Email address (required): Enter the email address you wish to use for your certificate file. This email address may receive notifications about the state of your certificate, including when it is expired.
- CA Common name: This is the common name for the self-signed Certificate Authority.
- Common name: This is the common name that is used for the domain.
"
) within any of the App-specific configuration fields, including user and database password fields. This special character may cause issues during deployment.Getting Started after Deployment
Obtaining Usernames and Passwords
After your cluster has been fully provisioned, use the instructs below to obtain and save passwords that were generated on your behalf during deployment.
Log in to your new Compute Instance through Lish or SSH using the
root
user and the associated password you entered when creating the instance. If you opted to include your SSH keys as part of this deployment, you can also log in using those keys as either theroot
user or the limited user account you specified during deployment.The passwords have been saved in a
.deployment-secrets.txt
file located in your user’s home directory. You can view this file in your preferred text editor or through thecat
command. In the command below, replace [username] with the limited sudo user you created during deployment.cat /home/[username]/.deployment-secrets.txt
The file contains your MongoDB admin username and password, the MongoDB root username and password, and your system’s limited username and password.
- File: /home/[user]/.deployment-secrets.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# BEGIN ANSIBLE MANAGED BLOCK # mongodb users user: mongo-admin password: 6nYPwXAtgM+xwAJCFlqjrr78/zuDF0+wd6xCCUwNpVk= user: mongo-root password: ltDpEM2AL1nS1j7L5JocZ7zj+7dXDSgTFDybdgaiGyU= user: example-user password: W?lXvW1dZ!o^?..Ea<[9Y9eN3)2MCIr* # SSL ca password mHGsxB@z{r(aCKmoJ<2U/cEg` # END ANSIBLE MANAGED BLOCK
Access the MongoDB Shell
After MongoDB has finished deploying, you can access and administer it directly from the console.
Launch the mongo shell by running the following command. When prompted, enter the admin user password you set when creating this instance.
mongo -u admin -p --authenticationDatabase admin
The
-u
,-p
, and--authenticationDatabase
options in the above command are required in order to authenticate connections to the shell. Without authentication, the MongoDB shell can be accessed but will not allow connections to databases.The
admin
user is purely administrative based on the roles specified. It is defined as an administrator of users for all databases, but does not have any database permissions itself. You may use it to create additional users and define their roles. If you are using multiple applications with MongoDB, set up different users with custom permissions for their corresponding databases.
Create a User Table
As the
admin
user, create a new database to store regular user data for authentication. The following example calls this databaseuser-data
:use user-data
Permissions for different databases are handled in separate
roles
objects. This example creates the user,example-user
, with read-only permissions for theuser-data
database and has read and write permissions for theexampleDB
database that we’ll create in the Manage Data and Collections section below.Create a new, non-administrative user to enter test data. Change both
example-user
andpassword
to something relevant and secure:db.createUser({user: "example-user", pwd: "password", roles:[{role: "read", db: "user-data"}, {role:"readWrite", db: "exampleDB"}]})
To create additional users, repeat this step as the administrative user, creating new usernames, passwords and roles by substituting the appropriate values.
Exit the mongo shell:
quit()
For more information on access control and user management, as well as other tips on securing your databases, refer to the MongoDB Security Documentation.
Manage Data and Collections
Much of MongoDB’s popularity comes from its ease of integration. Interactions with databases are done via JavaScript methods, but drivers for other languages are available. This section will demonstrate a few basic features, but we encourage you to do further research based on your specific use case.
Open the MongoDB shell using the
example-user
we created above:mongo -u example-user -p --authenticationDatabase user-data
Create a new database. This example calls it
exampleDB
:use exampleDB
Make sure that this database name corresponds with the one for which the user has read and write permissions (we added these permissions in the previous section).
To show the name of the current working database, run the
db
command.Create a new collection called
exampleCollection
:db.createCollection("exampleCollection", {capped: false})
If you’re not familiar with MongoDB terminology, you can think of a collection as analogous to a table in a relational database management system. For more information on creating new collections, see the MongoDB documentation on the db.createCollection() method.
Note Collection names should not include certain punctuation such as hyphens. However, exceptions may not be raised until you attempt to use or modify the collection. For more information, refer to MongoDB’s naming restrictions.Create sample data for entry into the test database. MongoDB accepts input as documents in the form of JSON objects such as those below. The
a
andb
variables are used to simplify entry; objects can be inserted directly via functions as well.var a = { name : "John Doe", attributes: { age : 30, address : "123 Main St", phone : 8675309 }} var b = { name : "Jane Doe", attributes: { age : 29, address : "321 Main Rd", favorites : { food : "Spaghetti", animal : "Dog" } }}
Note that documents inserted into a collection need not have the same schema, which is one of many benefits of using a NoSQL database.
Insert the data into
exampleCollection
, using theinsert
method:db.exampleCollection.insert(a) db.exampleCollection.insert(b)
The output for each of these operations will show the number of objects successfully written to the current working database:
WriteResult({ "nInserted" : 1 })
Confirm that the
exampleCollection
collection was properly created:show collections
The output will list all collections containing data within the current working database:
exampleCollection
View unfiltered data in the
exampleCollection
collection using thefind
method. This returns up to the first 20 documents in a collection, if a query is not passed:db.exampleCollection.find()
The output will resemble the following:
{ "_id" : ObjectId("5e68d4618bd4ea23cc3f5e96"), "name" : "John Doe", "attributes" : { "age" : 30, "address" : "123 Main St", "phone" : 8675309 } } { "_id" : ObjectId("5e68d4628bd4ea23cc3f5e97"), "name" : "Jane Doe", "attributes" : { "age" : 29, "address" : "321 Main Rd", "favorites" : { "food" : "Spaghetti", "animal" : "Dog" } } }
You may notice the objects we entered are preceded by
_id
keys andObjectId
values. These are unique indexes generated by MongoDB when an_id
value is not explicitly defined.ObjectId
values can be used as primary keys when entering queries, although for ease of use, you may wish to create your own index as you would with any other database system.The
find
method can also be used to search for a specific document or field by entering a search term parameter (in the form of an object) rather than leaving it empty. For example:db.exampleCollection.find({"name" : "John Doe"})
Running the command above returns a list of documents containing the
{"name" : "John Doe"}
object:{ "_id" : ObjectId("5e68d4618bd4ea23cc3f5e96"), "name" : "John Doe", "attributes" : { "age" : 30, "address" : "123 Main St", "phone" : 8675309 } }
Next Steps
For more on MongoDB, checkout the following guides:
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on