Cheth.com

Contact | About | Home

*

PHP MongoDB Driver

Installing MongoDB for PHP on a Windows development machine is a great first step toward understanding and developing with this popular NoSQL database. As with any new technology there is a learning curve. One cureveball you might encounter—without realizing it—is libsasl.dll.

Simple Authentication Security Layer is Oracle's name for this module. It provides developers of applications and shared libraries with mechanisms for authentication, data integrity-checking, and encryption. The encryption part is unintentionally assisted by the cryptic and quite misleading error message displayed when this file is missing:

PHP Error Log
PHP Warning:  PHP Startup: Unable to load dynamic library 'C:\Program Files (x86)\php5.6\ext\php_mongo.dll' - %1 is not a valid Win32 application.

Upon encountering not a valid Win32 application one's first reaction is likely to be a search for a better MongoDB driver. In spite of what it says, this error message does not always mean you've selected the wrong driver; it may mean you've selected the wrong PHP extension.

MONGO <> MONGODB

Confusingly, there are dozens of MongoDB drivers. If you download the drivers from PECL (and you should) you'll find the correct versions are named php_mongodb.dll. If instead you somehow get a file named php_mongo.dll (without the "db") you may be disappointed. I know I was.

In my search for an acceptable DLL I tried several PHP5.6 drivers from what I eventually discovered was an outdated AWS resource. One that initially seemed approriate was named php_mongo-1.6.6-5.6-vc11.dll. Note again that the basic name was "mongo," not "mongodb."

The mongo drivers are deprecated. But the never-forgetful web retains enough documentation about them that it is easy to get misled (my rationalization).

Once you get the correct mongodb driver, copy it into your php extension folder. Here's what mine looks like:

mongodb driver location
C:\Program Files (x86)\php5.6\ext\php_mongodb.dll   (467KB)

You also need to enable this extension in your php.ini file. The example below shows both the deprecated and correct entries; only use the mongodb line. The deprecated line is shown merely as a self-reminder of the pain.

php.ini
;extension=php_mongo.dll   ; deprecated MongoDB driver
extension=php_mongodb.dll  ; correct MongoDB driver

The libsasl DLL remains critically important. MongoDB will not initialize without it. Although its handling is improved with the modern mongodb driver, it's still not perfect.

To use the old, deprecated mongo driver it was necessary to manually copy libsasl.dll to the Apache bin folder. Thankfully the new mongodb driver allows libsasl.dll to remain in its as-installed PHP folder. But, if it is removed (as I perversely tried), PHP once again emits its unhelpful log complaint that the mongodb driver "is not a valid win32 application." The moral? Logs are not always truth.