背景:業務反饋因爲MongoDB遷移致使PHP客戶端鏈接DB異常,排查發現MongoDB3.0版本的鑑權認證機制是SCRAM-SHA-1,程序驅動異常,需降級爲鑑權認證機制:MONGODB-CR
鑑權降級操做以下(在無鑑權地狀況下登錄):
一、配置文件註釋鑑權
security:
keyFile: /data1/ceshi27020_mongo/etc/keyfile
authorization: enabled
二、重啓mongo
三、在主上執行以下命令:
db.system.users.find();
db.system.version.findOne();
var schema = db.system.version.findOne({"_id" : "authSchema"})
schema.currentVersion = 3
db.system.version.save(schema)
重建用戶:
db.dropUser('test_rw');
db.createUser( { "user" : "test_rw",
"pwd": "iwoCsl173",
"roles" : [{ role: "readWrite", db: "lsession" }] });
四、開啓鑑權,重啓MongoDB
3.0版本的鑑權認證機制:SCRAM-SHA-1
降級以後的鑑權認證機制:MONGODB-CR
PHP鏈接MongoDB代碼示例:
root@xxx-xxxx:/# cat mongo.php
<?php
$server = 'mongodb://10.x.x.x:27030';
$options = array(
'db'=>'admin',//',
'username' => 'dba',//',
'password' => 'xxxxxxxx',
'replicaSet' => 'test3',
'readPreference' => MongoClient::RP_SECONDARY_PREFERRED,
// 'readPreference' => MongoClient::RP_PRIMARY,
'connectTimeoutMS'=>5000,
);
$mongo = new MongoClient($server, $options);
$db = $mongo->selectDB('zyq');
$coll = $db->selectCollection('test10');
$result = $coll->find(array("userid"=>30748));
while($doc = $result->getNext()){
var_dump($doc);
}
執行OK!