1. Install MongoDB (3.4)
1.1 Import the public key used by the package management system
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
1.2 Create a list file for MongoDB
$ echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
1.3 Reload local package database
$ sudo apt-get update
1.4 Install the MongoDB packages
$ sudo apt-get install -y mongodb-org
1.5 The MongoDB instance stores its data files in /var/lib/mongodb
and its log files in /var/log/mongodb
by default, and runs using the mongodb user account. You can specify alternate log and data file directories in /etc/mongod.conf
2. Start / Stop / Restart MongoDB
$ sudo service mongod start
$ sudo service mongod stop
$ sudo service mongod restart
3. Completely remove MongoDB
$ sudo service mongod stop
$ sudo apt-get purge mongodb-org*
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb
4. 安装后遇到的问题
- 1 mongod启动失败
- 查看日志信息(这个路径是默认的日志路径,可以在配置文件中修改)
$ tail -f /var/log/mongodb/mongod.log
- 如果log中提示
Failed to unlink socket file /tmp/mongodb-27017.sock
$ rm /tmp/mongodb-27017.sock
4.2 无法从其他机器的客户端连接mongo服务
Always remember to edit the /etc/mongod.conf file and set your bind_ip = 0.0.0.0 in order to make connections externally.