日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

mongodb php 报错,PHP5 mongodb 切换db报错 Authentication failed on database admin-汗血宝马

發(fā)布時間:2025/3/20 php 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongodb php 报错,PHP5 mongodb 切换db报错 Authentication failed on database admin-汗血宝马 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

權(quán)限

已經(jīng)開啟,在test上創(chuàng)建了用戶test1,并賦予了 test的readWrite 和 reporting的read權(quán)限。

{

"_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模式進(jìn)行連接,mongo -u test1 -p –authenticationDatabase test 可以進(jìn)入,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());

}

報錯內(nèi)容

string(113) "Failed to connect to: 127.0.0.1:27017: SASL Authentication failed o

n database 'reporting': Authentication failed."

可問題是 test1用戶對 reporting也有read權(quán)限啊,而且在 cli模式是完全可以列出和操作。

總結(jié)

以上是生活随笔為你收集整理的mongodb php 报错,PHP5 mongodb 切换db报错 Authentication failed on database admin-汗血宝马的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。