Earlier today, MongoLab announced the availability of MongoDB as a service on Windows Azure: MongoLab on Windows Azure. From my point of view this is great as it now makes it easy to use 3 of my favorite technologies in concert: Windows Azure Web Sites, PHP, and MongoDB. So, I’m wasting no time in playing with them: this post will show you how to create a MongoDB database and access it from PHP in Windows Azure Web Sites.
Note: I strongly encourage you to read MongoLab’s announcement before following this tutorial as it contains information about the beta status of MongoLab’s offering.
I’m assuming you have a Windows Azure account. If you don’t, you can sign up for the free trial.
Create a MongoDB database
To create a MongoDB database…
1. Log in to the Windows Azure portal, click +NEW at the bottom of the page, then select STORE.
2. Scroll through the available add-ons, select MongoLab, and click the arrow at the bottom of the screen.
3. Provide the MongoDB database name and select the region for deployment. Click the right arrow at the bottom of the screen.
Note: The name you provide here will be the name of your database, and in the free offering (which is all that is available now), you only get one database.
4. Confirm your “purchase” (it’s free!).
5. After your database has been created, you will be able to see it in the portal under ADD-ONS. Click on the name of the database to go to its dashboard.
6. Explore the links in the dashboard, but to get connection information, go to the next step.
7. Click CONNECTION INFO to get your MongoDB connection string. Make note of the connection string as you will need it later. (Also note that by clicking MANAGE, you will be taken to MongoLab’s web UI for managing your database.)
That’s it. Your MongoDB database is now ready to use.
Create a Web Site and enable php_mongo.dll
Once you have a MongoDB database set up, you can connect to it from anywhere by using the connection string you noted in the section above. If you want to connect to it from a site in Windows Azure Web Sites, however, you’ll need to a little bit of extra work to enable the MongoDB extension:
- Download the php_mongo.dll extension here: https://github.com/mongodb/mongo-php-driver/downloads. Choose the download for PHP 5.3, Windows, VC9. After you have downloaded the archive, select the appropriate non-thread-safe version from the available files (e.g. php_mongo-1.2.12-5.3-vc9-nts.dll).
- Follow instructions in this post for enabling the MongoDB extension in Windows Azure Web Sites: Using Custom PHP Extensions in Windows Azure Web Sites. (Note: That post assumes you have renamed the extension file to php_mongo.dll.)
If you are new to Windows Azure Web Sites, you’ll need to follow the steps in one of these articles for creating a website before enabling the MongoDB extension:
Create and publish your application
As I mentioned earlier, I’m assuming you have some familiarity with MongoDB and PHP. If you don’t, here’s a good place to get started: http://php.net/manual/en/book.mongo.php. Just for fun, here’s a simple example application (it registers people for an imaginary event) that uses a MongoDB database. You can run it locally or publish to Windows Azure Web Sites. You will need to replace YOUR_CONNECTION_STRING_HERE with the connection string you noted when you created your database, and you’ll need to replace YOUR_DB_NAME_HERE with the name of your database.
Note: Instead of hard-wiring the connection string into the code above, you could add it as a Custom string to the connection string section on the CONFIGURE tab of your website’s dashboard. You would then have to modify the code above to retrieve the connection string as an environment variable. More information on how to do that here: Getting Database Connection Information in Windows Azure Web Sites.
To publish your application with Git, follow the instructions here: Publishing with Git.
That’s it. Have fun and let us know what you think.