Installing the PHP 7 MongoDB Client/Driver?
Asked Answered
S

12

35

I am very eager to start working with PHP 7 however one issue is getting in the way... I primarily use MongoDB for the database, and the problem is that I don't know how to install the MongoDB driver/client for PHP 7.

My current installation is PHP 5.6 and on my Mac and brew install php56-mongo does the trick.

Can anyone recommend how I can get this working on my Mac or an Ubuntu install?

Thanks in advance and much appreciated!

Sublingual answered 28/12, 2015 at 1:51 Comment(0)
J
46

The Mongo extension for PHP Version 5.99.99 or older has been superseded:

https://pecl.php.net/package/mongo

Use the newer one for PHP Version 7.99.99 or older instead:

https://pecl.php.net/package/mongodb

You can install a PECL/PEAR extension automatically:

pecl install mongodb

or manually.

The classes have been changed too:

new \MongoClient(); // legacy class!

see http://php.net/manual/en/book.mongo.php

new \MongoDB\Driver\Manager(); // new classes! 

see http://php.net/manual/en/set.mongodb.php

Additional information regarding compatibility can be found here:

https://docs.mongodb.org/ecosystem/drivers/php/#compatibility

Jab answered 29/12, 2015 at 9:38 Comment(6)
Really irks me they replaced MongoClient, forcing us to refactor our code. MongoClient worked great. Now it's been "improved". What a time suck.Lasagne
And it's not the first time they did it, too. :( Good news is that someone has built MongoClient interface: packagist.org/packages/alcaeus/mongo-php-adapter so you can use your old code without (major) changes.Fleck
Do you know of any decent tutorials for the newer classes?Couvade
@Sinfieldd best thing is to go through the docs on php.netJab
One of our main programs is destroyed because of it.Paxton
Agreed that github.com/alcaeus/mongo-php-adapter/tree/master/tests/Alcaeus/… is a great drop in replacement.Glyptodont
Q
16

The MongoDB driver that supports PHP 7 was only released December 22nd - its likely downstream repositories like brew haven't updated.

Update confirmed there is currently no php70-mongo brew script, though there is an active pull request to add one.

You may be able to install it manually via pecl in the meantime:

pecl channel-update pecl.php.net

pecl install mongodb

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
Quanta answered 28/12, 2015 at 2:4 Comment(3)
Thank you for responding... I created a new server via forge(I use laravel) and followed the instructions on the website... and even see mongodb when doing phpinfo()... however when trying to run it I get this error: Fatal error: Class 'MongoClient' not found Any ideas?Sublingual
Without seeing the code I cannot really comment. You might find this SO query helpful?Quanta
I have tried above steps on Ubuntu. For command "pecl install mongodb" end up with: In file included from /usr/include/php5/ext/spl/spl_iterators.h:27:0, from /usr/include/php5/ext/spl/spl_array.h:26, from /tmp/pear/temp/mongodb/src/bson.c:30: /usr/include/php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h: No such file or directory #include "pcre.h" Any idea?Voter
F
8

How to connect php 7.0 with MongoDB in ubuntu 16.04 lts?

1)Install LAMP using the following link. It installs Apache2, mysql and php 7.0. https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

2)Install the MongoDB community Edition on Ubuntu using the steps in the following link. http://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

3)Type the following command to get the mongoDB extension from pecl

sudo apt install php-pear

4)Add the following to the php.ini file at /etc/php/apache2/7.0

extension=mongodb.so

Important - The classes have been changed too:

new MongoClient();  //Old Class

new MongoDB\Driver\Manager(); // New Class

Refer - http://php.net/manual/en/set.mongodb.php

Forefather answered 8/8, 2016 at 16:8 Comment(0)
C
8

You can try install mongodb driver with:

sudo apt-get install php-mongodb
Colwen answered 26/6, 2017 at 16:36 Comment(0)
O
3

No, the legacy driver does not support PHP7, unfortunately. Here's the commit and the JIRA Ticket where this was officially finalized.

The new PHP MongoDB driver can be found in PECL here (or GitHub).

To install, just:

pecl channel-update pecl.php.net

pecl install mongodb

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

The documentation for the new driver can be found here. I'd like to include a note from the documentation:

Ultimately, this extension is not intended to be used alone. Users should considering using this driver alongside one or more userland PHP libraries, such as mongo-php-library.

The new mongodb driver / PHP extension is a lot more low-level than the legacy mongo driver, and you are encouraged to use a higher-level library on top of the driver rather than using it directly in your code.

The Mongo PHP Library (releases) is the official high-level library for PHP, and it's what is recommended to use in your projects. It's still in Beta, but this still seems to be the safest and most-future-proof path forward with PHP7.

It might be possible for someone to port the legacy driver to PHP7, but there probably isn't much of a need for it, as there are many other problems with the legacy driver.

Offshoot answered 4/1, 2016 at 2:59 Comment(1)
I have tried above steps on Ubuntu. For command "pecl install mongodb" end up with: In file included from /usr/include/php5/ext/spl/spl_iterators.h:27:0, from /usr/include/php5/ext/spl/spl_array.h:26, from /tmp/pear/temp/mongodb/src/bson.c:30: /usr/include/php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h: No such file or directory #include "pcre.h" Any idea?Voter
L
3
Laevorotation answered 23/2, 2016 at 14:14 Comment(0)
E
3

I almost gave up, too. For The MongoDB driver for PHP 7x, Ubuntu 18.04 Pecl will not work. Instead, try:

sudo apt-get install php-mongodb  

Then in the base of your project folder install the mongodb library https://docs.mongodb.com/php-library/current/tutorial/install-php-library/

composer require mongodb/mongodb
composer install

Which accesses the lower level functions provided by the driver.

Lastly, go to php.ini and add

extension = mongo.so

and restart apache

To test, try adding this to a php file:

<?php
     require_once __DIR__ . "/vendor/autoload.php";
     $collection = (new MongoDB\Client)->test->users;
     print_r($collection);
?>
Erotic answered 22/1, 2019 at 2:59 Comment(0)
D
2

Old question, but new excellent solution. Just use Mongostead7 automated script for installing all needed stuff. Worked for me just fine. No additional work needed.

Use it as follows:

sudo curl -sS https://raw.githubusercontent.com/zakhttp/Mongostead7/master/mongoHomestead7.sh | sudo sh
Derwin answered 6/9, 2016 at 12:48 Comment(0)
T
2

Complementing answers and publishing what worked for me:

1 followed this guide in order to install lamp https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04 (The third step is needed only for installing the mongo client)

2 $ sudo apt-get install php7.0-dev

3 $ sudo pecl install mongodb

4 $ sudo nano /etc/php/7.0/apache2/php.ini

Add the following line in the file:

extension = mongo.so;

(You might need to specify the exact location of the file. In my case the file was in /usr/lib/php/20151012/mongodb.so.)

And thats all for installing just the mongo client for php 7.0

I am complementing the Pransh Tiwari answer

Tsaritsyn answered 29/12, 2017 at 4:25 Comment(0)
V
1

This worked for me on Ubuntu for PHP7:

sudo apt-get install php7.0-mongodb
Voter answered 7/12, 2016 at 7:25 Comment(1)
Unable to locate package php7.0-mongodbCann
I
1

UBUNTU 16.0.4 (07.12.2016)

install PHP-MONGODB drivers : Commandes :
- sudo pecl install mongodb -> résultat : Build process completed successfully Installing '/usr/lib/php/20151012/mongodb.so' install ok: channel://pecl.php.net/mongodb-1.2.0 configuration option "php_ini" is not set to php.ini location You should add "extension=mongodb.so" to php.ini

                    -> la librairie se trouve dans "/usr/lib/php/20151012/mongodb.so"
            - sudo systemctl restart apache2.service
            https://secure.php.net/manual/en/mongodb.installation.pecl.php
            - create 2 new files called "30-mongodb.ini" in both path to add the extension to your server:
                -/etc/php/7.0/fpm/conf.d/30-mongodb.ini
                -/etc/php/7.0/cli/conf.d/30-mongodb.ini
                Commandes :
                    sudo nano /etc/php/7.0/fpm/conf.d/30-mongodb.ini

                        -> add "extension=mongodb.so"

                    sudo nano /etc/php/7.0/cli/conf.d/30-mongodb.ini

                        -> add "extension=mongodb.so"


            - Test if the mongodb extension is running in your server :
                Commandes :
                    php --ini

install DoctrineMongoDBBundle : http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html, http://www.doctrine-project.org/2016/06/09/odm-1-1-0-and-1-0-6.html,

    Commandes :
        - cd
        - cd my_project_name
        - ls -a composer*
            -> résultat : composer.json  composer.lock
        - sudo nano composer.json
        - Add to the composer.json in "require" array
            "doctrine/mongodb-odm": "^1.0",
            "doctrine/mongodb-odm-bundle": "^3.0"
        - Add to the composer.json in "require" array
            "alcaeus/mongo-php-adapter": "^1.0",
            "ext-mongo": "*"
        - Add a new array :
            "provide": 
            {
                "ext-mongo": "1.6.12"
            }
        - Move 
        sudo cp -i /usr/lib/php/20151012/mongodb.so /etc/php/7.0/cli

To give the solution I need at least 10 reputation to post...

Immediate answered 7/12, 2016 at 14:9 Comment(1)
Full solution to install Ubuntu, LAMP, Mongodb, PHP, Composer, Symfony, MongodbBundle : openclassrooms.com/forum/sujet/…Immediate
C
1

I am using php version 7.0 on ubuntu 16.04. I am giving a detailed info for installing the mongo driver/client. First I manually installed mongodb and then the mongodb-php driver for it.

1) Installing mongo db. Enter the following commands:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

$ sudo apt-get update

$ sudo apt-get install -y mongodb-org

In order to properly launch Mongdb as a service, ie automatically starting Mongodb when the system starts, follow the following steps:

Create file mongodb.service in /etc/systemd/system/ by entering the command:

$ sudo nano /etc/systemd/system/mongodb.service

Paste the following contents in it:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Then enter the following commands:

$ sudo systemctl start mongodb

$ sudo systemctl enable mongodb

2) Installing the mongo-php driver:

$ sudo pecl install mongodb

Also you might receive error: phpize not found. Phpize is a command which is used to create a build environment. This error could appear at the time of installation of any pecl extension. To solve this problem of the phpize command not found, the user has to install the php5-dev package. To install it enter the command:

 $ sudo apt-get install php7.0-dev

Then in the php.ini file which is in /etc/php/7.0/apache2 directory, add the mongo db extension:

$ sudo nano /etc/php/7.0/apache2/php.ini 

Add the following line in the file:

extension = mongo.so;

(You might need to specify the exact location of the file. In my case the file was in /usr/lib/php/20151012/mongodb.so.)

So the mongo db is installed along with its driver.

3) Now keep in mind that the mongo-php classes have been changed. Most of the available resources in the net give solutions using old classes which is superseded. Below are the links which you can refer to:

http://php.net/manual/en/set.mongodb.php

http://zetcode.com/db/mongodbphp/

Here are some commands for basic database operations:

$mng = new MongoDB\Driver\Manager(); // Driver Object created

To insert data into the database:

$bulk = new MongoDB\Driver\BulkWrite;

$doc = ["_id" => new MongoDB\BSON\ObjectID, "data" => $someData, "info" => $someInfo];

$bulk->insert($doc);

$mng->executeBulkWrite('dbName.collectionName', $bulk);

For fetching data:

$query = new MongoDB\Driver\Query([]); 

$rows = $mng->executeQuery("dbName.collectionName", $query);

foreach ($rows as $row) 
    {
         echo "$row->data - $row->info\n";
    }
Cumings answered 4/4, 2017 at 17:18 Comment(2)
sudo systemctl start mongodb, systemctl is not found on ubuntu/trusty64Pinprick
Try using the systemctl equivalent @Notflip. HereCumings

© 2022 - 2024 — McMap. All rights reserved.