您好,登錄后才能下訂單哦!
MongoDB的安裝啟動(dòng)和服務(wù)化以及連接是怎樣的,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
下載:https://www.mongodb.com/download-center/community/releases ,注意下載對(duì)應(yīng)的版本
解壓:將下載的文件解壓到一個(gè)文件夾
創(chuàng)建數(shù)據(jù)文件夾 mongo-data, 創(chuàng)建log文件夾:mongo-log
本文中的目錄是這樣的
/opt/mongo/ bin/ # mongo 程序 data/ # 數(shù)據(jù) log/ # 日志 mongod.conf # 日志
所有命令相對(duì)路徑都是相對(duì) /opt/mongo
創(chuàng)建用戶(hù)組和用戶(hù)
groupadd mongod useradd -g mongod mongod
第一個(gè)條命令是添加用戶(hù)組,第二條是向 mongod 用戶(hù)組添加用戶(hù) mongod
組mongo 用戶(hù) 對(duì) mongo相關(guān)文件的訪問(wèn)權(quán)限
chown -R mongo:mongo <mongo相關(guān)文件夾>
啟動(dòng)
./bin/mongod --logpath=log/mongo.log --dbpath=data --port=9999 --fork
上面的命令是用登出用戶(hù)啟動(dòng)的,mongo.log 這個(gè)文件要先創(chuàng)建好,否則會(huì)報(bào)錯(cuò)
--fork 代表后臺(tái)運(yùn)行
測(cè)試是否啟動(dòng)成功
./bin/mongo --port=9999
使用mongo客戶(hù)端連接一下,上面這個(gè)命令是連接localhosts的9999端口上的MONGOD
連接成功后你會(huì)看到很多warn
Access control is not enabled for the database. Read and write access to data and configuration is unrestricted,
沒(méi)有使用訪問(wèn)權(quán)限限制,后面解決
You are running this process as the root user, which is not recommended
不推薦使用root 啟動(dòng)服務(wù),后面解決
This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
說(shuō)現(xiàn)在服務(wù)使用 localhost啟動(dòng),本機(jī)以外的客戶(hù)端不能訪問(wèn),并告訴你應(yīng)該怎么處理,后面在配置文件中解決
Soft rlimits too low :
這個(gè)問(wèn)題的官方文檔:https://docs.mongodb.com/manual/reference/ulimit/
這個(gè)問(wèn)題的大概意思是linux系統(tǒng)對(duì)用戶(hù)使用各種資源有數(shù)量限制,當(dāng)前系統(tǒng)的一個(gè)限制會(huì)影響mongo的運(yùn)行,使用ulimit -a
查看各種限制
官方指出下面幾種資源會(huì)對(duì)mongo的運(yùn)行有影響,并給出了推薦值
-f (file size): unlimited -t (cpu time): unlimited -v (virtual memory): unlimited [1] -l (locked-in-memory size): unlimited -n (open files): 64000 -m (memory size): unlimited [1] [2] -u (processes/threads): 64000
參照上面的推薦值,和-ulimit -a
的結(jié)果對(duì)比一下(按照前面的 -f,-t來(lái)對(duì)比),哪個(gè)不對(duì),就改哪個(gè),比如 現(xiàn)在系統(tǒng)的 -n
值是 1024 ,比推薦的64000小,那么就執(zhí)行命令 -ulimit -n 64000
, 之后kill服務(wù)再啟動(dòng)
如果你的系統(tǒng)是使用systemd的,那么也可以在 .service 的文件中加入下面的配置
[Service] # Other directives omitted # (file size) LimitFSIZE=infinity # (cpu time) LimitCPU=infinity # (virtual memory size) LimitAS=infinity # (locked-in-memory size) LimitMEMLOCK=infinity # (open files) LimitNOFILE=64000 # (processes/threads) LimitNPROC=64000
配置文件:一個(gè)簡(jiǎn)單的例子
systemLog: destination: file path: "/opt/mongo/mongodb/log/mongo.log" net: port: 9999 bindIp: 192.168.145.220,127.0.0.1 storage: dbPath: "/opt/mongo/mongodb/data" processManagement: fork: true
啟動(dòng)時(shí):bin/mongod -f mongod.conf
systemctl 啟動(dòng)mongodb
[Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/opt/mongo/mongodb/bin/mongod --config /opt/mongo/mongodb/mongod.conf ExecStop=/opt/mongo/mongodb/bin/mongod --shutdown --config /opt/mongo/mongodb/mongod.conf PrivateTmp=true LimitFSIZE=infinity LimitCPU=infinity LimitAS=infinity LimitMEMLOCK=infinity LimitNOFILE=64000 LimitNPROC=64000 Group=mongod User=mongod ExecReload=/bin/kill -s HUP $MAINPID [Install] WantedBy=multi-user.target
對(duì)應(yīng)配置文件應(yīng)該有如下的配置
processManagement: fork: true pidFilePath: "/opt/mongo/mongodb/log/9999.pid"
如果不設(shè)置 pid文件,只設(shè)置 fork為true,不能啟動(dòng),如果兩個(gè)都沒(méi)有,那么啟動(dòng)時(shí)(service mongo start)時(shí)會(huì)卡住,但這時(shí)會(huì) ctrl + c 你會(huì)發(fā)現(xiàn) 服務(wù)已經(jīng)啟動(dòng)了。。只有在這兩個(gè)選項(xiàng)都有的時(shí)候,才能特別正常
另外注意 .service 文件中的Group 與User兩個(gè)選項(xiàng),設(shè)置為最開(kāi)始的 mongo 就會(huì)消除前面的警告
創(chuàng)建用戶(hù)
不創(chuàng)建用戶(hù)你的數(shù)據(jù)庫(kù)就只能在沒(méi)有安全的情況下運(yùn)行創(chuàng)建用戶(hù)以后就可以在登陸 mongo只要都帶著用戶(hù)登陸信息
db.createUser( { user: "superuser", pwd: "123456", roles: [ "root" ] } )
到此為止,你的mongo就可以開(kāi)啟 認(rèn)證登陸了,mongo的權(quán)限是基于角色的,我們現(xiàn)在創(chuàng)建了一個(gè)root角色的 superuser 用戶(hù)
在配置文件中加入下面的配置,開(kāi)啟認(rèn)證登陸
security: authorization: enabled
重啟服務(wù)
登陸時(shí)有兩種方式
bin/mongo --port 9999 --username superuser --password 123456 --authenticationDatabase admin ## --authenticationDatabase 代表你創(chuàng)建用戶(hù)的那個(gè)數(shù)據(jù)庫(kù) bin/mongo mongodb://localhost:9999/admin?authSource=admin --username superuser ## 然后會(huì)讓你輸入 superman 的密碼 ## /admin 代表你要登陸的數(shù)據(jù)庫(kù) ## authSource 與 --authenticationDatabases 是一樣的作用
現(xiàn)在我們擁有一個(gè)root權(quán)限的 superman 用戶(hù)
先在不需要 auth 的情況下登陸
創(chuàng)建后 use admin
,切換到admin 數(shù)據(jù)庫(kù)
創(chuàng)建用戶(hù)
完整的創(chuàng)建用戶(hù)的語(yǔ)句
use reporting db.createUser( { user: "reportsUser", pwd: "2222", roles: [ { role: "read", db: "reporting" }, { role: "read", db: "products" }, { role: "read", db: "sales" }, { role: "readWrite", db: "accounts" } ] } )
對(duì)上面的語(yǔ)句解釋?zhuān)?/p>
所有這個(gè)用戶(hù)在需時(shí)就需要這樣
bin/mongo --port 9999 --username reportsUser --password 2222 --authenticationDatabase reporting
或
bin/mongo mongodb://localhost:9999/admin?reporting=admin --username superuser
使用 reporting 數(shù)據(jù)庫(kù)
創(chuàng)建一個(gè)用戶(hù),用戶(hù)名 reportsUser, 密碼:2222 , 對(duì) reporting,products,sales 三個(gè)數(shù)據(jù)庫(kù)有讀的權(quán)限,對(duì) accounts 數(shù)據(jù)庫(kù)有讀寫(xiě)權(quán)限
刪除/添加用戶(hù)角色
use reporting db.revokeRolesFromUser( "reportsUser", [ { role: "readWrite", db: "accounts" } ] )
use reporting db.grantRolesToUser( "reportsUser", [ { role: "read", db: "accounts" } ] )
更改用戶(hù)密碼
先用有更新用戶(hù)密碼權(quán)限的用戶(hù)登錄
下面
db.changeUserPassword("reporting", "SOh4TbYhxuLiW8ypJPxmt1oOfL")
看完上述內(nèi)容,你們掌握MongoDB的安裝啟動(dòng)和服務(wù)化以及連接是怎樣的的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。