Search code examples
phpmongodblaravelnginxphp-mongodb

Can not connect to MongoDB from PHP


I have installed MongoDB PHP lib

phpinfo screenshot

but despite this I can't connect to MongoDB database

Error of rockmonogo :

To make things right, you must install php_mongo module. Here for installation documents on PHP.net.

Error of phpmoadmin :

PHP cannot access MongoDB, you need to install the Mongo extension for PHP.

Error of Laravel with Jenssegers\Mongodb lib :

Unsupported driver [mongodb]

Solution

  • Make sure that it is in fact properly installed. The errors you're getting seem to indicate that your environment can't find the extension. I've compiled installation instructions for you.

    Unix-like Systems

    1. Open a terminal emulator and run wget http://pear.php.net/go-pear.phar
    2. Then start the installation of PECL with php go-pear.phar
    3. Install the PHP MongoDB extension sudo pecl install mongodb.
    4. Add extension=mongodb.so to the php.ini file.

    Mac OS X

    1. Install homebrew by running /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" in a terminal.
    2. Run brew update
    3. Run brew tap homebrew/php
    4. Install the extension according to your PHP version.
      • PHP 5.4: brew install php54-mongodb
      • PHP 5.5: brew install php55-mongodb
      • PHP 5.6: brew install php56-mongodb
      • PHP 7.0: brew install php70-mongodb

    Windows

    1. Open your web browser to http://pecl.php.net/package/mongodb
    2. Open the latest version from the DLL link (with the Windows icon).
    3. Choose the appropriate version from the DLL list. They are sorted by PHP version, system architecture (i.e. 32-bit vs 64-bit), and thread safety.
    4. Extract the downloaded archive and place php_mongodb.dll in your PHP extensions directory.
    5. Add extension=php_mongodb.dll to the php.ini file.