Skip to content

Latest commit

 

History

History
151 lines (113 loc) · 7.83 KB

store-php-create-mysql-database.md

File metadata and controls

151 lines (113 loc) · 7.83 KB
title description documentationcenter services author manager editor tags ms.assetid ms.service ms.workload ms.tgt_pltfrm ms.devlang ms.topic ms.date ms.author
Create and connect to a MySQL database in Azure
Learn how to use the Azure portal to create a MySQL database and then connect to it from a PHP web app in Azure.
php
app-service\web
cephalin
erikre
mysql
55465a9a-7e65-4fd9-8a65-dd83ee41f3e5
multiple
data-management
na
PHP
article
11/01/2016
robmcm;cephalin

Create and connect to a MySQL database in Azure

This tutorial shows you how to create a MySQL database in the Azure portal (provider is ClearDB) and how to connect to it from a PHP web app running in Azure App Service.

Note

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:

  1. Log in to the Azure portal.

  2. From the left menu, click New > Data + Storage > MySQL Database.

    Create a MySQL database in Azure - start

  3. 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.
      Create a MySQL database in Azure - configure

  4. When you're done configuring, click Create.

    Create a MySQL database in Azure - create

    You will see a pop-up notification letting you know that deployment has started.

    Create a MySQL database in Azure - in progress

    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.

Create a MySQL database in Azure - MySQL database 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:

Note

The following steps assume that you have finished the tutorial Create, configure, and deploy a PHP web app to Azure.

  1. Configure the Laravel app in your local development environment to point to the MySQL database. To do this, open .env from 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>
    

    [!NOTE] 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.

    Create a MySQL database in Azure - in progress

  2. 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/migrations directory, and the second command scaffolds the basic views and routes for user registration and authentication.

  3. Run the development server now:

     php artisan serve
    
  4. In the browser, navigate to http://localhost:8000 and register a new user as shown:

    Connect to MySQL database in Azure - register user

    Follow the UI prompt complete the registration. Once registration completes, you will be logged in.

    Connect to MySQL database in Azure - register user

    You are now developing your app against the MySQL database in Azure.

  5. Now, you just need to replicate your .env settings 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.

  6. 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
    
  7. Browse to the Azure web app.

     azure site browse
    
  8. Log in using the user credentials you created earlier.

    Connect to MySQL database in Azure - browse to Azure web app

    After you log in, you should see the friendly post-login screen.

    Connect to MySQL database in Azure - logged in

    Congratulations, your PHP web app in Azure is now accessing data from your MySQL database.

Next steps

For more information, see the PHP Developer Center.