douhanxujiuji6098 2016-11-10 13:28
浏览 370

Mongodb通过Laravel / PHP检查连接是否正常

I'm using laravel 5.2 and MongoDB 3.2.

I want to test if connection with is ok before my app starts (i can't use DB facade), in the monolog configuration. If connection is not ok, i will use logging in file.

By recommendation, i'm testing MongoClient, Mongo and MongoDB\Client, and using whatever is enabled.

I'm trying to test mongo connect as the following:

$mongoClient = new \MongoDB\Client('mongodb://localhost:27017');
$mongoClient->selectCollection('mydb', 'mycollection');

That's the return:

Client {   
    +manager: Manager {#21}
    +uri: "mongodb://localhost:27017"   
    +typeMap: [
        array => "MongoDB\Model\BSONArray",
        document => "MongoDB\Model\BSONDocument",
        root => "MongoDB\Model\BSONDocument"   
    ]
}

Finnaly, my questions:

  • Exists a way to use DB facade before app starts?
  • How and what is the right way to test MongoDB connection with PHP?

If you has another suggestion, i will be thankful.

  • 写回答

2条回答 默认 最新

  • dpg78570 2016-11-10 13:43
    关注

    using this way you can check MongoDB connection with PHP:

    $connection = new MongoClient(); // connects to localhost:27017
    $connection = new MongoClient( "mongodb://example.com" ); // connect to a remote host (default port: 27017)
    $connection = new MongoClient( "mongodb://example.com:65432" ); // connect to a remote host at a given port
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b