Mongodb on windows and XAMPP

08/07/2013


Install XAMPP(link to tutorial)


Go to http://www.mongodb.org/downloads and download latest(not Nightly) mongodb version for windows 32bit or 64bit depending on your system. 32-bit versions of MongoDB are suitable only for testing and evaluation purposes and only support databases smaller than 2GB, but since this is for testing on localhost I will use 32bit version. Extract file on C:\ (you can choose other drive). Change extracted file name from mongodb-win32-i386-2.4.5 to mongodb, so now you have:



C:\mongodb



MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is C:\data\db. Create this folder. If you need to use other forlder use following commands:



C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data



Next step is downloading php driver for mongodb. Go to https://s3.amazonaws.com/drivers.mongodb.org/php/index.html and download latest stable version - version with highest number without beta and rc in name. I downloaded php_mongo-1.4.1.zip which is the newest at the moment of writing this tutorial. There are more then one driver in zip file, so you have to choose right one. This is content of zip file for 1.4.1 version.


 mongodb php driver installation


Now you need to start XAMPP to determine which version to use. Type in your browser address http://localhost/xampp/ , choose phpinfo() and you'll get page with information about your PHP settings. We need PHP version, PHP Extension Build,  Architecture.


 mongodb php driver version determing


In this case php version is 5.4.7 so the right driver name is starting with php_mongo_1.4.1-5.4.


PHP Extension build is VC9 so the driver must be vc9(vc=Visual C++)Tags


PHP Extension build is TS so you can use driver without ntl in name. For more info search google for "php ts vs nts". TS mean Thread Safe and NTS Non Thread Safe.


Architecture is x86 so the driver must be without x86_64 in name. If you installed default XAMPP installation it is serenely x86 even if windows is 64bit.


Right driver for this php configuration is  php_mongo-1.4.1-5.4-vc9.dll. Copy this driver to C:\xampp\php\ext\


Open C:\xampp\php\php.ini file, insert next line at the end of file and save. Don't forgot to change part after "=" sign to your version of driver.



extension=php_mongo-1.4.1-5.4-vc9.dll



Now open XAMPP Control Panel and start Apache. Go to phpinfo page  http://localhost/xampp/phpinfo.php and there should be mongo section.


Mongo section in phpinfo


As you can see default port for mongodb is 27017.


Start mongodb in Command Promt with



C:\mongodb\bin\mongod.exe



You can now access mongodb by opening other command prompt window and insert command



C:\mongodb\bin\mongo.exe



Note it is not the same as previous command, in first case mongod.exe in second mongo.exe.


Exit mongodb with quit() or ctrl-c shortcut.


"Unclean shutdown detected" is most common problem with mongodb so here is solution. When you forgot to shutdown mongodb before turning off computer, mongodb will not start. Error message is  Unclean shutdown detected. 


Delete file C:\data\db\mongod.lock. file. If your data folderi is on other location, change path.


Run in console



C:\mongodb\bin\mongod.exe --repair



Problem should be fixed now.