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、啟動(dòng)mongodb
安裝完啟動(dòng)服務(wù)則可以使用
啟動(dòng)、停止、重啟命令如下:
service mongod start
service mongod stop
service mongod restart
4、開放mongodb
的遠(yuǎn)程連接
mongodb
的配置文件是 /etc/mongod.conf
如果要開放遠(yuǎn)程訪問需要修改該文件的 bindIp
值為: 0.0.0.0
,否則通過其它電腦是連接不到的
vim /etc/mongod.conf
文件修改后要執(zhí)行 restart 使配置生效
service mongod restart
如果仍不能遠(yuǎn)程連接,查看防火墻狀態(tài),如果防火墻開啟,關(guān)閉防火墻或讓防火墻放開 27017 端口(該端口是mongodb
的默認(rèn)端口,可通過配置文件修改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
測試是否可以遠(yuǎn)程連接
http://服務(wù)器ip:27017/
阿里云服務(wù)器則需要添加端口得安全組
5、創(chuàng)建用戶和密碼
1.進(jìn)入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
這個(gè)庫是mongodb
自動(dòng)帶的,專門管理用戶和權(quán)限的,創(chuàng)建超級用戶,這個(gè)用戶可以管理所有用戶的增刪改以及權(quán)限控制
> use admin
switched to db admin
3.添加賬戶
創(chuàng)建一個(gè)超級管理員權(quán)限(擁有userAdminAnyDatabase
和readWriteAnyDatabase
兩個(gè)權(quán)限)的用戶。用戶名和密碼隨便寫,但是角色必須是這兩個(gè)
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ù)添加日志。否則,將會(huì)備份當(dāng)前日志文件,然后創(chuàng)建一個(gè)新的日志文件;默認(rèn)為false。
path: /var/log/mongodb/mongod.log #日志路徑
# Where and how to store data.
storage:
dbPath: /var/lib/mongo # mongod進(jìn)程存儲數(shù)據(jù)目錄,此配置僅對mongod進(jìn)程有效
journal:
enabled: true #是否開啟journal日志持久存儲,journal日志用來數(shù)據(jù)恢復(fù),是mongod最基礎(chǔ)的特性,通常用于故障恢復(fù)。64位系統(tǒng)默認(rèn)為true,32位默認(rèn)為false,建議開啟,僅對mongod進(jìn)程有效。
# engine: #存儲引擎類型,mongodb 3.0之后支持“mmapv1”、“wiredTiger”兩種引擎,默認(rèn)值為“mmapv1”;官方宣稱wiredTiger引擎更加優(yōu)秀。
# wiredTiger: #對wiredTiger引擎配置生效
# how the process runs
processManagement:
fork: true # fork and run in background 運(yùn)行在后臺
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 多個(gè)用逗號分隔,如果開放全部外網(wǎng)訪問, 輸入0.0.0.0
# maxIncomingConnections: 65536 #進(jìn)程允許的最大連接數(shù) 默認(rèn)值為65536
# wireObjectCheck: true #當(dāng)客戶端寫入數(shù)據(jù)時(shí) 檢測數(shù)據(jù)的有效性(BSON) 默認(rèn)值為true
#security: #安全有關(guān)的配置
#authorization: enabled #disabled或者enabled,僅對mongod有效;表示是否開啟用戶訪問控制(Access Control),即客戶端可以通過用戶名和密碼認(rèn)證的方式訪問系統(tǒng)的數(shù)據(jù),默認(rèn)為“disabled”,即客戶端不需要密碼即可訪問數(shù)據(jù)庫數(shù)據(jù)。(限定客戶端與mongod、mongos的認(rèn)證)
#javascriptEnabled: true #true或者false,默認(rèn)為true,僅對mongod有效;表示是否關(guān)閉server端的javascript功能,就是是否允許mongod上執(zhí)行javascript腳本,如果為false,那么mapreduce、group命令等將無法使用,因?yàn)樗鼈冃枰趍ongod上執(zhí)行javascript腳本方法。如果你的應(yīng)用中沒有mapreduce等操作的需求,為了安全起見,可以關(guān)閉javascript。
#operationProfiling: #性能分析器
#slowOpThresholdMs: 100 #數(shù)據(jù)庫profiler判定一個(gè)操作是“慢查詢”的時(shí)間閥值,單位毫秒;
#mode: off #數(shù)據(jù)庫profiler級別,操作的性能信息將會(huì)被寫入日志文件中,
# 可選值:1)off:關(guān)閉profiling
# 2)slowOp:on,只包含慢操作日志
# 3)all:on,記錄所有操作
# 數(shù)據(jù)庫profiling會(huì)影響性能,建議只在性能調(diào)試階段開啟。此參數(shù)僅對mongod有效。
#replication: #主從復(fù)制 主備模式 這個(gè)是大點(diǎn),需要單獨(dú)講
#oplogSizeMB:10240 #replication操作日志的最大尺寸,單位:MB。
#sharding: #sharding架構(gòu) 集群中使用,暫時(shí)沒有接觸
總結(jié)
到此這篇關(guān)于Linux下MongoDB的安裝和配置的文章就介紹到這了,更多相關(guān)Linux下MongoDB的安裝和配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!