本文主要介紹了使用 CentOs 6 和 CentOs 7 安裝 MongoDB 3.6
CentOs 7 安裝MongoDB
- MongoDB中文網(wǎng):https://www.mongodb.org.cn/
- 安裝教程:https://www.jb51.net/article/102769.htm
- 下載頁(yè)面:https://www.mongodb.com/download-center/community
- 3.6版下載地址:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.14.tgz
- systemctl參考文檔:https://blog.csdn.net/yuanguozhengjust/article/details/38019923
- centos6參考文檔:https://blog.csdn.net/weixin_37696997/article/details/78545330
- mongodb參數(shù)配置:https://docs.mongodb.com/v3.6/reference/configuration-options/
- MongoDB Compass: https://downloads.mongodb.com/compass/mongodb-compass-1.19.12-win32-x64.exe
-
通過(guò)官網(wǎng)下載軟件包
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.14.tgz
如果感覺(jué)下載太慢或無(wú)法連接汪拥,可以使用windows下載然后上傳到服務(wù)器上,或者使用百度下載3.6:
下載地址:https://pan.baidu.com/s/1q0b-XriEMmfbv6MpC5j7jA
提取碼:3wz1 -
解壓到
/usr/local
文件夾下,并重命名為mongodb
## 解壓 tar -zxf mongodb-linux-x86_64-rhel62-3.6.14.tgz ## 重命名 mv mongodb-linux-x86_64-rhel62-3.6.14/ mongodb3.6
-
創(chuàng)建數(shù)據(jù)庫(kù)目錄和日志文件
cd /mongodb mkdir dbs touch logs
-
在
bin
目錄下配置文件:注意:mongod 默認(rèn)綁定到 localhost(127.0.0.1) ,這里修改為 0.0.0.0 ,允許所有IP訪問(wèn)
cd bin vim mongod.conf ## 添加 bind_ip=0.0.0.0 port=27017 dbpath=/usr/local/mongodb3.6/dbs logpath=/usr/local/mongodb3.6/logs fork=true
-
進(jìn)入
/usr/lib/systemd/system
下添加系統(tǒng)服務(wù)mongodb.servie
[Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/mongodb3.6/bin/mongod --config /usr/local/mongodb3.6/bin/mongod.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/usr/local/mongodb3.6/bin/mongod --shutdown --config /usr/local/mongodb3.6/bin/mongod.conf PrivateTmp=true [Install] WantedBy=multi-user.target
說(shuō)明:
[Unit]部分主要是對(duì)這個(gè)服務(wù)的說(shuō)明,內(nèi)容包括Description和After,Description用于描述服務(wù)挥吵,After用于描述服務(wù)類別
[Service]部分是服務(wù)的關(guān)鍵炫惩,是服務(wù)的一些具體運(yùn)行參數(shù)的設(shè)置洽糟,注意:[Service]部分的啟動(dòng)扁瓢、重啟详恼、停止命令全部要求使用絕對(duì)路徑,使用相對(duì)路徑則會(huì)報(bào)錯(cuò)引几!
- Type=forking 是后臺(tái)運(yùn)行的形式昧互,
- PIDFile 為存放PID的文件路徑,
- ExecStart 為服務(wù)的具體運(yùn)行命令伟桅,
- ExecReload為重啟命令敞掘,
- ExecStop為停止命令,
- PrivateTmp=True表示給服務(wù)分配獨(dú)立的臨時(shí)空間楣铁,
[Install]部分是服務(wù)安裝的相關(guān)設(shè)置玖雁,可設(shè)置為多用戶的
————————————————
版權(quán)聲明:本片段節(jié)選CSDN博主「袁國(guó)正_yy」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議盖腕,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明赫冬。
原文鏈接:https://blog.csdn.net/yuanguozhengjust/article/details/38019923 -
啟動(dòng)、停止赊堪、開(kāi)機(jī)啟動(dòng)相關(guān)命令
# 啟動(dòng)服務(wù) systemctl start mongodb.service # 停止服務(wù) systemctl stop mongodb.service # 設(shè)計(jì)開(kāi)機(jī)啟動(dòng) systemctl enadble mongodb.service
-
連接MongoDB
可以使用MongoDB Compass軟件連接數(shù)據(jù)庫(kù)面殖。如下:
MongoDB Compass Connect.png連接成功后如下:
CentOs 6 安裝MongoDB
前面已經(jīng)介紹了CentOs 7 安裝,接下來(lái)介紹下如何使用CentOs 6 安裝MongoDB哭廉,但是由于CentOs 6中沒(méi)有systemctl脊僚,所以這里需要?jiǎng)?chuàng)建腳本并使用chkconfig啟動(dòng)
第一、第二遵绰、第三步驟同上辽幌,這里就不再介紹
-
這一步也可以使用上面第四步,但這里嘗試使用YAML
注意: YAML不支持使用Tab按鍵進(jìn)行縮進(jìn)椿访,請(qǐng)使用空格
systemLog: destination: file path: /usr/local/mongodb3.6/logs logAppend: false timeStampFormat: ctime net: port: 20017 bindIp: 0.0.0.0 storage: dbPath: /usr/local/mongodb3.6/dbs processManagement: fork: true
-
在
/etc/init.d
中創(chuàng)建服務(wù)mongodb
#!/bin/bash # chkconfig: 2345 85 90 # description: Forever for Node.js, This is a node server file about js obfuscator DEAMON=/usr/local/mongodb3.6/bin/mongod.conf #這里需要填寫config文件 export PATH=$PATH:/usr/local/mongodb3.6/bin #在這里指定一下MongoDB啟動(dòng)路徑 mongod=mongod case "$1" in start) $mongod --config $DEAMON ;; stop) $mongod --shutdown --config $DEAMON ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 ;; esac
-
設(shè)置自啟動(dòng)
# 添加權(quán)限 chmod -R a+x mongodb # 加入到chkconfig chkconfig --add mongodb # 設(shè)置開(kāi)機(jī)啟動(dòng) chkconfig mongodb on # 啟動(dòng) service mongodb start # 關(guān)閉 service mongodb stop # 重啟 service mongodb restart