安裝
安裝方式1:yum
參考
https://docs.mongodb.com/manual/tutorial/i...
安裝完後的配置檔案
/etc/mongod.conf
bindIp: 172.0.0.1 改為 bindIp: 0.0.0.0
啟動mongo:
systemctl start mongod
參考官網文件
下載地址:
https://www.mongodb.com/download-center/co...
官網安裝文件:
https://docs.mongodb.com/manual/tutorial/i...
下載後解壓到:
/usr/local/mongodb
配置環境變數:
vim /etc/profile
export MONGODB_HOME=/usr/local/mongodb
PATH="${JAVA_HOME}:${PATH}:${GRADLE_HOME}/bin:${MAVEN_HOME}/bin:${MYSQL_HOME}/bin:{PYTHON_HOME}:${ZOOKEEPER_HOME}/bin:${GROOVY_HOME}/bin:${JENKINS_HOME}:${GRADLE_USER_HOME}:${M2_HOME}:${MONGODB_HOME}/bin"
export PATH
source ~/.zshrc
不使用預設的地址:
/data/db
修改許可權:
sudo chown -R $(whoami) /data
建立自定義的資料地址:
mkdir -p /data/mongodb
建立日誌地址:
mkdir -p /data/logs/mongodb
官網配置檔案說明:
https://docs.mongodb.com/manual/reference/...
建立配置檔案:
vim /usr/local/mongodb/bin/mongodb.conf
systemLog:
destination: file
path: "/data/logs/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/data/mongodb"
journal:
enabled: true
processManagement:
fork: true # 以守護程式的方式啟動,即在後臺執行
net:
bindIp: 0.0.0.0 # 允許所有連線
port: 27017
setParameter:
enableLocalhostAuthBypass: false
啟動mongo:
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
配置開機啟動(未驗證):
cd /etc/init.d
vi mongodb(建立啟動檔案,給予執行許可權,內容如下,確保下面啟動和關閉的命令可以正常執行)
#!/bin/sh
# chkconfig: 2345 75 15
# description: mongodb
### BEGIN INIT INFO
# Provides: mongodb
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mongodb
# Description: mongo db server
### END INIT INFO
EXE_FILE=/usr/local/mongodb/mongodb-linux-x86_64-3.6.4/bin/mongod
CONFIG_FILE=/usr/local/mongodb/mongodb-linux-x86_64-3.6.4/bin/mongodb.conf
#. /lib/lsb/init-functions
MONGOPID=`ps -ef| grep mongod| grep -v grep| awk '{print $2}'`
test -x $EXE_FILE || exit 0
case "$1" in
start)
ulimit -n 3000
$EXE_FILE --config $CONFIG_FILE
;;
stop)
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
;;
status)
ps -aux| grep mongod
;;
*)
log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}"
exit 1
esac
exit 0
service mongodb start //啟動mongodb服務
chkconfig mongodb on //開啟開機啟動
參考:
https://blog.csdn.net/ztx114/article/detai...
使用systemctl方式參考:
https://www.jianshu.com/p/d4ae4f5d283a
進入mongo:
/usr/local/mongodb/bin/mongo --port 27017
此時,不需要任何密碼就可以登入
所以,需要配置賬號密碼
use admin;
db.createUser( {user: "admin",pwd: "admin123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]});
db.adminCommand( { shutdown: 1 } )
修改配置檔案:
systemLog:
destination: file
path: "/data/logs/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/data/mongodb"
journal:
enabled: true
processManagement:
fork: true # 以守護程式的方式啟動,即在後臺執行
net:
bindIp: 0.0.0.0 # 允許所有連線
port: 27017
setParameter:
enableLocalhostAuthBypass: false
security:
authorization: enabled
再次重啟mongo
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
此時使用命令,已經無法查詢出來
show users
使用授權命令:
db.auth("admin", "admin123456")
即可成功
參考:
http://blog.itpub.net/69902483/viewspace-2...
mongoduGUI:
https://www.cnblogs.com/shiweida/p/7692468...
進入mongo
mongo
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> db.enableFreeMonitoring()
{
"state" : "enabled",
"message" : "To see your monitoring data, navigate to the unique URL below. Anyone you share the URL with will also be able to view this page. You can disable monitoring at any time by running db.disableFreeMonitoring().",
"url" : "https://cloud.mongodb.com/freemonitoring/cluster/4HFMUQN67BMLJMH5SGZLFHTZI7V6TXJM",
"userReminder" : "",
"ok" : 1
}
問題解決:Unit mongod.service entered failed state
檢視
cat /var/log/mongodb/mongod.log
發現是tmp中的mongo.lock沒有被刪除
rm /tmp/mongodb-27017.sock
即可解決
Aug 02 18:55:18 iZbp12ft4a1zgzzaoq9xgrZ sudo[3571]: pam_unix(sudo:session): session closed for user root
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ polkitd[2020]: Registered Authentication Agent for unix-process:3582:87621665 (system bus name :1.3488 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/A
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Starting MongoDB Database Server...
-- Subject: Unit mongod.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mongod.service has begun starting up.
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ mongod[3596]: about to fork child process, waiting until server is ready for connections.
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ mongod[3596]: forked process: 3599
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ mongod[3596]: ERROR: child process failed, exited with error number 14
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ mongod[3596]: To see additional information in this output, start without the "--fork" option.
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: mongod.service: control process exited, code=exited status=14
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Failed to start MongoDB Database Server.
-- Subject: Unit mongod.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mongod.service has failed.
--
-- The result is failed.
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: Unit mongod.service entered failed state.
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ systemd[1]: mongod.service failed.
Aug 02 18:55:43 iZbp12ft4a1zgzzaoq9xgrZ polkitd[2020]: Unregistered Authentication Agent for unix-process:3582:87621665 (system bus name :1.3488, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disc
[root@iZbp12ft4a1zgzzaoq9xgrZ download]#
[root@iZbp12ft4a1zgzzaoq9xgrZ lib]# cat /var/log/mongodb/mongod.log
2019-08-02T18:55:43.660+0800 I CONTROL [main] ***** SERVER RESTARTED *****
2019-08-02T18:55:43.663+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] MongoDB starting : pid=3599 port=27017 dbpath=/var/lib/mongo 64-bit host=iZbp12ft4a1zgzzaoq9xgrZ
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] db version v4.0.11
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] git version: 417d1a712e9f040d54beca8e4943edce218e9a8c
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] allocator: tcmalloc
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] modules: none
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] build environment:
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] distmod: rhel70
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] distarch: x86_64
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] target_arch: x86_64
2019-08-02T18:55:43.674+0800 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid", timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2019-08-02T18:55:43.674+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
2019-08-02T18:55:43.674+0800 F - [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 693
2019-08-02T18:55:43.674+0800 F - [initandlisten]
***aborting after fassert() failure
[root@iZbp12ft4a1zgzzaoq9xgrZ lib]# rm /tmp/mongodb-27017.sock
rm: remove socket ‘/tmp/mongodb-27017.sock’? y
許可權管理
對於admin庫,可以
use admin
db.createUser( {user: "admin",pwd: "密碼",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]});
是使用資料庫,不是使用使用者
use guache
通過使用資料庫後,再去建立該資料庫的使用者
db.createUser( {user: "guache",pwd: "密碼",roles: [ { role: "readWrite", db: "guache" } ]});
刪除使用者
db.dropUser('guache')
mongo-express
https://github.com/mongo-express/mongo-exp...
demo:
https://github.com/madhums/node-express-mo...
必須先安裝完NodeJS,部落格中有
npm install -g mongo-express
[root@iZbp12ft4a1zgzzaoq9xgrZ mongo-express]# npm install -g mongo-express
/usr/local/node-v10.16.1-linux-x64/bin/mongo-express -> /usr/local/node-v10.16.1-linux-x64/lib/node_modules/mongo-express/app.js
+ mongo-express@0.53.0
檢視安裝目錄:
[root@iZbp12ft4a1zgzzaoq9xgrZ mongo-express]# npm list -g mongo-express
/usr/local/node-v10.16.1-linux-x64/lib
└── mongo-express@0.53.0
參考:
https://www.jianshu.com/p/b6ea35eb4762
https://blog.csdn.net/Algorithmguy/article...
pm2 安裝
npm install -g pm2
pm2啟動應用
pm2 start app.js --name mongo-express # 啟動應用,併為之命名
檢視pm2管理的應用執行狀態
pm2 list
停止應用
pm2 stop mongo-express
從pm2列表裡刪除應用
pm2 delete mongo-express
mongo 角色具體說明
Read:允許使用者讀取指定資料庫
readWrite:允許使用者讀寫指定資料庫
dbAdmin:允許使用者在指定資料庫中執行管理函式,如索引建立、刪除,檢視統計或訪問system.profile
userAdmin:允許使用者向system.users集合寫入,可以找指定資料庫裡建立、刪除和管理使用者
dbOwner:資料庫擁有者,包含readWrite、dbAdmin、userAdmin
clusterAdmin:只在admin資料庫中可用,賦予使用者所有分片和複製集相關函式的管理許可權
readAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀許可權
readWriteAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀寫許可權
userAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的userAdmin許可權
dbAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的dbAdmin許可權
root:只在admin資料庫中可用。超級賬號,超級許可權
https://blog.csdn.net/shidebin/article/det...
https://blog.csdn.net/su377486/article/det...
本作品採用《CC 協議》,轉載必須註明作者和本文連結