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會同步到243和246mongos
測試
[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" :"[email protected]","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"})