CentOS 部署MongoDB 4.0

如果配置文件 /data/mongodb/conf/mongodb.conf 包含如下內容

security:
  authorization: enabled
  1. kill mongo 進程(不要加-9)
  2. 修改配置文件 開啓匿名登陸
security:
  authorization: disabled
  1. 重新啓動mongod /data/mongodb/bin/mongod --config /data/mongodb/conf/mongodb.conf
  2. 匿名方式登陸 mongo
use admin
db.system.users.find()

已經存在admin賬戶,不需要創建admin,但是不知道密碼

db.createUser({ user: "root", pwd: "xxx", roles: [{ role: "root", db: "admin" }] })

修改密碼

db.changeUserPassword('root','xxx')

退出重新登陸(一個mongo客戶端鏈接不能切換數據庫)

use gre
db.createUser({ user: "aa", pwd: "xxx", roles: [{ role: "dbOwner", db: "aa" }] })
  1. 修改配置文件 kill 進程重啓 開啓認證登陸
security:
  authorization: enabled
  1. 鏈接認證
use aa
db.auth('aa','xxx')
db.aa.insert({})
show tables

最終的配置文件如下:

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/
  wiredTiger:
    engineConfig:
      cacheSizeGB: 20
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 172.22.0.12  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

security:
 authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章