环境:
ocean@ubuntu:~$ lsb_release -dc
Description: Ubuntu 18.10
Codename: cosmic
安装
1、导入mongodb public gpg密钥:
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
Executing: /tmp/apt-key-gpghome.UW1KoNPvQr/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
gpg: key 68818C72E52529D4: public key "MongoDB 4.0 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg: imported: 1
2、为MongoDB创建一个列表文件
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse
3、下载数据库
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org
- 这是通过包管理器安装,在安装过程中会创建数据目录/var/lib/mongodb和日志目录/var/log/mongodb。
- 默认情况下,mongodb使用mongodb用户帐户运行。如果更改运行MongoDB进程的用户,还必须修改对数据和日志目录的权限,以便此用户可以访问这些目录。
- 官方的mongodb包包含一个配置文件(/etc/mongod.conf)。这些设置(如数据目录和日志目录规范)在启动时生效。也就是说,如果在MongoDB实例运行时更改配置文件,则必须重新启动该实例以使更改生效。
$ sudo cat /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
运行
1、重启、启动、停止、查看mongoDB的状态
$ sudo service mongod start
$ sudo service mongod status
$ sudo service mongod stop
$ sudo service mongod restart
2、启动mongoDB的shell
$ mongo
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("db4c1099-8f86-4b65-9814-8027c63d368e") }
MongoDB server version: 4.0.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-03-17T15:46:20.063+0800 I STORAGE [initandlisten]
2019-03-17T15:46:20.063+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-03-17T15:46:20.063+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-03-17T15:46:21.055+0800 I CONTROL [initandlisten]
2019-03-17T15:46:21.055+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-03-17T15:46:21.055+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-03-17T15:46:21.055+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
完全卸载
$ sudo service mongod stop
$ sudo apt-get purge mongodb-org*
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb
参考:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/#uninstall-mongodb-community-edition