1. Official website:
Synopsis
This tutorial outlines the basic installation process for installing MongoDB on Ubuntu Linux systems. This tutorial uses .deb packages as the basis of the installation. 10gen publishes packages of the MongoDB releases as .deb packages for easy installation and management for users of Ubuntu systems. Ubuntu does include MongoDB packages, the 10gen packages are generally more up to date.
This tutorial includes: an overview of the available packages, instructions for configuring the package manager, the process for installing packages from the 10gen repository, and preliminary MongoDB configuration and operation.
annotation
If you use an older Ubuntu that does not use Upstart, (i.e. any version before 9.10 “Karmic”) please follow the instructions on the 在Debian上安装 tutorial.
Also refer to
The documentation of following related processes and concepts.
Other installation tutorials:
Package Options
The 10gen repository contains three packages:
-
mongodb-10gen
This package contains the latest stable release. Use this for production deployments.
-
mongodb20-10gen
This package contains the stable release of v2.0 branch.
-
mongodb18-10gen
This package contains the stable release of v1.8 branch.
You cannot install these packages concurrently with each other or with the mongodb package that your release of Ubuntu may include.
10gen also provides packages for “unstable” or development versions of MongoDB. Use the mongodb-10gen-unstable package to test the latest development release of MongoDB, but do not use this version in production.
Install MongoDB
Configuring the Package Management System (APT)
The Ubuntu package management tool (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the 10gen public GPG Key:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Create a /etc/apt/sources.list.d/10gen.list file and include the following line for the 10gen repository.
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
Now issue the following command to reload your repository:
sudo apt-get update
Installation package
Issue the following command to install the latest stable version of MongoDB:
sudo apt-get install mongodb-10gen
When this command completes, you have successfully installed MongoDB! Continue for configuration and start-up suggestions.
Configure MongoDB
These packages configure MongoDB using the /etc/mongodb.conf file in conjunction with the control script. You will find the control script is at /etc/init.d/mongodb.
This MongoDB instance will store its data files in the /var/lib/mongodb and its log files in /var/log/mongodb, and run using the mongodb user account.
annotation
If you change the user that runs the MongoDB process, you will need to modify the access control rights to the /var/lib/mongodb and /var/log/mongodb directories.
Control MongoDB
Start MongoDB
You can start the mongod process by issuing the following command:
sudo service mongodb start
You can verify that mongod has started successfully by checking the contents of the log file at /var/log/mongodb/mongodb.log.
Stop MongoDB¶
As needed, you may stop the mongod process by issuing the following command:
sudo service mongodb stop
Restart MongoDB
You may restart the mongod process by issuing the following command:
sudo service mongodb restart
control mongos
As of the current release, there are no control scripts for mongos. mongos is only used in sharding deployments and typically do not run on the same systems where mongod runs. You can use the mongodb script referenced above to derive your own mongos control script.
Using MongoDB
Among the tools included with the MongoDB package, is the mongo shell. You can connect to your MongoDB instance by issuing the following command at the system prompt:
mongo
This will connect to the database running on the localhost interface by default. At the mongo prompt, issue the following two commands to insert a record in the “test” collection of the (default) “test” database.
> db.test.save( { a: 1 } ) > db.test.find()
Also refer to
“mongo” and “mongo Shell JavaScript 快参“
(From: https://mongodb-documentation.readthedocs.org/en/latest/tutorial/install-mongodb-on-ubuntu.html )
2. Other references:
Ubuntu
Install the 10Gen GPG key
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Edit source.list file to add 10gen source
$ sudo vi /etc/apt/sources.list
Add to:
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
Install MongoDB
sudo apt-get update sudo apt-get install mongodb-10gen
Add permissions to MongoDB's running directory
$ sudo chown mongodb /var/lib/mongodb
This way MongoDB is installed.
Starting and shutting down mongodb works.
$ sudo service mongodb stop $ sudo service mongodb start
Configuration information is stored in /etc/mongodb.conf
If you want to change the port, you can modify
port=27017
By default, mongodb can accept any connection, no username and password are required.
We have two options for the production environment
1. Create a username and password
$ mongo > use admin switched to db admin > db.addUser("username","password")
After the user name is established, you need to log in with the user name when you need to open the connection
Add in /etc/mongodb.conf
auth=true
2. Only accept local connections
In /etc/mongodb.conf add:
bind_ip=127.0.0.1
This way only connections from local can connect to MongoDB
(From: https://ruby-china.org/topics/454 )
After the installation is complete, the MongoDB server will start automatically, we check the MongoDB server program
# Check the MongoDB server system process ~ ps -aux|grep mongo mongodb 6870 3.7 0.4 349208 39740 ? Ssl 10:27 2:23 /usr/bin/mongod --config /etc/mongodb.conf # Check MongoDB server status by starting command ~ netstat -nlt|grep 27017 tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN # Check MongoDB server status by starting command ~ sudo /etc/init.d/mongodb status Rather than invoking init scripts through /etc/init.d, use the service(8) utility, e.g. service mongodb status Since the script you are attempting to invoke has been converted to an Upstart job, you may also use the status(8) utility, e.g. status mongodb mongodb start/running, process 6870 # Check MongoDB server status via system services ~ sudo service mongodb status mongodb start/running, process 6870
View the status of the MongoDB server through the web console. Enter http://ip:27017 in the browser to open the console through the web.
http://localhost:27017
When you see the following, it means that the service has been started:
It looks like you are trying to access MongoDB over HTTP on the native driver port.
3. Some installation issues:
couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
Reinstallation can be tested:
sudo apt-get install --reinstall mongodb
Original/From: Install mongoDB on Ubuntu