Home > Article > Backend Development > Detailed explanation and cases of connecting Mongodb to remote database under PHP
这篇文章主要介绍了PHP下 Mongodb 连接远程数据库的实例代码,需要的朋友可以参考下
WINDOWS 下装MongoDB
1、在mongodb的文件夹下创建 data、logs 文件夹 和mongo.conf
命令行命令!
D:\mongodb\bin> mongod --dbpath D:\mongodb\data --logpath=D:\mongodb\logs\mongodb.log --logappend D:\mongodb\bin> mongod -dbpath "d:\mongodb\data\db" D:\mongodb\bin> mongod --config "d:\mongodb\mongo.conf"
2、配置mongo.conf
dbpath=D:\Program Files\MongoDB\Server\3.2\data #数据库路径 logpath=D:\Program Files\MongoDB\Server\3.2\logs\mongo.log #日志输出文件路径 logappend=true #错误日志采用追加模式 journal=true #启用日志文件,默认启用 quiet=true #这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false port=27017 #端口号 默认为27017
3、将mongo设为服务,方便以后启动
mongod --config "D:\Program Files\MongoDB\Server\3.2\mongo.conf" --install --serviceName "MongoDB" net start MongoDB
PHP连接
$conn = new MongoClient("mongodb://xxx",array('username'=>'username','password'=>'password')); $db = $conn->test; $collection = $db->test; $cursor = $collection->find(); foreach ($cursor as $id => $value) { echo "$id: "; var_dump($value); echo "<br>"; }
相关推荐:
The above is the detailed content of Detailed explanation and cases of connecting Mongodb to remote database under PHP. For more information, please follow other related articles on the PHP Chinese website!