Skip to content

Commit

Permalink
Add mongodb connect example
Browse files Browse the repository at this point in the history
  • Loading branch information
yeszao committed Oct 6, 2019
1 parent 5ed63e9 commit 9f4e292
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions www/localhost/index.php
@@ -1,4 +1,6 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

echo '<h2>已安装扩展</h2>';
Expand Down Expand Up @@ -53,6 +56,27 @@ function getRedisVersion()
}
}

/**
* 获取MongoDB版本
*/
function getMongoVersion()
{
if (extension_loaded('mongodb')) {
try {
$manager = new MongoDB\Driver\Manager('mongodb://root:123456@mongodb:27017');
$command = new MongoDB\Driver\Command(array('serverStatus'=>true));

$cursor = $manager->executeCommand('admin', $command);

return $cursor->toArray()[0]->version;
} catch (Exception $e) {
return $e->getMessage();
}
} else {
return 'MongoDB 扩展未安装 ×';
}
}

/**
* 获取已安装扩展列表
*/
Expand Down

0 comments on commit 9f4e292

Please sign in to comment.