一. 安裝 mongodb
1. 導(dǎo)入包管理系統(tǒng)使用的公鑰 (Import the public key used by the package management system )
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
2. 為MongoDB創(chuàng)建一個列表文件 (Create a list file for MongoDB.)
/etc/apt/sources.list.d/mongodb-org-3.6.list使用適合您的Ubuntu版本的命令創(chuàng)建列表文件:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
3. 重新加載本地包數(shù)據(jù)庫 (Reload local package database)
發(fā)出以下命令重新加載本地包數(shù)據(jù)庫:
sudo apt-get update
4. 安裝MongoDB包 (Install the MongoDB packages)
安裝最新的穩(wěn)定版本的MongoDB [Install the latest stable version of MongoDB]
sudo apt-get install -y mongodb-org
二. 運(yùn)行mongodb服務(wù)
MongoDB實(shí)例默認(rèn)存儲其數(shù)據(jù)文件/var/lib/mongodb 及其日志文件/var/log/mongodb西设,并使用mongodb 用戶帳戶運(yùn)行瓣铣。您可以在中指定備用日志和數(shù)據(jù)文件目錄/etc/mongod.conf。請參閱systemLog.path
并storage.dbPath
獲取更多信息贷揽。
如果更改運(yùn)行MongoDB進(jìn)程的用戶棠笑,則 必須修改/var/lib/mongodb和 /var/log/mongodb目錄的訪問控制權(quán)限,以使該用戶可以訪問這些目錄禽绪。
1. Start MongoDB.
sudo service mongod start
2. Verify(確認(rèn)) that MongoDB has started successfully
驗(yàn)證mongod
過程已成功啟動通過檢查日志文件的內(nèi)容 /var/log/mongodb/mongod.log
對于這樣一行
[initandlisten] waiting for connections on port 27017
<port>
是mongod
監(jiān)聽的端口蓖救。如果您修改了 配置文件中的net.port
設(shè)置/etc/mongod.conf
,端口可能會有所不同印屁。
如果您修改了systemLog.path
配置文件選項(xiàng)循捺,請?jiān)谀鸀樵撛O(shè)置指定的位置查找日志文件。
您可能會在mongod
輸出中看到非嚴(yán)重警告雄人。只要你看到上面顯示的日志行从橘,你可以放心地忽略這些警告,在你最初的MongoDB評估础钠。
3. Stop(停止) MongoDB.
根據(jù)需要恰力,您可以mongod
通過發(fā)出以下命令來停止進(jìn)程: (As needed, you can stop the mongod
process by issuing the following command:
)
sudo service mongod stop
4. Restart(重啟) MongoDB.
sudo service mongod restart
5. Begin using MongoDB
Start a mongo
shell on the same host machine as the mongod
. Use the --host
command line option to specify the localhost address and port that the mongod
listens on:
mongo --host 127.0.0.1:27017
Later, to stop MongoDB, press Control+C
in the terminal where the mongod
instance is running.
三. Uninstall(卸載) MongoDB Community Edition
To completely(完全的) remove MongoDB from a system, you must remove the MongoDB applications themselves(應(yīng)用程序本身), the configuration files(配置文件), and any directories containing data and logs(包含數(shù)據(jù)和日志的目錄). The following section guides you through the necessary steps.
WARNING
This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible(不可逆), so ensure that all of your configuration and data is backed up before proceeding.
1. Stop MongoDB.
sudo service mongod stop
2. Remove Packages.
Remove any MongoDB packages that you had previously installed.
sudo apt-get purge mongodb-org*
3. Remove Data Directories.
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb