【发布时间】:2023-04-02 07:53:01
【问题描述】:
我已经安装了一个运行(alpine)linux 的新服务器,带有 Apache 和 PHP7。我还安装了 mongodb 驱动程序。以下是我运行 phpinfo() 时看到的内容:
mongodb support enabled
mongodb version 1.1.10-dev
mongodb stability devel
libmongoc version 1.3.6
libbson version 1.3.6
我还安装了 mongodb php7 库 (https://pkgs.alpinelinux.org/package/edge/testing/x86_64/mongo-php7-library),它为我创建了以下目录结构:
webdev1:/var/www/# ls -lah /usr/lib/php7/vendor/mongodb/mongodb/
total 104
drwxr-xr-x 6 root root 4.0K Aug 16 18:03 .
drwxr-xr-x 3 root root 4.0K Aug 15 18:07 ..
-rw-r--r-- 1 root root 5.1K Sep 23 2016 BulkWriteResult.php
-rw-r--r-- 1 root root 5.3K Aug 16 18:03 Client.php
-rw-r--r-- 1 root root 27.1K Sep 23 2016 Collection.php
-rw-r--r-- 1 root root 9.6K Sep 23 2016 Database.php
-rw-r--r-- 1 root root 1.3K Sep 23 2016 DeleteResult.php
drwxr-sr-x 2 root root 4.0K Aug 15 18:07 Exception
-rw-r--r-- 1 root root 2.0K Sep 23 2016 InsertManyResult.php
-rw-r--r-- 1 root root 2.0K Sep 23 2016 InsertOneResult.php
drwxr-sr-x 2 root root 4.0K Aug 15 18:07 Model
drwxr-sr-x 2 root root 4.0K Aug 15 18:07 Operation
-rw-r--r-- 1 root root 3.3K Sep 23 2016 UpdateResult.php
-rw-r--r-- 1 root root 4.3K Sep 23 2016 functions.php
drwxr-sr-x 6 root root 4.0K Aug 16 17:41 tests
我现在正在尝试编写一个基本页面,以证明该库正在运行。
到目前为止,我有:
-
将库文件复制到我的测试项目的子文件夹中。所以我在我的测试网络应用程序文件夹中有这个:
webdev1:/var/www/localhost/htdocs/test# ls -lah total 16 drwxr-sr-x 3 root root 4.0K Aug 16 18:41 . drwxr-sr-x 4 root root 4.0K Aug 16 18:30 .. drwxr-xr-x 6 root root 4.0K Aug 16 18:44 mongodb -rw-r--r-- 1 root root 516 Aug 16 18:39 test.php webdev1:/var/www/localhost/htdocs/test# ls -lah mongodb/ total 104 drwxr-xr-x 6 root root 4.0K Aug 16 18:44 . drwxr-sr-x 3 root root 4.0K Aug 16 18:41 .. -rw-r--r-- 1 root root 5.1K Aug 16 18:32 BulkWriteResult.php -rw-r--r-- 1 root root 5.3K Aug 16 18:32 Client.php -rw-r--r-- 1 root root 27.1K Aug 16 18:32 Collection.php -rw-r--r-- 1 root root 9.6K Aug 16 18:32 Database.php -rw-r--r-- 1 root root 1.3K Aug 16 18:32 DeleteResult.php drwxr-sr-x 2 root root 4.0K Aug 16 18:32 Exception -rw-r--r-- 1 root root 2.0K Aug 16 18:32 InsertManyResult.php -rw-r--r-- 1 root root 2.0K Aug 16 18:32 InsertOneResult.php drwxr-sr-x 2 root root 4.0K Aug 16 18:32 Model drwxr-sr-x 2 root root 4.0K Aug 16 18:32 Operation -rw-r--r-- 1 root root 3.3K Aug 16 18:32 UpdateResult.php -rw-r--r-- 1 root root 4.3K Aug 16 18:32 functions.php drwxr-sr-x 6 root root 4.0K Aug 16 18:41 tests -
使用以下代码创建一个 test.php 页面:
"; 回声 extension_loaded("mongodb") ? "已加载\n" : "未加载\n"; echo "测试 2 - MongoDB 客户端库工作...
"; $client = new MongoDB\Client("mongodb://localhost:27017"); $collection = $client->jjtest->col1; $result = $collection->find(); var_dump($result); ?>
结果
当我运行 test.php 页面时,它显示我的驱动程序已加载... 但我得到了错误:
Test 1 - Mongodb Driver loaded?
loaded Test 2 - MongoDB Client Library working...
Fatal error: Uncaught Error: Class 'MongoDB\Database' not found in /var/www/localhost/htdocs/test/mongodb/Client.php:168 Stack trace: #0 /var/www/localhost/htdocs/test/mongodb/Client.php(93): MongoDB\Client->selectDatabase('jjtest') #1 /var/www/localhost/htdocs/test/test.php(9): MongoDB\Client->__get('jjtest') #2 {main} thrown in /var/www/localhost/htdocs/test/mongodb/Client.php on line 168
到目前为止我已经尝试过什么
为了证明 mongo 本身在工作,我启动了一个 CLI 并运行了这个命令:
> use jjtest
switched to db jjtest
> show collections
col1
> db.col1.find();
{ "_id" : ObjectId("5994980ba9e72bb4f6afed66"), "name" : "john", "age" : "99" }
>
我目前正在阅读命名空间和 autoloader.php (Why my autoload.php of composer doesn't work?),但到目前为止我没有尝试过在那里工作。通过安装 mongo-php7-library 自动创建的文件不包含我可以指向的 autoloader.php。
更新 1
我也尝试在我的 test.php 文件的顶部包含这个:
require_once __DIR__ . "/mongodb/";
但我收到以下错误:
警告:require_once(/var/www/localhost/htdocs/test/mongodb/): 失败 打开流:没有错误信息 /var/www/localhost/htdocs/test/test.php 在第 1 行
致命错误:require_once():需要打开失败 '/var/www/localhost/htdocs/test/mongodb/' (include_path='.:') 在 /var/www/localhost/htdocs/test/test.php 在第 1 行
【问题讨论】: