主頁 > 知識庫 > Linux下MongoDB的安裝和配置教程

Linux下MongoDB的安裝和配置教程

熱門標(biāo)簽:網(wǎng)站建設(shè) 硅谷的囚徒呼叫中心 智能手機 阿里云 美圖手機 百度競價點擊價格的計算公式 檢查注冊表項 使用U盤裝系統(tǒng)

MongoDB安裝

選擇使用Yum安裝

1、制作 repo 文件

cat << EOF > /etc/yum.repos.d/mongodb-org-4.2.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
EOF
12345678

baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.2/x86_64/安裝失敗,嘗試把地址寫死為7,安裝基于centos7的版本。可以成功安裝
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/

2、使用yum 命令安裝

yum install -y mongodb-org

3、啟動mongodb

安裝完啟動服務(wù)則可以使用

啟動、停止、重啟命令如下:

service mongod start
service mongod stop
service mongod restart

4、開放mongodb的遠程連接

mongodb的配置文件是 /etc/mongod.conf

如果要開放遠程訪問需要修改該文件的 bindIp值為: 0.0.0.0 ,否則通過其它電腦是連接不到的

vim /etc/mongod.conf

文件修改后要執(zhí)行 restart 使配置生效

service mongod restart

如果仍不能遠程連接,查看防火墻狀態(tài),如果防火墻開啟,關(guān)閉防火墻或讓防火墻放開 27017 端口(該端口是mongodb的默認端口,可通過配置文件修改mongodb的端口)
查看防火墻狀態(tài)

firewall-cmd --state  

關(guān)閉防火墻狀態(tài)

systemctl stop firewalld.service

防火墻放開 27017 端口

firewall-cmd --permanent --zone=public --add-port=27017/tcp
firewall-cmd --reload

測試是否可以遠程連接

http://服務(wù)器ip:27017/

阿里云服務(wù)器則需要添加端口得安全組

5、創(chuàng)建用戶和密碼

1.進入mongo shell

[root@iZ2ze1wbnx7ym2bkq1xtk5Z conf.d]# mongo
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("73551ca3-8d61-4ce2-a5d1-c0563f9828d4") }
MongoDB server version: 4.2.8
Server has startup warnings:
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten]
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten]
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten]
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-07-01T15:24:12.665+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-07-01T15:24:12.665+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()
---

>

2.切換到admin數(shù)據(jù)庫

admin這個庫是mongodb自動帶的,專門管理用戶和權(quán)限的,創(chuàng)建超級用戶,這個用戶可以管理所有用戶的增刪改以及權(quán)限控制

> use admin
switched to db admin

3.添加賬戶

創(chuàng)建一個超級管理員權(quán)限(擁有userAdminAnyDatabasereadWriteAnyDatabase兩個權(quán)限)的用戶。用戶名和密碼隨便寫,但是角色必須是這兩個

db.createUser( 
{ 
user: "alenghan", pwd: "123456", roles: [
{ 
role: "userAdminAnyDatabase", db: "admin" 
},
"readWriteAnyDatabase" 
] 
}
)

注:``db.createUser()`的具體使用方法:鏈接地址

創(chuàng)建完成就可以使用命令鏈接

mongo --port 27017 -u "alenghan" --authenticationDatabase "admin" -p 123456

4.修改mongo.conf文件

停止mongodb服務(wù)(service mongod stop),修改配置文件(/etc/mongod.conf

# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog: #系統(tǒng)日志
  destination: file #日志輸出目的地
  logAppend: true # 如果為true,當(dāng)mongod/mongos重啟后,將在現(xiàn)有日志的尾部繼續(xù)添加日志。否則,將會備份當(dāng)前日志文件,然后創(chuàng)建一個新的日志文件;默認為false。
  path: /var/log/mongodb/mongod.log #日志路徑

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo # mongod進程存儲數(shù)據(jù)目錄,此配置僅對mongod進程有效
  journal:
    enabled: true #是否開啟journal日志持久存儲,journal日志用來數(shù)據(jù)恢復(fù),是mongod最基礎(chǔ)的特性,通常用于故障恢復(fù)。64位系統(tǒng)默認為true,32位默認為false,建議開啟,僅對mongod進程有效。
#  engine: #存儲引擎類型,mongodb 3.0之后支持“mmapv1”、“wiredTiger”兩種引擎,默認值為“mmapv1”;官方宣稱wiredTiger引擎更加優(yōu)秀。
#  wiredTiger: #對wiredTiger引擎配置生效

# how the process runs
processManagement:
  fork: true  # fork and run in background 運行在后臺
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile PID文件路徑
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017 #端口
  bindIp: 127.0.0.1
  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. 綁定外網(wǎng)op 多個用逗號分隔,如果開放全部外網(wǎng)訪問, 輸入0.0.0.0
  # maxIncomingConnections: 65536  #進程允許的最大連接數(shù) 默認值為65536
  # wireObjectCheck: true #當(dāng)客戶端寫入數(shù)據(jù)時 檢測數(shù)據(jù)的有效性(BSON) 默認值為true
 
#security: #安全有關(guān)的配置
  #authorization: enabled #disabled或者enabled,僅對mongod有效;表示是否開啟用戶訪問控制(Access Control),即客戶端可以通過用戶名和密碼認證的方式訪問系統(tǒng)的數(shù)據(jù),默認為“disabled”,即客戶端不需要密碼即可訪問數(shù)據(jù)庫數(shù)據(jù)。(限定客戶端與mongod、mongos的認證)
  #javascriptEnabled: true #true或者false,默認為true,僅對mongod有效;表示是否關(guān)閉server端的javascript功能,就是是否允許mongod上執(zhí)行javascript腳本,如果為false,那么mapreduce、group命令等將無法使用,因為它們需要在mongod上執(zhí)行javascript腳本方法。如果你的應(yīng)用中沒有mapreduce等操作的需求,為了安全起見,可以關(guān)閉javascript。
 
#operationProfiling: #性能分析器
  #slowOpThresholdMs: 100 #數(shù)據(jù)庫profiler判定一個操作是“慢查詢”的時間閥值,單位毫秒;
  #mode: off #數(shù)據(jù)庫profiler級別,操作的性能信息將會被寫入日志文件中,
  # 可選值:1)off:關(guān)閉profiling
  #       2)slowOp:on,只包含慢操作日志
  #       3)all:on,記錄所有操作
  # 數(shù)據(jù)庫profiling會影響性能,建議只在性能調(diào)試階段開啟。此參數(shù)僅對mongod有效。
 
#replication: #主從復(fù)制 主備模式 這個是大點,需要單獨講
  #oplogSizeMB:10240 #replication操作日志的最大尺寸,單位:MB。

#sharding: #sharding架構(gòu) 集群中使用,暫時沒有接觸

總結(jié)

到此這篇關(guān)于Linux下MongoDB的安裝和配置的文章就介紹到這了,更多相關(guān)Linux下MongoDB的安裝和配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:通遼 煙臺 湖北 賀州 黃山 湘潭 懷化 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux下MongoDB的安裝和配置教程》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266