在 Ubuntu 13.10 (saucy) 中安装 Mongodb PHP 扩展的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22459164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:56:53  来源:igfitidea点击:

Easiest way to install Mongodb PHP extension in Ubuntu 13.10 (saucy)?

phpmongodbubuntu-13.10

提问by Tom

I'm using Ubuntu 13.10 (saucy salamander) and I was hoping that sudo apt-get install php5-mongowould be enough to get the mongodb database driver installed.

我正在使用 Ubuntu 13.10(saucy salamander),我希望这sudo apt-get install php5-mongo足以安装 mongodb 数据库驱动程序。

No such luck though. I'm using php5-fpm, so firstly I found I also needed to do php5enmod mongobut even with that, I get a error:

然而没有这样的运气。我正在使用 php5-fpm,所以首先我发现我也需要这样做,php5enmod mongo但即便如此,我还是收到了一个错误:

include(MongoClient.php): failed to open stream: No such file or directory

Instead, to install I have to do the following (which uses a lot more disk space):

相反,要安装我必须执行以下操作(使用更多磁盘空间):

 sudo apt-get install php5-dev make php-pear
 sudo pecl install mongo
 sudo echo "extension=mongo.so" | tee /etc/php5/mods-available/mongo.ini

My question is why isn't sudo apt-get install php5-mongoenough? Is it a problem with the Ubuntu repo? How can I look into the ubuntu repositories and find what version it uses or why MongoClient.phpisn't included. I'm using the "ubuntu:saucy" docker image as my base and it includes universe by default I think.

我的问题是为什么还 sudo apt-get install php5-mongo不够?Ubuntu 存储库有问题吗?我如何查看 ubuntu 存储库并找到它使用的版本或为什么MongoClient.php不包括在内。我使用“ ubuntu:saucy” docker 镜像作为我的基础,我认为它默认包含 Universe。

回答by Tom

In Ubuntu 14.04, sudo apt-get install php5-mongoresults in a fully working mongo PHP extension. So I guess there is just a packaging problem in Ubuntu 13.10 which causes the problem.

在 Ubuntu 14.04 中,会sudo apt-get install php5-mongo产生一个完全可用的 mongo PHP 扩展。所以我猜只是 Ubuntu 13.10 中的一个打包问题导致了这个问题。

Solution: Use Ubuntu 14.04 LTS instead of Ubuntu 13.10.

解决方案:使用 Ubuntu 14.04 LTS 而不是 Ubuntu 13.10。

But, if you haveto use Ubuntu 13.10, don't use the php5-mongoUbuntu package, instead install the mongo extension via pecl:

但是,如果您必须使用 Ubuntu 13.10,请不要使用php5-mongoUbuntu 包,而是通过 pecl 安装 mongo 扩展:

sudo apt-get install php5-dev make php-pear
sudo pecl install mongo
sudo echo "extension=mongo.so" | sudo tee /etc/php5/mods-available/mongo.ini

回答by Yassine Khachlek

The easiest to install the mongoDB driver for php5 in ubuntu is using the command :

在 ubuntu 中为 php5 安装 mongoDB 驱动程序最简单的方法是使用以下命令:

sudo apt-get install php5-mongo

Attention, the driver is correctly installed but not loaded yet, so should absolutly restart the server, if using apache should do :

注意,驱动已正确安装但尚未加载,所以应该绝对重启服务器,如果使用apache应该这样做:

sudo service apache2 restart

回答by Hafiz Shehbaz Ali

There is also a good way to install the mongo DB for PHP. I have ubuntu 14.04LTS.

还有一种为 PHP 安装 mongo DB 的好方法。我有 ubuntu 14.04LTS。

open synaptic package manager and search "mongo"

select these packages to install.

选择这些软件包进行安装。

  • php-horde-mongo
  • php-mongo
  • php-horde-mongo
  • php-mongo

synaptic package manager

突触包管理器

click apply to install the packages.

单击应用以安装软件包。

Now restart you apache2 using this command.

现在使用此命令重新启动 apache2。

sudo service apache2 restart

Test the mongo

测试 mongo

create the PHP file with name mongo-test.php and paste this code there.

创建名为 mongo-test.php 的 PHP 文件并将此代码粘贴到那里。

<?php
   // connect to mongodb
   $m = new MongoClient();

   echo "Connection to database successfully";
   // select a database
   $db = $m->mydb;

   echo "Database mydb selected";
?>

test this URL http://localhost/mongo-test.php

测试这个 URL http://localhost/mongo-test.php

回答by bjori

This seems to be a packaging problem in Ubuntu.

这似乎是 Ubuntu 中的打包问题。

Did you install PHP5-fpm via apt-gettoo? It looks like there is a disconnect between how you installed PHP5 and which php.ini file the php5-mongo package is trying to update.

您是否也通过安装 PHP5-fpm apt-get?看起来您安装 PHP5 的方式与 php5-mongo 包试图更新的 php.ini 文件之间存在脱节。

As for the include(MongoClient.php): failed to open streamerror; it is because of your autoloader. When you do new MongoClientwhen the extension is not enabled it hits your autoloaded which tries to include a file called <classname>.php

至于include(MongoClient.php): failed to open stream错误;这是因为你的自动加载器。当您new MongoClient在未启用扩展程序时执行此操作时,它会命中您的自动加载,它试图包含一个名为<classname>.php