Skip to content

Commit 9f4e292

Browse files
committed
Add mongodb connect example
1 parent 5ed63e9 commit 9f4e292

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

www/localhost/index.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
error_reporting(E_ALL);
3+
ini_set('display_errors', 1);
24

35
echo '<h1 style="text-align: center;">欢迎使用DNMP!</h1>';
46
echo '<h2>版本信息</h2>';
@@ -8,6 +10,7 @@
810
echo '<li>Nginx版本:', $_SERVER['SERVER_SOFTWARE'], '</li>';
911
echo '<li>MySQL服务器版本:', getMysqlVersion(), '</li>';
1012
echo '<li>Redis服务器版本:', getRedisVersion(), '</li>';
13+
echo '<li>MongoDB服务器版本:', getMongoVersion(), '</li>';
1114
echo '</ul>';
1215

1316
echo '<h2>已安装扩展</h2>';
@@ -53,6 +56,27 @@ function getRedisVersion()
5356
}
5457
}
5558

59+
/**
60+
* 获取MongoDB版本
61+
*/
62+
function getMongoVersion()
63+
{
64+
if (extension_loaded('mongodb')) {
65+
try {
66+
$manager = new MongoDB\Driver\Manager('mongodb://root:123456@mongodb:27017');
67+
$command = new MongoDB\Driver\Command(array('serverStatus'=>true));
68+
69+
$cursor = $manager->executeCommand('admin', $command);
70+
71+
return $cursor->toArray()[0]->version;
72+
} catch (Exception $e) {
73+
return $e->getMessage();
74+
}
75+
} else {
76+
return 'MongoDB 扩展未安装 ×';
77+
}
78+
}
79+
5680
/**
5781
* 获取已安装扩展列表
5882
*/

0 commit comments

Comments
 (0)