This guide is geared towards web developers using wamp. As you know, wamp keeps your filesystem tidy by installing apache, mysql, and php all inside c:\wamp. My setup is a Windows 7 64bit machine running wamp 32bit (Why run wamp 32bit on Windows 7 64bit?). Although untested on each, this guide should also work for Windows XP, Vista, and Windows Server variants.
MongoDB is a open source, high performance,NOSQL database server(mongod.exe) and shell(mongo.exe) developed by 10gen. MongoDB win32 binaries are available in both 64bit and 32bit flavors. The win32 binaries are packaged as a very simple zip file containing only the binary executables, no installer, and no example config files.
Lets get started! We will install mongodb into the c:\wamp directory, create conf,data,log directories, and configure mongod to run as a windows service.
1. Download the latest win32 build of mongodb, choosing your architecture (32bit or 64bit) from http://www.mongodb.org/downloads.
2. Create the directory c:\wamp\bin\mongodb\ and extract the contents of the zip archive here. Your directory structure should look something like the following depending on the version number and architecture you download.
C:\wamp\bin\mongodb\mongodb-win32-x86_64-2.0.2\
3. Create the following directories
mkdir c:\wamp\bin\mongodb\mongodb-win32...2.x.x\data mkdir c:\wamp\bin\mongodb\mongodb-win32...2.x.x\data\db mkdir c:\wamp\bin\mongodb\mongodb-win32...2.x.x\logs mkdir c:\wamp\bin\mongodb\mongodb-win32...2.x.x\conf
4. Create the file c:\wamp\bin\mongodb\mongodb-win32…2.x.x\conf\mongodb.conf and add the following base configuration.
# mongodb.conf # data lives here dbpath=C:\wamp\bin\mongodb\mongodb-win32...2.x.x\data\db # where to log logpath=C:\wamp\bin\mongodb\mongodb-win32...2.x.x\logs\mongodb.log logappend=true # only run on localhost for development bind_ip = 127.0.0.1 port = 27017 rest = true
5. Execute the windows console(command line) as administrator and change to the c:\wamp\bin\mongodb\mongodb-win32…2.x.x\bin directory.
6. Execute the following command to install mongod as a windows service.
mongod.exe --install --config c:\wamp\bin\mongodb\mongodb-win32...2.x.x\conf\mongodb.conf --logpath c:\wamp\bin\mongodb\mongodb-win32...2.x.x\logs\mongodb.log
7. Execute services.msc and scroll down to find the Mongo DB service. Right click on the service and choose start. From here you can choose the have the service started automatically on boot or change it to manual and you will need to start the service each time.
8. We can confirm that mongod is running by inspecting the log file located at c:\wamp\mongodb\mongodb-win32…2.x.x\logs\mongodb.log
Note: Each time you make changes to mongodb.conf, you will need to restart the Mongo DB service.
Adding mongo.exe to your path
mongo.exe is the MongoDB shell, it will be very convenient to add the bin directory to your PATH environment variable so that you can simple type mongo at the command line regardless of your current working directory.
1. Right Click on My Computer
2. Choose Properties and Advanced System Settings
3. Click the Environment Variables button
4. Under system variables scroll down and double click on Path.
5. Append the following to the existing Variable value.
;C:\wamp\bin\mongodb\mongodb-win32-x86_64-2.0.2\bin
Note: The ; (semicolon) at the beginning is the delimiter between each path. While you are here, it’s a good idea to also add the path to php.exe.
;C:\wamp\bin\php\php5.3.9
Remember, your version numbers will probably vary slightly so double check the correct path for your machine and revision of wamp.
Use mongo.exe to confirm everything is up and running
Start | Run | cmd.exec:\> mongo MongoDB shell version: 2.0.2 connecting to: test > > use test; switched to db test > db.test.insert( {"hello":"world"} ); > db.test.find(); { "_id" : ObjectId("4f33df871c81e6d645a53dd3"), "hello" : "world" } > exit;
Installing the Mongo PHP Extensions (php_mongo.dll)
1. Download the latest version of the win32 php extension from https://github.com/mongodb/mongo-php-driver/downloads. For this example I used mongo-1.2.5.zip
2. Extract the zip archive find the mongo-1.2.5-php5.3vc9ts/ directory. The ts in the folder name means thread safe.
3. Copy c:\wamp\bin\php\php-5.3.x\ext\
4. Edit c:\wamp\bin\apache\Apache2.2.xx\bin\php.ini and add the following line near the other loaded extensions.
extension=php_mongo.dll
5. Restart all the wamp services by clicking on the wamp task tray icon and choosing ‘Restart All Services’.
6. Open your browser to http://127.0.0.1/?phpinfo=1 to confirm that the mongo driver is loaded.
Additional Tips for running MongoDB on Windows
Stop and Start the mongod service from the command line
Note: You must be running the command prompt shell with administrative privileges for the following commands to work.
Stop mongod
c:\> NET STOP "Mongo DB"
Start mongod
c:\> NET START "Mongo DB"
Backup your mongodb data files
1. Stop the “Mongo DB” service.
2. Copy c:\wamp\bin\mongodb\mongodb-win32…2.x.x\data to your backup destination.
3. Restart the “Mongo DB” service.
Why run wamp 32bit on Windows 7 64bit?
The Mongo PHP driver does not have a 64bit win32 binary, if you are running the 64bit version of wamp, php will refuse to load the 32 bit version of php_mongo.dll. While wamp and the php driver are 32bit, it’s still recommended to install the 64bit version of mongodb server if you plan on working with collections over 2GB.
Links
- MongoDB Downloads – http://www.mongodb.org/downloads
- Win32 Mongo PHP Drivers – https://github.com/mongodb/mongo-php-driver/downloads
- PHP MongDB Documentation – http://php.net/manual/en/book.mongo.php
- 10gen – http://www.10gen.com/
- WampServer – http://www.wampserver.com/
Keywords:
- wamp mongodb
- mongodb wamp
- wamp mongo
- installing phpdriver for mongodb for wamp on windows xp
- install mongodb wamp
- wampserver mongodb
- mongo wamp
- how to install mongodb on windows 7
- php_mongo dll 64 bit
- install mongodb on windows 7
June 18th, 2012 on 8:51 am 4023
Hey there,
Great tutorial, I have been trying to get Mongo set up for a while and your very detailed tutorial helped so much.
One problem that I am getting is trying to get the PHP driver working. Whenever I try to initiate a connection like $m = new Mongo(); it just gives me the error Class ‘Mongo’ not found. Now I noticed you said to have to 32bit Wamp installed on a 64bit OS, is this the reason I am getting this error?
Any help would be very much appreciated!
June 20th, 2012 on 12:58 pm 4028
Thanks a lot man. Excellent tut. Saved a lot of time.
June 26th, 2012 on 3:50 pm 4032
I’ve been having this problem as well. 32bit WAMP. Configured all the files, Mongo running, the dll is there, but gives error alert on WAMP startup.
June 28th, 2012 on 1:24 am 4034
Hey thanks for the article.