# mongod.conf
# 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: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# 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: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
rs0:PRIMARY> db # 查看当前db
test
rs0:PRIMARY> use admin #切换db
switched to db admin
rs0:PRIMARY> db
admin
rs0:PRIMARY> db.createUser({ user: "root", pwd: "123456", roles: [{ role: "root", db: "admin" }] }) # 创建用户,给root角色
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
rs0:PRIMARY> db.auth("root","123456") # 查看用户角色是否成功
1
rs0:PRIMARY> rs.conf()
5. 查看集群状态
rs.status()
6. 验证数据
# master插入数据
rs0:PRIMARY> use test
switched to db test
rs0:PRIMARY> db.user.insert({"name":"123456"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.user.find()
{ "_id" : ObjectId("5c17be0959c694bdb2d421ca"), "name" : "123456" }
rs0:PRIMARY> show tables
user
# secondary查询数据
rs0:SECONDARY> use admin # 先登录到admin认证
switched to db admin
rs0:SECONDARY> db.auth("root","123456")
1
rs0:SECONDARY> rs.slaveOk(true) # 打开slave查询功能
rs0:SECONDARY> use test # 切换到test数据库查询数据
switched to db test
rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5c17be0959c694bdb2d421ca"), "name" : "123456" }