MongoDB的安裝啟動和服務(wù)化以及連接是怎樣的,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
公司主營業(yè)務(wù):成都網(wǎng)站制作、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出雙峰免費做網(wǎng)站回饋大家。
下載:https://www.mongodb.com/download-center/community/releases ,注意下載對應(yīng)的版本
解壓:將下載的文件解壓到一個文件夾
創(chuàng)建數(shù)據(jù)文件夾 mongo-data, 創(chuàng)建log文件夾:mongo-log
本文中的目錄是這樣的
/opt/mongo/ bin/ # mongo 程序 data/ # 數(shù)據(jù) log/ # 日志 mongod.conf # 日志
所有命令相對路徑都是相對 /opt/mongo
創(chuàng)建用戶組和用戶
groupadd mongod useradd -g mongod mongod
第一個條命令是添加用戶組,第二條是向 mongod 用戶組添加用戶 mongod
組mongo 用戶 對 mongo相關(guān)文件的訪問權(quán)限
chown -R mongo:mongo <mongo相關(guān)文件夾>
啟動
./bin/mongod --logpath=log/mongo.log --dbpath=data --port=9999 --fork
上面的命令是用登出用戶啟動的,mongo.log 這個文件要先創(chuàng)建好,否則會報錯
--fork 代表后臺運行
測試是否啟動成功
./bin/mongo --port=9999
使用mongo客戶端連接一下,上面這個命令是連接localhosts的9999端口上的MONGOD
連接成功后你會看到很多warn
Access control is not enabled for the database. Read and write access to data and configuration is unrestricted,
沒有使用訪問權(quán)限限制,后面解決
You are running this process as the root user, which is not recommended
不推薦使用root 啟動服務(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
說現(xiàn)在服務(wù)使用 localhost啟動,本機以外的客戶端不能訪問,并告訴你應(yīng)該怎么處理,后面在配置文件中解決
Soft rlimits too low :
這個問題的官方文檔:https://docs.mongodb.com/manual/reference/ulimit/
這個問題的大概意思是linux系統(tǒng)對用戶使用各種資源有數(shù)量限制,當前系統(tǒng)的一個限制會影響mongo的運行,使用ulimit -a
查看各種限制
官方指出下面幾種資源會對mongo的運行有影響,并給出了推薦值
-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é)果對比一下(按照前面的 -f,-t來對比),哪個不對,就改哪個,比如 現(xiàn)在系統(tǒng)的 -n
值是 1024 ,比推薦的64000小,那么就執(zhí)行命令 -ulimit -n 64000
, 之后kill服務(wù)再啟動
如果你的系統(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
配置文件:一個簡單的例子
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
啟動時:bin/mongod -f mongod.conf
systemctl 啟動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
對應(yīng)配置文件應(yīng)該有如下的配置
processManagement: fork: true pidFilePath: "/opt/mongo/mongodb/log/9999.pid"
如果不設(shè)置 pid文件,只設(shè)置 fork為true,不能啟動,如果兩個都沒有,那么啟動時(service mongo start)時會卡住,但這時會 ctrl + c 你會發(fā)現(xiàn) 服務(wù)已經(jīng)啟動了。。只有在這兩個選項都有的時候,才能特別正常
另外注意 .service 文件中的Group 與User兩個選項,設(shè)置為最開始的 mongo 就會消除前面的警告
創(chuàng)建用戶
不創(chuàng)建用戶你的數(shù)據(jù)庫就只能在沒有安全的情況下運行創(chuàng)建用戶以后就可以在登陸 mongo只要都帶著用戶登陸信息
db.createUser( { user: "superuser", pwd: "123456", roles: [ "root" ] } )
到此為止,你的mongo就可以開啟 認證登陸了,mongo的權(quán)限是基于角色的,我們現(xiàn)在創(chuàng)建了一個root角色的 superuser 用戶
在配置文件中加入下面的配置,開啟認證登陸
security: authorization: enabled
重啟服務(wù)
登陸時有兩種方式
bin/mongo --port 9999 --username superuser --password 123456 --authenticationDatabase admin ## --authenticationDatabase 代表你創(chuàng)建用戶的那個數(shù)據(jù)庫 bin/mongo mongodb://localhost:9999/admin?authSource=admin --username superuser ## 然后會讓你輸入 superman 的密碼 ## /admin 代表你要登陸的數(shù)據(jù)庫 ## authSource 與 --authenticationDatabases 是一樣的作用
現(xiàn)在我們擁有一個root權(quán)限的 superman 用戶
先在不需要 auth 的情況下登陸
創(chuàng)建后 use admin
,切換到admin 數(shù)據(jù)庫
創(chuàng)建用戶
完整的創(chuàng)建用戶的語句
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" } ] } )
對上面的語句解釋:
所有這個用戶在需時就需要這樣
bin/mongo --port 9999 --username reportsUser --password 2222 --authenticationDatabase reporting
或
bin/mongo mongodb://localhost:9999/admin?reporting=admin --username superuser
使用 reporting 數(shù)據(jù)庫
創(chuàng)建一個用戶,用戶名 reportsUser, 密碼:2222 , 對 reporting,products,sales 三個數(shù)據(jù)庫有讀的權(quán)限,對 accounts 數(shù)據(jù)庫有讀寫權(quán)限
刪除/添加用戶角色
use reporting db.revokeRolesFromUser( "reportsUser", [ { role: "readWrite", db: "accounts" } ] )
use reporting db.grantRolesToUser( "reportsUser", [ { role: "read", db: "accounts" } ] )
更改用戶密碼
先用有更新用戶密碼權(quán)限的用戶登錄
下面
db.changeUserPassword("reporting", "SOh4TbYhxuLiW8ypJPxmt1oOfL")
看完上述內(nèi)容,你們掌握MongoDB的安裝啟動和服務(wù)化以及連接是怎樣的的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱:MongoDB的安裝啟動和服務(wù)化以及連接是怎樣的
轉(zhuǎn)載源于:http://www.rwnh.cn/article36/ipcssg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、網(wǎng)站改版、虛擬主機、定制網(wǎng)站、微信公眾號、標簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)