Create and connect to a MySQL database in Azure
You can also create a MySQL database as part of a Marketplace app template.
Create a MySQL database in Azure portal
To create a MySQL database in the Azure portal, do the following:
- Log in to the Azure portal.
From the left menu, click New > Data + Storage > MySQL Database.
In the New MySQL Database blade, configure your new MySQL database as follows (blade: a portal page that opens horizontally):
- Database Name: Type a uniquely identifiable name
- Subscription: Choose the subscription to use
- Database Type: Select Shared for low-cost or free tiers, or Dedicated to get dedicated resources.
- Resource group: Add the MySQL database to an existing resource group or put it in a new one. Resources in the same group can be easily managed together.
- Location: Select a location close to you. When adding to an existing resource group, you're locked to the resource group's location.
- Pricing Tier: Click Pricing Tier, then select a pricing option (Mercury tier is free), and then click Select.
- Legal Terms: Click Legal Terms, review the purchase details, and click Purchase.
Pin to dashboard: Select if you want to access it directly from the dashboard. This is especially helpful if you aren't familiar with portal navigation yet.
The following screenshot is just an example of how you can configure your MySQL database.
When you're done configuring, click Create.
You will see a pop-up notification letting you know that deployment has started.
You will get another pop-up once deployment has succeeded. The portal will also open your MySQL database blade automatically.
Connect to your MySQL database
To see the connection information for your new MySQL database, just click Properties in your web app's blade.
You can now use that connection information in any web app. A sample that shows how to use the connection information from a simple PHP app is available here.
Connect a Laravel web app (from the PHP get started tutorial)
Suppose you just finished the tutorial Create, configure, and deploy a PHP web app to Azure and have a Laravel web app running in Azure. You can easily add database capabilities to your Laravel app. Just follow the steps below:
The following steps assume that you have finished the tutorial Create, configure, and deploy a PHP web app to Azure.
Configure the Laravel app in your local development environment to point to the MySQL database. To do this, open
.envfrom your Laravel app's root directory and configure the MySQL database options.
DB_CONNECTION=mysql DB_HOST=<HOSTNAME_from_properties_blade> DB_PORT=<PORT_from_properties_blade> DB_DATABASE=<see_note_below> DB_USERNAME=<USERNAME_from_properties_blade> DB_PASSWORD=<PASSWORD_from_properties_blade>
In the Properties blade, the name of your MySQL database may or may not be the one shown in the DATABASE NAME field. It's better to check the Database parameter in the CONNECTION STRING field.
The quickest way to verify that you have MySQL access now is to use Laravel's default authentication scaffolding. In the command-line terminal, run the following commands from your Laravel app's root directory:
php artisan migrate php artisan make:auth
The first command creates the tables in Azure based on predefined migrations in the
database/migrationsdirectory, and the second command scaffolds the basic views and routes for user registration and authentication.
Run the development server now:
php artisan serve
In the browser, navigate to http://localhost:8000 and register a new user as shown:
Follow the UI prompt complete the registration. Once registration completes, you will be logged in.
You are now developing your app against the MySQL database in Azure.
Now, you just need to replicate your
.envsettings to your Azure web app. Run the following Azure CLI commands:
azure site appsetting add DB_CONNECTION=mysql azure site appsetting add DB_HOST=<HOSTNAME_from_properties_blade> azure site appsetting add DB_PORT=<PORT_from_properties_blade> azure site appsetting add DB_DATABASE=<Database_param_from_CONNECTION_INFO_from_properties_blade> azure site appsetting add DB_USERNAME=<USERNAME_from_properties_blade> azure site appsetting add DB_PASSWORD=<PASSWORD_from_properties_blade>
Find out how this works in Configure the Azure web app.
Next, commit and push to Azure the local changes made earlier while running
php artisan make:auth.
git add . git commit -m "scaffold auth views and routes" git push azure master
Browse to the Azure web app.
azure site browse
Log in using the user credentials you created earlier.
After you log in, you should see the friendly post-login screen.
Congratulations, your PHP web app in Azure is now accessing data from your MySQL database.
For more information, see the PHP Developer Center.