Installing the PHP 7 MongoDB Client/Driver?

后端 未结 12 934
日久生厌
日久生厌 2020-11-28 06:17

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

相关标签:
12条回答
  • 2020-11-28 06:54

    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
    
    0 讨论(0)
  • 2020-11-28 06:54

    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

    0 讨论(0)
  • 2020-11-28 06:54

    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";
        }
    
    0 讨论(0)
  • 2020-11-28 06:56

    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

    0 讨论(0)
  • 2020-11-28 06:58

    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.

    0 讨论(0)
  • 2020-11-28 06:59

    This worked for me on Ubuntu for PHP7:

    sudo apt-get install php7.0-mongodb
    
    0 讨论(0)
提交回复
热议问题