mongodb 3.4.5安裝及安全配置

1、yum安裝mongodbjavascript

vi /etc/yum.repos.d/mongodb-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.ascjava

yum install -y mongodb-orglinux

service mongod start
service mongod stop
service mongod restartmongodb

2、源碼安裝
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.5.tgz
tar zxvf mongodb-linux-x86_64-3.4.5.tgz
mv mongodb-linux-x86_64-3.4.5 /usr/local/mongodb
export PATH=/usr/local/mongodb/bin:$PATH
mkdir -p /data/mongodb
useradd mongo
chown mongo.mongo /data/mongodbshell

echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
ulimit -HSn 65535數據庫

su mongo安全

/usr/local/mongodb/bin/mongod --dbpath=/data/mongodb函數

這樣就以普通用戶mongo身份運行了mongodbui

3、安全認證設置url

新建用戶
/usr/local/mongodb/bin/mongo

>user admin
>db.createUser({user:"root",pwd:"uiop789",roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"dbAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]})
root:只在admin數據庫中可用。超級帳號,超級權限
Read:容許用戶讀取指定數據庫
readWrite:容許用戶讀寫指定數據庫
dbAdmin:容許用戶在指定數據庫中執行管理函數,如索引建立、刪除,查看統計或訪問system.profile
userAdmin:容許用戶向system.users集合寫入,能夠找指定數據庫裏建立、刪除和管理用戶
clusterAdmin:只在admin數據庫中可用,賦予用戶全部分片和複製集相關函數的管理權限
clusterAdmin:只在admin數據庫中可用,賦予用戶全部分片和複製集相關函數的管理權限
readWriteAnyDatabase:只在admin數據庫中可用,賦予用戶全部數據庫的讀寫權限
userAdminAnyDatabase:只在admin數據庫中可用,賦予用戶全部數據庫的userAdmin權限
dbAdminAnyDatabase:只在admin數據庫中可用,賦予用戶全部數據庫的dbAdmin權限

啓用認證
/usr/local/mongodb/bin/mongod --dbpath=/data/mongodb --auth

1,建立鏈接mongo shell時進行受權,指定-u <username>, -p <password>, and the --authenticationDatabase <database>
mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"

2,mongo shell登陸後認證

use admin
db.auth("root","uiop789")
輸出1,即認證成功


>show dbs  查看庫
>db.system.users.find() 查詢全部用戶
>show users 查看用戶
>db.system.users.remove("user":"username") 刪除用戶username
>db.changeUserPassword("root","newpass") 修改用戶密碼

>use admin  切換到admin
>db.shutdownServer() 關閉mongodb

4、阿里的一個配置模板

mongod.conf內容 (將$mymongo替換成你的工做目錄)

systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: $mymongo/logs/mongod.log
  timeStampFormat: iso8601-local
  traceAllExceptions: false
  verbosity: 0

processManagement:
  fork: true
  pidFilePath: $mymongo/logs/mongod.pid

net:    
  #bindIp: 127.0.0.1
  port: 3001
  http:
    enabled: false
  maxIncomingConnections: 1000
  unixDomainSocket:
    enabled: false

operationProfiling:
  mode: slowOp
  slowOpThresholdMs: 100    

security:
  authorization: enabled
  keyFile: $mymongo/etc/keyfile
  javascriptEnabled: false

replication:
  oplogSizeMB: 5120
  replSetName: myreplset

storage:
  dbPath: $mymongo/data
  directoryPerDB: true
  syncPeriodSecs: 60
  engine: wiredTiger
  journal:
    enabled: true
    commitIntervalMs: 100
  wiredTiger:
    engineConfig:
      cacheSizeGB: 4

mongod -f /etc/mongodb.conf --dbpath=/data/mongodb --auth

相關文章
相關標籤/搜索