Store DB Connection Info in Windows Azure Web Sites App Settings

Store DB Connection Info in Windows Azure Web Sites App Settings

Rate This
  • Comments 2

This post is really just a Pro Tip: Use app settings in Windows Azure Web Sites to store your database connection information. A question on Stackoverflow the other day made me realize that using app settings in Windows Azure Web Sites to store database connection information was the key to migrating and/or upgrading databases easily. To test my idea, I decided to migrate a local WordPress website to a Windows Azure Web Site, then upgrade the database. Although I’m using WordPress, the idea can be used for any application. (I thought WordPress would be a good example since, like many PHP applications, database information is stored in a configuration file.) I won’t go into all the details, but will instead focus on how I used the app settings to make the migration and upgrade easy.

The first thing I did was to create a Windows Azure Web Site with a MySQL database and enable Git publishing. Then, on the CONFIGURE tab for my site in the Management Portal, I viewed my database connection string information and manually copied it into four app settings:

image

 

image

 

image

Note: Be sure to click Save at the bottom of the page after you enter app settings.

 

Now, to access the app settings, I needed to modify the wp-config.php file in my application. Instead of hard-coding database connection string information, I needed to use the getenv function to get this information from the app settings (which are accessible as environment variables):

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', getenv('DBNAME'));
 
/** MySQL database username */
define('DB_USER', getenv('DBUSER'));
 
/** MySQL database password */
define('DB_PASSWORD', getenv('DBPASSWORD'));
 
/** MySQL hostname */
define('DB_HOST', getenv('DBHOST'));

Next, I used Git to push my local code to my remote site, and I used mysqldump to copy my local database to my remote DB. When I browsed to my Windows Azure Web Site, Voila!, everything worked perfectly.

The real value in storing my database connection info in app settings came when I wanted to upgrade my database. With Windows Azure Web Sites, you get one 20MB MySQL database for free, provided by ClearDB. However, ClearDB has made it very easy to add more MySQL databases of varying sizes (some for a fee, but not all). By selecting the database size that met my needs, I was able to choose where it is deployed...

 

image

…and I had connection information in a matter of minutes:

image

Then, I again used mysqldump to copy my existing database to the new (bigger) DB, and I only had to update my app settings with the new connection information to begin using it.

Hopefully, this gives you some ideas about ways to use the app settings that are available in Windows Azure Web Sites. If you have comments on this post, or if you have other ideas about useful ways to use app settings, I’d love to hear them in the comments below.

Thanks.

-Brian

Leave a Comment
  • Please add 6 and 4 and type the answer here:
  • Post
  • Very interesant!, Congratulations!

  • Quite helpful as I suppose it would be possible to set the environment variables in the local dev environment so that this code would work both locally and in the cloud.  It would be great if one day azure could just override/transform the wp-config.php settings with values provided.

Page 1 of 1 (2 items)