启用shareding认证

来源:互联网 发布:js设置div颜色 编辑:程序博客网 时间:2024/05/21 06:22

启用shareding认证必须停止集群

Enforce Keyfile Internal Authentication on Existing Sharded Cluster Deployment


Create a keyfile.

shareding cluster 成员服务器所有的keyfile的内容必须完成一致
 
可以使用任意方法生成keyfile文件的内容,但是内容的长度必须在6-1024位
 
下面的方法是使用openssl生成1024个随机字符作为keyfile
 
openssl rand -base64 755 > <path-to-keyfile>chmod 400 <path-to-keyfile>

Copy the keyfile to each component in the sharded cluster.

 

拷贝keyfile到shareding cluster 所有成员服务器中的相同位置


Disable the Balancer.

必须停用balance,并且直到balance完全停止为止
可以通过查看sh.status()查看是否就还有正在running的balance
sh.stopBalancer()
sh.getBalancerState()

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服务器

 


Enforce Access Control for each Shard in the Sharded Cluster.

然后启动每个分片的mongod replset服务器

启用keyfile配置文件参数,重新启动mongod服务器,

修改mongod启动配置文件

keyFile=/etc/mongo.key
replSet=replset

以配置文件的方式启动mongod

mongod --config <path-to-config-file>

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" } ]
  }
)

 


Authenticate as cluster admin.

 

使用新创建的clusterAdmin账号登陆


Start the load balancer.

使用新创建的clusterAdmin账号启动balance

 

sh.stopBalancer()
sh.getBalancerState()

Create additional users (Optional).

创建其它有需要的用户角色

原创粉丝点击