MongoDB 安裝

每日一句

Sometimes it takes going through something so awful to realize the beauty that is out there in this world.
有時候就是要經(jīng)歷一些糟糕的事情才能意識到世間存在的美麗。

概述

本文介紹MongoDB分別在windows和linux平臺下的安裝與配置和MongoDB服務(wù)啟動與連接。

windows安裝與配置

MongoDB提供了可用于32位和64位系統(tǒng)的預(yù)編譯二進制包,你可以從MongoDB官網(wǎng)下載安裝,MongoDB預(yù)編譯二進制包下載地址:https://www.mongodb.com/try/download/community

注意:在 MongoDB 2.2 版本后已經(jīng)不再支持 Windows XP 系統(tǒng)。最新版本也已經(jīng)沒有了 32 位系統(tǒng)的安裝文件。

你也直接可以直接點擊鏈接下載windows 5.0.5版本→https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-5.0.5-signed.msi

安裝過程中,你可以通過點擊 "Custom(自定義)" 按鈕來設(shè)置你的安裝目錄。

啟動服務(wù)

進入 bin 目錄:D:\devTools\MongoDB\Server\5.0\bin 雙擊mongo.exe 瀏覽器訪問:http://127.0.0.1:27017/

看到頁面出現(xiàn):It looks like you are trying to access MongoDB over HTTP on the native driver port. 就說明啟動成功了。

MongoDB Compass

new Connection:輸入 mongodb://127.0.0.1:27017 就可以連接到本地的MongoDB服務(wù)了。

linux 安裝

MongoDB 提供了 linux 各個發(fā)行版本 64 位的安裝包,你可以在官網(wǎng)下載安裝包。

前置依賴包

安裝前我們需要安裝各個 Linux 平臺依賴包。

Red Hat/CentOS:sudo yum install libcurl openssl

Ubuntu 18.04 LTS ("Bionic")/Debian 10 "Buster":sudo apt-get install libcurl4 openssl

Ubuntu 16.04 LTS ("Xenial")/Debian 9 "Stretch":sudo apt-get install libcurl3 openssl

安裝并啟動

MongoDB 源碼下載地址:https://www.mongodb.com/try/download/community

# 創(chuàng)建 安裝包存放目錄并進入該目錄
mkdir -p /usr/yltrcc/mongodb
cd usr/yltrcc/mongodb/
# 下載并解壓縮
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.5.tgz
tar -xvf mongodb-linux-x86_64-rhel70-5.0.5.tgz 
# 新建幾個目錄,分別用來存儲數(shù)據(jù)和日志
mkdir -p /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db
mkdir -p /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/log
# 新建并修改配置文件
vi /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/mongod.conf

# 向 mongod.conf 添加如下內(nèi)容:
systemLog:
  #MongoDB發(fā)送所有日志輸出的目標指定為文件
  # #The path of the log file to which mongod or mongos should send all diagnostic logging information
  destination: file
  #mongod或mongos應(yīng)向其發(fā)送所有診斷日志記錄信息的日志文件的路徑
  path: "/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/log/mongod.log"
  #當(dāng)mongos或mongod實例重新啟動時,mongos或mongod會將新條目附加到現(xiàn)有日志文件的末尾。
  logAppend: true
storage:
  #mongod實例存儲其數(shù)據(jù)的目錄。storage.dbPath設(shè)置僅適用于mongod。
  ##The directory where the mongod instance stores its data.Default Value is "/data/db".
  dbPath: "/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db"
  journal:
    #啟用或禁用持久性日志以確保數(shù)據(jù)文件保持有效和可恢復(fù)。
    enabled: true
processManagement:
  #啟用在后臺運行mongos或mongod進程的守護進程模式。
  fork: true
net:
  #服務(wù)實例綁定的IP,默認是localhost 192.168.20.131表示遠程訪問
  bindIp: localhost,192.168.20.131
  #綁定的端口,默認是27017
  port: 27017
  
# 啟動 MongoDB 服務(wù)
/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/bin/mongod -f /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/mongod.conf

# 通過進程來查看服務(wù)是否啟動了
ps -ef |grep mongod

提示:看到 child process started successfully, parent exiting表示啟動成功了

連接測試

提示:如果遠程連接不上,需要配置防火墻放行,或直接關(guān)閉linux防火墻

#查看防火墻狀態(tài) 
systemctl status firewalld 
#臨時關(guān)閉防火墻 
systemctl stop firewalld 
#開機禁止啟動防火墻 
systemctl disable firewalld

compass 連接

地址:mongodb://192.168.20.131:27017

停止關(guān)閉服務(wù)

停止服務(wù)的方式有兩種:快速關(guān)閉和標準關(guān)閉

快速關(guān)閉

快速,簡單,數(shù)據(jù)可能會出錯

# 通過 ps 命令查看 進程號
ps -ef | grep mongod
# kill命令結(jié)束該進程
kill -9 54410

【補充】

如果一旦是因為數(shù)據(jù)損壞,則需要進行如下操作(了解):

1)刪除lock文件:rm -f /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db/*.lock

  1. 修復(fù)數(shù)據(jù):/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/bin/mongod --repair --dbpath=/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db

標準關(guān)閉

通過mongo客戶端中的shutdownServer命令來關(guān)閉服務(wù)

# 客戶端登錄服務(wù),注意,這里通過localhost登錄,如果需要遠程登錄,必須先登錄認證才行。 
mongo --port 27017 
# 切換到admin庫
use admin 
# 關(guān)閉服務(wù) 
db.shutdownServer()

美文佳句

同一個人,用不同的眼光去看,瞬間就不一樣。很多時候我們會對身邊的人吹毛求疵,可如果一味盯著別人的缺點,生活自然一地雞毛。只有看到對方的優(yōu)點,才能舒服相處。

朋友間如此,愛人間亦如此。越是優(yōu)秀的人,越容易看到別人的好。

你好,我是yltrcc,日常分享技術(shù)點滴,歡迎關(guān)注我

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容