_posts/2016-8-18-Announcing MySQL in-app for Web Apps (Windows).html [3:188]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - hide_excerpt: true ---
mksunitha
MySQL in-app feature enables running MySql natively on Azure App Service platform.You don’t need to provision a database explicitly as during the creation of the web app when using this feature,  we take care of enabling it if you select “MySQL in-app ” during creation or if the feature is turned ON for existing web app. To understand what MySQL in-app means , I have highlighted core functionality supported with the preview release of the feature:   Note : This MySQL in-app is specific to Windows version of Azure app service. If you are looking for local mysql on Linux MySQL app service , click here.

Create new Web App with MySQL in-app

Login to Azure portal  and launch Web App + MySQL template by clicking here .Enter the Site name and select MySQL in-app as the database provider. Click on Create to deploy a web app using MySQL in-app. webappmysqlcreate You may also create a WordPress application with MySQL in-app from Azure marketplace. Test drive our demo site here using MySQL in-app.You can login with username : demo and password: demopassword . 

Limitations

For preview release the feature has some limitations that to keep in mind.

Manage MySQL in-app

Go to your web app and select MySQL in-app in the Menu blade on the right. You can use the setting here to manage your MySQL in-app feature , turn on logging , access PHPmyadmin etc. localmysql8  

Access database content

The database is protected by our sandbox environment and hence cannot be accessed remotely through MySQL workbench or MySQL command line tools (running on remote machine) . There are two ways you can manage your database content : localmysql3 (2) The database enabled with your web app is "localdb". You are now ready to import your database schema or create a new one for your web app. localmysql5
D:\Program Files (x86)\mysql\5.7.9.0\bin\mysql.exe -e "ENTER SQL STATEMENTS" --user=azure --password=password --port=49175 --bind-address=127.0.0.1  
Example:
D:\Program Files (x86)\mysql\5.7.9.0\bin\mysql.exe -e "USE localdb;Select * from tasks;" --user=azure --password=password --port=49175 --bind-address=127.0.0.1  
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: Unknown OS character set 'cp0'.
mysql: Switching to the default character set 'latin1'.
task_id    subject    start_date    end_date    description
1    task1    2016-02-18    2016-02-19     sample task
The mysql warning statements can be ignored in the output. You can use mysqladmin.exe and mysqld.exe as well , in the similar format as above .Please review the password and database information from D:\home\data\mysql\MYSQLCONNSTR_localdb.ini  before entering the connection information in the commands below. Here is an example to flush logs
mysqladmin.exe flush-logs --user=azure --password=password --port=49175 --bind-address=127.0.0.1

Logging

Turn on slow query logs or general logs for MySQL . Once turned on , you can find these logs in D:\home\logfiles\mysql folder. Note if these settings are always on , then this can impact your web app performance localmysql6

How to deploy your web app to using MySQL in-app

Get the database connection string

Before you deploy your web app code , the key thing to note when using this feature is to use ENVIRONMENT VARIABLES since the database connection information is not accessible directly. You can get the database connection information using MYSQLCONNSTR_localdb environment variable . You can also get the connection string from D:\home\data\mysql\MYSQLCONNSTR_localdb.ini .Here is a sample code snippet that you can use in your application to get the database host, port, database name , database user, database password.

$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

foreach ($_SERVER as $key => $value) {
    if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
        continue;
    }
    
    $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
    $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
    $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
    $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}

$link = mysqli_connect($connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword,$connectstr_dbname);

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
As an example , if you are running a WordPress site you need to update wp-config.php such that it reads the connection string from the environment variable .
/*Add at the begining of the file*/

$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

foreach ($_SERVER as $key => $value) {
    if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
        continue;
    }
    
    $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
    $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
    $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
    $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', $connectstr_dbname);

/** MySQL database username */
define('DB_USER', $connectstr_dbusername);

/** MySQL database password */
define('DB_PASSWORD', $connectstr_dbpassword);

/** MySQL hostname : this contains the port number in this format host:port . Port is not 3306 when using this feature*/
define('DB_HOST', $connectstr_dbhost);
As a BEST PRACTICE when using MySQL in-app, we recommend to ALWAYS use Environment variables for the database information to prevent database connection issues with your web app. If your application requires a separate variable for port , you can use WEBSITE_MYSQL_PORT environment variable. The port number selected can vary if the instance is recycled and hence ALWAYS use environment variables.

Deploy you code

Deploy your web app code using GIT or FTP or any one of the supported deployment processes with Azure Web Apps . Refer Deploy to Azure app service web apps for details .

Deploy your database

You cannot directly deploy you database . Hence , export your local database into a SQL script . Access your web app MySQL in-app database using PHPmyAdmin ( https://sitename.scm.azurewebsites.net/phpmyadmin) and click on the IMPORT tab to import your script into the localdbdatabase . For example:
USE localdb;

CREATE TABLE IF NOT EXISTS tasks (
  task_id INT(11) NOT NULL AUTO_INCREMENT,
  subject VARCHAR(45) DEFAULT NULL,
  start_date DATE DEFAULT NULL,
  end_date DATE DEFAULT NULL,
  description VARCHAR(200) DEFAULT NULL,
  PRIMARY KEY (task_id)
) ENGINE=InnoDB
Remember to include USE localdb; statement in your script so that the your schema and data are imported into the correct database.

How to turn on MySQL for existing Web Apps

Go to your web app and select MySQL in-app in the Menu blade on the right . Turn ON MySQL in-app feature. localmysql9 Browse your web app and Check the process explorer to verify if mysqld.exe is running. If the process is running, then MySQL server is ready to use. Remember to open the MySQL connection to your MySQL in-app database , localdb in your web app. processexploreremysql

Migration to production database

You can easily migrate this database when ready for production to

Best practices

References

Exporting your MySQL database to MySQL in-app database Benchmarking MySQL in-app performance
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - old_posts/2016-8-18-Announcing MySQL in-app for Web Apps (Windows).html [3:188]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - hide_excerpt: true ---
mksunitha
MySQL in-app feature enables running MySql natively on Azure App Service platform.You don’t need to provision a database explicitly as during the creation of the web app when using this feature,  we take care of enabling it if you select “MySQL in-app ” during creation or if the feature is turned ON for existing web app. To understand what MySQL in-app means , I have highlighted core functionality supported with the preview release of the feature:   Note : This MySQL in-app is specific to Windows version of Azure app service. If you are looking for local mysql on Linux MySQL app service , click here.

Create new Web App with MySQL in-app

Login to Azure portal  and launch Web App + MySQL template by clicking here .Enter the Site name and select MySQL in-app as the database provider. Click on Create to deploy a web app using MySQL in-app. webappmysqlcreate You may also create a WordPress application with MySQL in-app from Azure marketplace. Test drive our demo site here using MySQL in-app.You can login with username : demo and password: demopassword . 

Limitations

For preview release the feature has some limitations that to keep in mind.

Manage MySQL in-app

Go to your web app and select MySQL in-app in the Menu blade on the right. You can use the setting here to manage your MySQL in-app feature , turn on logging , access PHPmyadmin etc. localmysql8  

Access database content

The database is protected by our sandbox environment and hence cannot be accessed remotely through MySQL workbench or MySQL command line tools (running on remote machine) . There are two ways you can manage your database content : localmysql3 (2) The database enabled with your web app is "localdb". You are now ready to import your database schema or create a new one for your web app. localmysql5
D:\Program Files (x86)\mysql\5.7.9.0\bin\mysql.exe -e "ENTER SQL STATEMENTS" --user=azure --password=password --port=49175 --bind-address=127.0.0.1  
Example:
D:\Program Files (x86)\mysql\5.7.9.0\bin\mysql.exe -e "USE localdb;Select * from tasks;" --user=azure --password=password --port=49175 --bind-address=127.0.0.1  
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: Unknown OS character set 'cp0'.
mysql: Switching to the default character set 'latin1'.
task_id    subject    start_date    end_date    description
1    task1    2016-02-18    2016-02-19     sample task
The mysql warning statements can be ignored in the output. You can use mysqladmin.exe and mysqld.exe as well , in the similar format as above .Please review the password and database information from D:\home\data\mysql\MYSQLCONNSTR_localdb.ini  before entering the connection information in the commands below. Here is an example to flush logs
mysqladmin.exe flush-logs --user=azure --password=password --port=49175 --bind-address=127.0.0.1

Logging

Turn on slow query logs or general logs for MySQL . Once turned on , you can find these logs in D:\home\logfiles\mysql folder. Note if these settings are always on , then this can impact your web app performance localmysql6

How to deploy your web app to using MySQL in-app

Get the database connection string

Before you deploy your web app code , the key thing to note when using this feature is to use ENVIRONMENT VARIABLES since the database connection information is not accessible directly. You can get the database connection information using MYSQLCONNSTR_localdb environment variable . You can also get the connection string from D:\home\data\mysql\MYSQLCONNSTR_localdb.ini .Here is a sample code snippet that you can use in your application to get the database host, port, database name , database user, database password.

$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

foreach ($_SERVER as $key => $value) {
    if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
        continue;
    }
    
    $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
    $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
    $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
    $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}

$link = mysqli_connect($connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword,$connectstr_dbname);

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
As an example , if you are running a WordPress site you need to update wp-config.php such that it reads the connection string from the environment variable .
/*Add at the begining of the file*/

$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

foreach ($_SERVER as $key => $value) {
    if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
        continue;
    }
    
    $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
    $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
    $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
    $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', $connectstr_dbname);

/** MySQL database username */
define('DB_USER', $connectstr_dbusername);

/** MySQL database password */
define('DB_PASSWORD', $connectstr_dbpassword);

/** MySQL hostname : this contains the port number in this format host:port . Port is not 3306 when using this feature*/
define('DB_HOST', $connectstr_dbhost);
As a BEST PRACTICE when using MySQL in-app, we recommend to ALWAYS use Environment variables for the database information to prevent database connection issues with your web app. If your application requires a separate variable for port , you can use WEBSITE_MYSQL_PORT environment variable. The port number selected can vary if the instance is recycled and hence ALWAYS use environment variables.

Deploy you code

Deploy your web app code using GIT or FTP or any one of the supported deployment processes with Azure Web Apps . Refer Deploy to Azure app service web apps for details .

Deploy your database

You cannot directly deploy you database . Hence , export your local database into a SQL script . Access your web app MySQL in-app database using PHPmyAdmin ( https://sitename.scm.azurewebsites.net/phpmyadmin) and click on the IMPORT tab to import your script into the localdbdatabase . For example:
USE localdb;

CREATE TABLE IF NOT EXISTS tasks (
  task_id INT(11) NOT NULL AUTO_INCREMENT,
  subject VARCHAR(45) DEFAULT NULL,
  start_date DATE DEFAULT NULL,
  end_date DATE DEFAULT NULL,
  description VARCHAR(200) DEFAULT NULL,
  PRIMARY KEY (task_id)
) ENGINE=InnoDB
Remember to include USE localdb; statement in your script so that the your schema and data are imported into the correct database.

How to turn on MySQL for existing Web Apps

Go to your web app and select MySQL in-app in the Menu blade on the right . Turn ON MySQL in-app feature. localmysql9 Browse your web app and Check the process explorer to verify if mysqld.exe is running. If the process is running, then MySQL server is ready to use. Remember to open the MySQL connection to your MySQL in-app database , localdb in your web app. processexploreremysql

Migration to production database

You can easily migrate this database when ready for production to

Best practices

References

Exporting your MySQL database to MySQL in-app database Benchmarking MySQL in-app performance
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -