启用shareding认证必须停止集群
Enforce Keyfile Internal Authentication on Existing Sharded Cluster Deployment
Shut down all mongos instances for the sharded cluster.
首先关闭所有的mongos服务器
db.getSiblingDB("admin").shutdownServer()
Shut down config server mongod instances.
然后关闭所有的config服务器
db.getSiblingDB("admin").shutdownServer()
Shut down shard replica set mongod instances.
关闭所有的mongoDB replset 分片
确保mongoDB replset 的primary节点是最后关闭的
db.getSiblingDB("admin").shutdownServer()
Enforce Access Control on the Config Servers.
首先启动config服务器
启用keyfile配置文件参数,重新启动mongod服务器,
修改mongod启动配置文件
keyFile=/etc/mongo.key
replSet=replset
以配置文件的方式启动mongod
mongod --config <path-to-config-file>
在MONGODB3.2版本中,不建议CONFIG服务器使用MIRROR的方式,推荐使用REPLSET的方式启动CONFIG服务器
Create a Shard-Local User Administrator (Optional).
单独配置每个分片的本地管理账号,这个账号只能在这个分片使用
admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: "changeme1",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Enforce Access Control for the mongos servers.
最后启动mongos服务器
启用keyfile配置文件参数,重新启动mongod服务器,
修改mongod启动配置文件
keyFile=/etc/mongo.key
replSet=replset
以配置文件的方式启动mongod
mongod --config <path-to-config-file>
如果config DB 服务器是一个replset的话,则
configdb = replset/192.168.1.1:27017,192.168.1.2:27017,192.168.1.3:27017
这个地方最少填写上replset的一个成员
如果config DB 服务器是mirror的话,则
configdb = 192.168.1.1:27017,192.168.1.2:27017,192.168.1.3:27017
必须填写上所有的mirror成员
mongoDB3.2不推荐使用mirror方式配置config DB服务器
Connect to the mongos instance over the localhost interface.
必须在mongos服务器所在的物理服务器上启动mongo shell 并连接到本地的mongod端口
这个本地端口在第一次创建用户后就会失效
Create the user administrator.
创建集群管理员
admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "fred",
pwd: "changeme1",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Authenticate as the user administrator.
使用新创建的用户登录到admin库
在mongo shell中使用db.auth()来进行认证
db.getSiblingDB("admin").auth("fred", "changeme1" )
另外还可以使用下面的方式登录到集群
mongo -u "fred" -p "changeme1" --authenticationDatabase "admin"
Create Administrative User for Cluster Management
创建集群管理员在admin数据库中
db.getSiblingDB("admin").createUser(
{
"user" : "ravi",
"pwd" : "changeme2",
roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
}
)