前提
Win7上测试。PHP5需要安装php_mongo.dll 而PHP7安装的是php_mongodb.dll扩展
各个扩展:
- php5.6.40 TS VC11 - mongo1.6.16 - mongodb1.6.10
- php7.3.5 TS VC15 - mongodb 1.5.5
创建用户
在shell中执行创建mongo用户
> use test
switched to db test
> db.createUser({
user: "test1",
pwd: "test1",
roles: [
{ role: "readWrite", db: "test" },
{ role: "read", db: "reporting" }
]
});
Successfully added user: {
"user" : "test1",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
},
{
"role" : "read",
"db" : "reporting"
}
]
}
>
> show users;
{
"_id" : "test.test1",
"userId" : UUID("406cb77c-1fc0-4f34-a257-1c975349533a"),
"user" : "test1",
"db" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
},
{
"role" : "read",
"db" : "reporting"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
>
> db.auth('test1','test1')
1
> db
test
测试代码
文件 E:/test/mongo.php
<?php
$config = [
'username' => 'test1',
'password' => 'test1',
'auth_db' => 'test',
'host' => '127.0.0.1',
'port' => '27017',
'db' => 'test',
'table' => 'foo',
// 'db' => 'reporting',
// 'table' => 'foo2',
];
$result = null;
if (PHP_VERSION_ID < 70000) {
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'],
));
$db = $conn->selectDB($config['db']);
$mongo = $db->selectCollection($config['table']);
$result = $mongo->findOne();
} catch (Exception $e) {
var_dump($e->getMessage());
}
} else {
// $manager = new MongoDB\Driver\Manager("mongodb://" . $config['username'] . ":" . $config['password'] . "@" . $config['host'] . ":" . $config['port'].'/'.$config['auth_db']);
$manager = new MongoDB\Driver\Manager("mongodb://" . $config['host'] . ":" . $config['port'] . '/' . $config['auth_db'], [
'username' => $config['username'],
'password' => $config['password'],
'db' => $config['auth_db']
]);
$filter = [];
$options = [];
$query = new MongoDB\Driver\Query($filter, $options); //查询请求
var_dump($config['db'] . "." . $config['table']);
try {
$result = $manager->executeQuery($config['db'] . "." . $config['table'], $query)->toArray();
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
var_dump($result);
输出
php5.6
D:\>d:/php56/php.exe e:/test/mongo.php
array(3) {
["_id"]=>
object(MongoId)#6 (1) {
["$id"]=>
string(24) "5d26ba7b1ed45d92904eb3d5"
}
["x"]=>
float(1)
["y"]=>
float(1)
}
D:\>
但PHP5.6有一个问题,如果是切换到非认证数据库 reporting,无法访问,报错提示:Authentication failed
。
D:\>d:/php56/php.exe e:/test/mongo.php
string(113) "Failed to connect to: 127.0.0.1:27017: SASL Authentication failed o
n database 'reporting': Authentication failed."
NULL
D:\>
可问题是我用终端和no sql 工具,比如No SQL Manager For MongoDB 都没有问题。
都可以访问,如下图。如果你知道这个问题的原因,希望能联系我 coconets@163.com
- php7.3
D:\>d:/php73/php.exe e:/test/mongo.php
string(14) "reporting.foo2"
array(1) {
[0]=>
object(stdClass)#6 (3) {
["_id"]=>
object(MongoDB\BSON\ObjectId)#5 (1) {
["oid"]=>
string(24) "5d26cde60e60a4d63cf516d1"
}
["name"]=>
string(3) "fff"
["pwd"]=>
string(3) "zzz"
}
}
D:\>d:/php73/php.exe e:/test/mongo.php
string(8) "test.foo"
array(1) {
[0]=>
object(stdClass)#6 (3) {
["_id"]=>
object(MongoDB\BSON\ObjectId)#5 (1) {
["oid"]=>
string(24) "5d26ba7b1ed45d92904eb3d5"
}
["x"]=>
float(1)
["y"]=>
float(1)
}
}
D:\>