How to install Mongodb PHP extension in Ubuntu 16.04 LTS
To install the mongodb extension, do:
sudo apt-get install php-mongodb
I am sure you found a solution by now. However I want to share how I did it:
- Installed php 5.6 on Ubuntu 16 (@see http://lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu)
- then ran
sudo apt-get install php5.6-mongo
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";
}