ubuntu上安装mongodb

安装mongodb

更多信息,请参看 https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/

1、Import the public key used by the package management system

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

2、Create a list file for MongoDB

Ubuntu 12.04

echo "deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Ubuntu 14.04

echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Ubuntu 16.04

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

3、Reload local package database

sudo apt-get update

4、Install the MongoDB packages

Install the latest stable version of MongoDB

sudo apt-get install -y mongodb-org

Install a specific release of MongoDB

sudo apt-get install -y mongodb-org=3.2.11 mongodb-org-server=3.2.11 mongodb-org-shell=3.2.11 mongodb-org-mongos=3.2.11 mongodb-org-tools=3.2.11

Pin a specific version of MongoDB

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

5、(Ubuntu 16.04-only) Create systemd service file

Create a new file at /lib/systemd/system/mongod.service with the following contents:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Run MongoDB Community Edition

1、Start MongoDB

sudo service mongod start

2、Verify that MongoDB has started successfully

Verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading

[initandlisten] waiting for connections on port <port>

where is the port configured in /etc/mongod.conf, 27017 by default.

3、Stop MongoDB

sudo service mongod stop

4、Restart MongoDB

sudo service mongod restart

Uninstall MongoDB Community Edition

1、Stop MongoDB

sudo service mongod stop

2、Remove Packages

sudo apt-get purge mongodb-org*

3、Remove Data Directories

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb