1.下載完安裝包囊骤,并解壓 tgz(以下演示的是 64 位 Linux上的安裝)?
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz? ? # 下載
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 解壓
mv? mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb? ? ? ? ? ? ? ? ? ? ? ? # 將解壓包拷貝到指定目錄
2.MongoDB 的可執(zhí)行文件位于 bin 目錄下晃择,所以可以將其添加到 PATH 路徑中:
命令:
vim /etc/profile
export PATH=<mongodb-install-directory>/bin:$PATH
3.創(chuàng)建數(shù)據(jù)庫目錄
MongoDB的數(shù)據(jù)存儲在data目錄的db目錄下,但是這個目錄在安裝過程不會自動創(chuàng)建淘捡,所以你需要手動創(chuàng)建data目錄藕各,并在data目錄中創(chuàng)建db目錄。
以下實例中我們將data目錄創(chuàng)建于根目錄下(/)焦除。
注意:/data/db 是 MongoDB 默認(rèn)的啟動的數(shù)據(jù)庫路徑(--dbpath)激况。
mkdir -p /data/db
4.創(chuàng)建數(shù)據(jù)庫存放目錄和日志
/data/db 是 mongodb 默認(rèn)的啟動的數(shù)據(jù)庫路徑(--dbpath)。這里我們就放默認(rèn)地兒膘魄,可以自己調(diào)整乌逐,啟動時指向到自己創(chuàng)建的目錄就行
$ cd /data
//創(chuàng)建數(shù)據(jù)庫目錄
$ sudo mkdir db
//創(chuàng)建日志
$ sudo touch logs
5.命令行中啟動MongoDB服務(wù)
$ cd /usr/local/mongo/bin
$ sudo ./mongod --dbpath=/data/db? --logpath=/data/logs
啟動mongodb服務(wù),并指定數(shù)據(jù)庫存放在/data/db路徑下创葡,數(shù)據(jù)庫日志放在/data/logs里浙踢。這種啟動屬于零時啟動,關(guān)掉終端服務(wù)會停掉 這里我們檢測使用灿渴,下面是常駐啟動命令
6.后臺啟動mongodb服務(wù)(常駐 -fork)
÷宀ā(1)使用-fork參數(shù)常駐啟動
$ cd /usr/local/mongo/bin
$ sudo ./mongod --dbpath=/data/db? --fork --logpath=/data/logs
(2)后臺成功提示
2018-08-23T20:55:29.326+0800 I CONTROL? [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 69961
child process started successfully, parent exiting
后臺啟動成功除非手動關(guān)掉mongo服務(wù)或者關(guān)機等意外骚露,否則mongo服務(wù)一直是開啟狀態(tài)
7.mongoDB后臺管理shell
7.1? mongodb shell是mongodb自帶的交互式j(luò)avascript shell,用來對mongodb進行操作和管理的交互式環(huán)境蹬挤。
(1)第一步要先啟動mongodb服務(wù)
(2)執(zhí)行mongo命令
因為我們配置了path,所以啟動和進入shell命令在任何目錄下都可以執(zhí)行棘幸,如果沒有配置path則必須進入/usr/local/mongo/bin目錄下執(zhí)行
$ sudo mongod --dbpath=/data/db? --fork --logpath=/data/logs
$ mongo
7.2? 進入shell
.....
.....
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()
---
>
執(zhí)行簡單的shell命令
7.3? 查看數(shù)據(jù)庫
$ show dbs
admin? 0.000GB
config? 0.000GB
local? 0.000GB
7.4? 查看用戶
$ db.getUsers();
[ ]
8.關(guān)閉mongodb服務(wù)
進入shell執(zhí)行??db.shutdownServer();??命令
db.shutdownServer();
這是最安全的關(guān)閉mongo服務(wù)的操作焰扳,不要kill進程來關(guān)閉