mongodb php 报错,PHP5 mongodb 切换db报错 Authentication failed on database admin-汗血宝马
權限
已經開啟,在test上創建了用戶test1,并賦予了 test的readWrite 和 reporting的read權限。
{
"_id" : "test.test1",
"userId" : UUID("406cb77c-1fc0-4f34-a257-1c975349552a"),
"user" : "test1",
"db" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
},
{
"role" : "read",
"db" : "reporting"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
使用CLI模式進行連接,mongo -u test1 -p –authenticationDatabase test 可以進入,show dbs會顯示兩個庫。
D:\>mongo -u test1 -p --authenticationDatabase test
MongoDB shell version v4.0.10
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=test&gssapiServiceName=mong
odb
Implicit session: session { "id" : UUID("b0e13217-63b3-454b-b8fc-32dd24fbfd1d")
}
MongoDB server version: 4.0.10
> show dbs
reporting ?0.000GB
test ? ? ? 0.000GB
> use reporting
switched to db reporting
> show collections
foo2
>
問題
但使用PHP連接mongodb,卻只能在test庫上操作,無法連接到 reporting 庫。可在cli模式是可以。
$config = [
'username' => 'test1',
'password' => 'test1',
'auth_db' => 'test',
'host' => '127.0.0.1',
'port' => '27017',
'db' => 'reporting',
'table' => 'foo2',
];
try {
$conn = new MongoClient("mongodb://" . $config['username'] . ':' . $config['password'] . '@' . $config['host'] . ':' . $config['port'] . '/' . $config['auth_db']);
// ? ? ? ?$conn = new MongoClient("mongodb://" . $config['host'], array(
// ? ? ? ? ? ?'username' => $config['username'],
// ? ? ? ? ? ?'password' => $config['password'],
// ? ? ? ? ? ?'db' => $config['auth_db']
// ? ? ? ?));
$dbs = $conn->selectDB($config['db']);
$mongo = $dbs->selectCollection($config['table']);
$row = $mongo->findOne();
} catch (Exception $e) {
var_dump($e->getMessage());
}
報錯內容
string(113) "Failed to connect to: 127.0.0.1:27017: SASL Authentication failed o
n database 'reporting': Authentication failed."
可問題是 test1用戶對 reporting也有read權限啊,而且在 cli模式是完全可以列出和操作。
總結
以上是生活随笔為你收集整理的mongodb php 报错,PHP5 mongodb 切换db报错 Authentication failed on database admin-汗血宝马的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: composer php 使用方法,Co
- 下一篇: Oracle 移植 失败,Oracle迁