3、分片集群开启认证

开启授权认证之后,只有经过授权的用户才能够连接到MongoDB,才允许访问相关的资源,增加数据库的安全性与稳定性。

分片集群适合keyFile的形式开启认证,而副本集适合开启auth即可。

思路:为5个集群分片,shard1、shard2、shard3、shard4、shard5分别创建超级用户(用来分别管理Mongo集群的分片),再为集群创建一个管理用户,控制外部链接对集群进程Mongos的访问。 

3、分片集群开启认证_第1张图片

1、为shard1创建分片管理超级用户【shard2,shard3,shard4,shard5类似】

cd/usr/local/mongodb/bin

./mongo 127.0.0.145:22001

#查看副本集状态

shard1:PRIMARY>rs.status()

shard1:PRIMARY>use admin

#添加超级用户

shard1:PRIMARY>db.createUser(

        {

                  user:"ybl_shard1",

                  pwd:"ybl_shard1",

                  roles:[{role:"root",db:"admin"}]

        }

shard1:PRIMARY> db.auth("ybl_shard1","ybl_shard1")

#这一步是为了验证角色是否创建成功,1-成功,其他-失败

#查看用户

shard1:PRIMARY>show users

2、为基于副本集的分片集群创建超级管理用户

cd/usr/local/mongodb/bin

./mongo 127.0.0.143:20000

mongos> use admin

switched to db admin

mongos>db.createUser(

{

user:"yuanbl",

pwd:"yuanbl",

roles:[{role:"root",db:"admin"}]

}

);

db.auth("yuanbl","yuanbl")

mongos> db.auth("yuanbl","yuanbl")

3、关闭集群进程

可先关闭shard服务,systemctl stop mongodb

4、创建keyFile文件

1、mkdir -p /opt/mongo/keyfile/

2、cd/opt/mongo/keyfile

3、openssl rand -base64 753>mongo-keyfile

4、chmod 600 keyfile

注意点:

3步骤中的数字不宜太大,否则启动时会报错

【security

key in /opt/mongo/keyfile/keyfile has length 1368, must be between 6 and 1024

chars】

4步骤一定得执行,要不然报错

【permissions on /opt/mongo/keyfile/mongo-keyfile are too open】

5、将文件keyFile存储在在所有的节点指定位置

mkdir –p /usr/local/mongodb/key

mongo-keyfile存放在此目录下

6、使用keyFile参数指定keyfile启动分片shard1、shard2、shard3、shard4、shard5

修改所有shard启动配置文件,增加keyFile =/usr/local/mongodb/key/mongo-keyfile

然后启动Systemctl start mongodb

7、shard1、shard2、shard3、shard1、shard1、服务器端基于keyfile的用户口令认证测试

./mongo admin --port 22001 -uybl_shard1 -p ybl_shard1

./mongo 127.0.0.145:22001

use admin

db.auth(“ybl_shard1”,”ybl_shard1”)

其他类似

如果认证不成功就会报错

2017-03-12T17:40:16.236+0800 EQUERY[thread1] Error: listDatabasesfailed:{

         "ok" : 0,

         "errmsg" : "notauthorized on admin to execute command { listDatabases: 1.0 }",

         "code" : 13

} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

shellHelper.show@src/mongo/shell/utils.js:761:19

shellHelper@src/mongo/shell/utils.js:651:15

@(shellhelp2):1:1

到此分片开启成功

8、config server按照如上步骤处理

1、先关闭config server:systemctl stop mongodb 或 kill -2 pid

2、将moongo-keyfile放入目录下/usr/local/mongodb/key,设置权限chmod 600 mongo-keyfile

3、修改config server启动配置文件,加入keyFile = /usr/local/mongodb/key/mongo-keyfile,保存

4、启动config server:

systemctl start mongodb

9、mongos按照如上步骤处理

1、先关闭mongos:systemctl stop mongos或kill -2 pid

2、将moongo-keyfile放入目录下/usr/local/mongodb/key/,设置权限chmod600 mongo-keyfile

3、修改mongos启动配置文件,加入keyFile =/usr/local/mongodb/key/mongo-keyfile,保存

4、启动mongos:

systemctlstart mongos

10、测试分片集群基于keyfile的用户口令认证

cd/usr/local/mongodb/bin/

./mongo127.0.0.143:20000

showdbs

2017-03-12T18:04:28.845+0800E QUERY[thread1] Error: listDatabasesfailed:{

         "ok" : 0,

         "errmsg" : "notauthorized on admin to execute command { listDatabases: 1.0 }",

         "code" : 13

} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1

shellHelper.show@src/mongo/shell/utils.js:761:19

shellHelper@src/mongo/shell/utils.js:651:15

@(shellhelp2):1:1

useadmin

db.auth("yuanbl","yuanbl")

1  #说明认证成功

#登陆时指定

./mongoadmin --port 20000 -u yuanbl -p yuanbl

./mongo 127.0.0.143:20000

Use admin

db.auth("yuanbl","yuanbl")

你可能感兴趣的:(3、分片集群开启认证)