mongo集群分片如何添加认证密码

时间:2021-05-14 08:42:28

mongo集群分片如何添加认证密码?

 

1.首先配置好集群

参考“重点实战mongodb3.0.5Relica Sets+sharding集群”  

2.登录到其中一个monogs,然后添加一个用户

$ mongo 172.28.19.237:60005

use admin

db.createUser({ user: 'root', pwd:'123456', roles:['root']})

PS: 创建的用户root会同步到243246mongos

测试

[mgousr03@mongodb-test1 ~]$  mongo -u root -p 123456 --host=172.28.19.237--port=60005 --authenticationDatabase admin mongo_test

MongoDB shell version: 3.0.5

connecting to:172.28.19.237:60005/mongo_test

 

 

3.在各个sharing分片添加用户

(1)172.28.19.237:37017添加用户

use admin

db.createUser({ user: 'root', pwd:'123', roles:['root']})

 

(2)172.28.19.237:37018添加用户

use admin

db.createUser({ user: 'root', pwd:'123', roles:['root']})

 

(3)172.28.19.237:37019添加用户

use admin

db.createUser({ user: 'root', pwd:'123', roles:['root']})

 

4.配置参数修改

(1)/data/users/mgousr03/mongodb/etc目录下生产一个keyFilers0.key文件

  所有配置文件共有keyFilers0.key文件

生成如下key

echo"c57a012cf2f8a8e20dd4b21a7fae48b3" >/data/users/mgousr03/mongodb/etc/keyFilers0.key

然后将keyFilers0.keycopy到其他主机上即可。

600 权限

chmod 600 keyFilers0.key

 

(2)修改配置参数

在各个shard*.conf参数中添加如下(启用认证)

auth=true

keyFile=/data/users/mgousr03/mongodb/etc/keyFilers0.key

 

在各个节点config.conf参数中添加如下(启用认证)

auth=true

keyFile=/data/users/mgousr03/mongodb/etc/keyFilers0.key

 

在各个节点mongos.conf参数中添加如下(启用认证)

keyFile=/data/users/mgousr03/mongodb/etc/keyFilers0.key

该参数不支持auth=true参数

 

5.查询mongodb各个进行

[mgousr03@mongodb-test1 etc]$ ps -ef| grep mongo |grep mgousr03 | grep -v grep

mgousr03  1907    1  1 10:27 ?        00:00:40 mongod -f shard1_1.conf

mgousr03  2130    1  0 10:28 ?        00:00:10 mongod -f shard2_1.conf

mgousr03  2225    1  1 10:29 ?        00:00:33 mongod -f shard3_1.conf

mgousr03  2777    1  0 10:38 ?        00:00:08 mongod -f config.conf

mgousr03  2798    1  1 10:39 ?        00:00:23 mongos -f mongos.conf

 

使用kill-2  进行kill掉。

 

6.启动mongo进程

先启动sharing分片,然后在动config,再启动mongos进程。

 

 

*******测试********

连接到mongos

[mgousr03@mongodb-test1 ~]$ mongo172.28.19.237:60005

MongoDB shell version: 3.0.5

connecting to:172.28.19.237:60005/test

mongos> show dbs;

2016-02-26T11:03:48.390+0800 EQUERY    Error: listDatabases failed:{

"ok" : 0,

"errmsg" : "not authorized on admin toexecute command { listDatabases: 1.0 }",

"code" : 13

}

   at Error (<anonymous>)

   at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)

   at shellHelper.show (src/mongo/shell/utils.js:630:33)

   at shellHelper (src/mongo/shell/utils.js:524:36)

   at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47

mongos> use admin

switched to db admin

mongos>db.auth('root','123456')   认证操作

1

或者直接登录 mongo-u root -p 123456 --host=172.28.19.237 --port=60005 --authenticationDatabaseadmin mongo_test

 

创建数据库,集合并记载分片数据:

对集合进行分片(chicago数据库中创建users集合)

mongos> use admin

switched to db admin

db.runCommand({"enablesharding":"wilson"})

mongos>db.runCommand({"shardcollection":"wilson.results","key":{user_id:"hashed"}})

use wilson

db.results.ensureIndex({user_id:"hashed"}, {background: true})

 

 for(var i=1;i<=100000;i++)db.results.insert({"ip" : "192.168.168.254","g_roup": "kiwi","mac" :"of:fd:67:8c:2f:8f","address" :"hongmei1801num","user_id" : i,"name" :"user10000000","title" :"system","database" :"mongodb","telphone" :NumberLong("15718441234"),"mail" :"yj@chinapnr.com","os" :"win7","company" : "chinapnr"})

 

 

 

登录其中一个sharing[mgousr03@mongodb-test1etc]$ mongo 172.28.19.237:37017

MongoDB shell version: 3.0.5

connecting to:172.28.19.237:37017/test

shard1:PRIMARY> use admin

switched to db admin

shard1:PRIMARY> show dbs;

2016-02-26T11:05:37.619+0800 E QUERY   Error: listDatabases failed:{

"ok" : 0,

"errmsg" : "not authorized on admin toexecute command { listDatabases: 1.0 }",

"code" : 13

}

   at Error (<anonymous>)

   at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)

   at shellHelper.show (src/mongo/shell/utils.js:630:33)

   at shellHelper (src/mongo/shell/utils.js:524:36)

   at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47

shard1:PRIMARY>db.auth('root','123')   认证操作

1

shard1:PRIMARY> show dbs;

admin       0.000GB

config      0.000GB

local       0.362GB

mongo_test  1.302GB

wilson      0.003GB

 

db.runCommand({"enablesharding":"wilson"})

 


本文出自 “九指神丐” 博客,请务必保留此出处http://beigai.blog.51cto.com/8308160/1751381