Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
04 (Xenial)
Use promo code DOCS10 for $10 credit on a new account. Try this Guide
Contribute on GitHub
View Project (https://github.com/linode/docs) | View File (https://github.com/linode/docs/blob/master/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04.md) | Edit File
(https://github.com/linode/docs/edit/master/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04.md)
In this MongoDB tutorial, we explain how to install the database on Ubuntu 16.04, and then provide a short guide on some basic features and
functions.
MongoDB is a database engine that provides access to non-relational, document-oriented databases. It is part of the growing NoSQL
(https://en.wikipedia.org/wiki/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 (https://www.mongodb.org/community/deployments) and is currently one of the most popular database engines across all systems.
Since MongoDB can require a significant amount of RAM, we recommend using a high memory Linode (https://www.linode.com/pricing#high-
memory) with this guide.
Complete the sections of our Securing Your Server (/docs/security/securing-your-server) to create a standard user account, harden SSH
access and remove unnecessary network services.
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 1/8
25/07/2017 Install MongoDB on Ubuntu 16.04 (Xenial)
Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo . If you’re not familiar with the sudo
command, you can check our Users and Groups (/docs/tools-reference/linux-users-and-groups) guide.
Because the Ubuntu repositories don’t contain a current version, we’ll need to use the MongoDB repository.
3. Update your repositories. This allows apt to read from the newly added MongoDB repo:
Install MongoDB
Now that the MongoDB repository has been added, we’re ready to install the latest stable version of MongoDB:
mongodb-org-server - The standard MongoDB daemon, and relevant init scripts and configurations
mongodb-org-mongos - The MongoDB Shard daemon
mongodb-org-shell - The MongoDB shell, used to interact with MongoDB via the command line
mongodb-org-tools - Contains a few basic tools to restore, import, and export data, as well as a variety of other functions.
These packages provide a good base that will serve most use cases, and we recommend installing them all. However, if you want a more
minimal installation, you can selectively install packages from the above list rather than using the mongodb-org metapackage.
For more information on the installation process and options, refer to the official MongoDB installation tutorial
(https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/).
dbPath indicates where the database files will be stored ( /var/lib/mongodb by default)
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 2/8
25/07/2017 Install MongoDB on Ubuntu 16.04 (Xenial)
These are only a few basic configuration options that are set by default.
We strongly recommend uncommenting the security section and adding the following:
The authorization option enables role-based access control (https://docs.mongodb.com/manual/core/authorization/) for your databases. If no
value is specified, any user will have the ability to modify any database. We’ll explain how to create database users and set their permissions
later in this guide.
For more information on how to customize these and other values in your configuration file, refer to the official MongoDB configuration tutorial
(https://docs.mongodb.com/manual/reference/configuration-options/).
After making changes to the MongoDB configuration file, restart the service as shown in the following section.
1 mongo
2. By default, MongoDB connects to a database called test . Before adding any users, create a database to store user data for
authentication:
1 use admin
3. Use the following command to create an administrative user with the ability to create other users on any database. For better security,
change the values mongo-admin and password :
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 3/8
25/07/2017 Install MongoDB on Ubuntu 16.04 (Xenial)
Keep these credentials in a safe place for future reference. The output will display all the information written to the database except the
password:
1 quit()
5. Test your connection to MongoDB with the credentials created in Step 3, using the admin database for authentication:
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 mongo-admin user created in Step 3 is purely administrative based on the roles specified. It is defined as an administrator of user 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.
6. As the mongo-admin user, create a new database to store regular user data for authentication. The following example calls this database
user-data :
1 use user-data
7. Permissions for different databases are handled in separate roles objects. This example creates the user, example-user , with read-only
permissions for the user-data database and has read and write permissions for the exampleDB 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 and password to something relevant and secure:
db.createUser({user: "example-user", pwd: "password", roles:[{role: "read", db: "user-data"}, {role:"readWrite", db: "exampleDB"}]})
1
To create additional users, repeat Steps 6 and 7 as the administrative user, creating new usernames, passwords and roles by substituing
the appropriate values.
1 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 (https://docs.mongodb.org/v3.2/security).
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 4/8
25/07/2017 Install MongoDB on Ubuntu 16.04 (Xenial)
Much of MongoDB’s popularity comes from its ease of integration. Interactions with databases are done via JavaScript methods, but drivers for
other languages (http://docs.mongodb.org/ecosystem/drivers/) are available. This section will demonstrate a few basic features, but we
encourage you to do further research based on your specific use case.
1 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 Step 7 of the previous section).
To show the name of the current working database, run the db command.
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
(https://docs.mongodb.com/v3.2/reference/method/db.createCollection/).
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 (https://docs.mongodb.com/manual/reference/limits/#naming-
restrictions).
4. 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 and b 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 }}
1
var b = { name : "Jane Doe", attributes: { age : 29, address : "321 Main Rd", favorites : { food : "Spaghetti", animal : "Dog" } }}
2
Note that documents inserted into a collection need not have the same schema, which is one of many benefits of using a NoSQL database.
1 db.exampleCollection.insert(a)
2 db.exampleCollection.insert(b)
The output for each of these operations will show the number of objects successfully written to the current working database:
1 WriteResult({ "nInserted" : 1 })
1 show collections
The output will list all collections containing data within the current working database:
1 exampleCollection
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 5/8
25/07/2017 Install MongoDB on Ubuntu 16.04 (Xenial)
7. View unfiltered data in the exampleCollection collection using the find method. This returns up to the first 20 documents in a collection, if
a query is not passed:
1 db.exampleCollection.find()
You may notice the objects we entered are preceded by _id keys and ObjectId 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:
Running the command above returns a list of documents containing the {"name" : "John Doe"} object.
The mongodb-org-tools package we installed also includes several other tools such as mongodump and mongorestore for creating and restoring
backups and snapshots, as well as mongoimport and mongoexport for importing and exporting content from extended JSON, or supported CSV or
TSV files.
To view the available options or how to use a particular method, append .help() to the end of your commands. For example, to see a list of
options for the find method in Step 6 of Manage Data and Collections:
1 db.exampleCollection.find().help()
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.
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 6/8
25/07/2017 Install MongoDB on Ubuntu 16.04 (Xenial)
Overview (https://www.linode.com/linodes)
Plans & Pricing (https://www.linode.com/pricing)
Features (https://www.linode.com/linodes)
Add-Ons (https://www.linode.com/addons)
Managed (https://www.linode.com/managed)
Resources (https://www.linode.com/docs)
Guides & Tutorials (https://www.linode.com/docs)
Forum (https://forum.linode.com/)
Chat (https://www.linode.com/chat)
Company (https://www.linode.com/about)
About Us (https://www.linode.com/about)
Blog (https://blog.linode.com)
Press (https://www.linode.com/press)
Careers (https://www.linode.com/careers)
Contact Us (https://www.linode.com/contact)
855-4-LINODE (tel:+18554546633)
(855-454-6633) (tel:+18554546633)
Email us (mailto:support@linode.com)
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 7/8
25/07/2017 Install MongoDB on Ubuntu 16.04 (Xenial)
Security (https://www.linode.com/security)
https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04 8/8