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: /data/mongodb/log/mongod.log
# Where and how to store data.
storage:
dbPath: /data/mongodb/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/run/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
setParameter:
enableLocalhostAuthBypass: false
#security:
# clusterAuthMode: keyFile
# keyFile: /data/mongodb/key/repl_set.key
# authorization: enabled
#operationProfiling:
replication:
replSetName: llRepSet
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
先启动不带参数 --auth
主库配置集群(3个节点,才能保证关闭一个节点后仍然能够使用,因为两个节点才能保证选举成功)
config = { _id: "llRepSet", members: [
{_id: 0, host: "192.168.0.83:27017"},
{_id: 1, host: "192.168.0.80:27017"},
{_id: 2, host: "192.168.0.80:37017"}]
}
rs.initiate(config)
rs.status();
以上命令只要在一台服务器上运行即可
再设置账号
use admin
db.createUser({user:'root',pwd:'********,roles:['root']})
db.auth('root','********')
再关掉主库
db.shutdownServer()
再修改复制集配置
再启动主库带参数 --auth
从库1再关掉从库
db.shutdownServer()
再启动从库1带参数 --auth
从库2再关掉从库
db.shutdownServer()
再启动从库2带参数 --auth
再创建数据库