Azure App Service, Azure Cloud, Azure Cosmos DB, Cloud Computing

How to use NoSQL Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications: Part 6

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications

Azure DocumentDB or Azure Cosmos DB is database for very low latency and immensely saleable applications, which have global audience and it provides native NOSQL support. Azure cosmos DB support storing and retrieving JSON object at scale.

During development you do not need to create a new instance of documentDB, visual studio provide you the emulator to use during development phase. To explore the possibilities, In our case, we will create Azure Cosmos DB / documentDB on azure and use it in our application that we are building in this series.

In Azure first we need to create an account for Azure Cosmos DB and then we can create collections and databases in it. Let us do just that.

Go to Azure Portal and Click on Add and search for documentDB | select Azure Cosmos DB and click on create.

Figure 1 Create Azure Cosmos DB

Now fill the form, select the API, Azure support multiple APIs like SQL API, MongoDB API, Graph API, Table API and Cassandra API, and choose the appropriate API, we are going for SQL API. This will create a new Azure Cosmos DB account for you.

For more info on APIs

Figure 2 Create Azure Cosmos DB

It may take some time to create it. Once created go to the resource | select your documentdb account | Quick start. Right now, we do not have any collection and we do not have any database. Firstly we will create new collection, and then create database in that collection, but we have a short cut to do just that, click on Create Items collection button. It will create the basic collection for you which will cost you around 0.033$ / hour and will give you 400 reads/sec. Read/sec defined as if the file is 1 kb size and 1 kb file read is represented as 1 RU  then 400 request could be read per second 400 RU/S and under 400 RU/S load, our current documentDB will work just fine. If the requests are, more the throttling will happen and latency will increase, in case of extreme throttling request timeout could happen. Writing operation will cost you more than reading, Writing the file of 1 Kb size will cost you 5 RUs as it is more expensive then read. You can check following link for more info

Figure 3 Create Items collection in Azure Cosmos DB

Click on Create “Items” collection” and then click on  download  button to get the code for TODOList sample Project, we will use this project as sample and will modify our existing project to communicate with the Azure Cosmos DB.

Now go to Collections | Browse you will see that it created new collection Items and database in it called ToDoList , all we have to do now is to integrate todo project we have downloaded, to our app that we are building in this series.

Figure 4 Azure Cosmos DB collections and database

Open the downloaded sample source code in the visual studio and run for few times and add some records. This will create some record in the database that we will see later and we will also clear some misconceptions about documentDB that many developers may have.

Figure 5 Run TodoList sample project

Open DocumentDBRepository, this is our main file, which is communicating with Azure Cosmos DB, and notice it is using , endpoint, authKey, database and collection app settings. We will copy the DocumentDBRepository.cs file and include appSettings to our exisitng application.

Figure 6 DocumentDBRepository

Make sure to install Microsoft.Azure.DocumentDB package through nugget.

Figure 7 Install Microsoft.Azure.DocumentDB

Make sure copy all the files highlighted and change _layout.cshtml file to include our Item controller also highlighted.

Figure 8 migrated the code

Also, make sure you copy below to Global.asax.cs | build  | check locally that there is no error while building or running, and publish the changes to App Service, we have already covered how to publish the source code to app service in previous posts.

DocumentDBRepository<todo.Models.Item>.Initialize();

Figure 9 Insert DocumentDBRepository initialize line

Now once your project run successfully, insert records few times by creating some tasks. Keep in mind that we can use To-do Items example to insert any JSON serializable object in the same collection even if the object do not relate with each other, DocumentDB is not limited to structure like table, you can insert any object of any type given that it is serializable to JSON or can be represented in JSON format.

Figure 10 insert few To-do items in documentDb

Sample code is also given below:

We can also verify the data is inserted through Azure Portal, Go to your Azure Cosmos DB | Collections | Document Explorer | click any item | you will see the data in document tab in JSON format.

{


“id”: “d2dc5626-e279-4390-b079-f6ba1a4af982”,


“name”: “test3”,


“description”: “test3”,


“isComplete”: false

}

Check for more information

At this point you are successfully integrated your existing project to use the Azure Cosmos DB.

Coming up next How to use Visual Studio Team Service to do continuous Integration and continuous delivery

 

Azure Cloud

Creating Azure Database for MySQL and exporting the Data from My SQL In App to Azure Database for MySQL Server, Handling Security: Part 3

Are you looking to create your very own Scalable optimized WordPress Site/blog using Azure MySQL, Azure Storage and Azure CDN, then this post series is for you?

I have divided the series into below parts:

  1. Getting Azure Subscription, Creating App Service Plan and WordPress Web Service using My SQL In App
  2. Connecting Word Press website with WordPress.com and Installing and configuring Jet Pack
  3. Creating Azure Database for MySQL and exporting the Data from My SQL In App to Azure Database for MySQL Server, Handling Security
  4. Creating Azure Storage Account and configuring WordPress to use Azure Storage to store media files and Optimizing with persistent connection
  5. Configuring Custom domain, SSL Certificates.
  6. Configuring Azure CDN, configuring back up scheduling in azure and Auto Scaling
  7. How to use REDIS CACHE with WORDPRESS on Azure
  8. How to install application insights in WordPress on Azure

If you are not following along please check the previous part 1 first.

3.Creating Azure Database for MySQL and exporting the Data from My SQL In App to Azure Database for MySQL Server, Handling Security

At this point, your site is connected with WordPress.com now we are moving to part 3, which is Creating Azure Database for MySQL, and exporting the Data from My SQL in App to Azure Database for MySQL server, we will also handle security and will optimize with persistent connection.

Let us go to https://Portal.Azure.com/

Now go to All Resources and then click “Add” button, type “Azure MySQL” in search area and hit enter.

Search for Azure MySQL

1 Search for Azure MySQL

Right now we are interested in the first item, “Azure Database for MySQL (preview)” currently it is in preview but by the time you view this post, may be it is released to public as well.

Select Azure MySQL

2 Select Azure MySQL

Select “Azure Database for MySQL (preview)” and click on “Create”

Create Azure MySQL

3 Create Azure MySQL

At this point, add the Server Name, choose Resource Group, you can choose existing one, mention password and then for Pricing Tier choose “Basic” as this is our blog site it usually don’t need high throughput unless you are top blogger on internet, click “OK and complete the information and click “Create”

Enter Information for Azure MySQL instance, choose pricing tier

4 Enter Information for Azure MySQL instance, choose pricing tier

Job is submitted to Azure and it may take few minutes to complete.

Job is submitted to Azure

5 Job is submitted to Azure

Click on “Go to Resource”

Deployment completed.

6 Deployment completed.

Now we will disable the SSL enforce Status to false and we will add our IP Address and our Azure Web Service app’s IP Addresses, so that our WordPress site can access it. I will write an article on how to enable SSL in Azure MySQL so that it can communicate with our WordPress Site on encrypted channel, since it needs some changes in WordPress site. For now we are going to disable the SSL but we will add Connection security next.

Disabled the SSL for Database Server.

7 Disabled the SSL for Database Server.

Click on “Disabled” it will show like below, and click on “Save” button.

SSL Disable

8 SSL Disable

In mean while we will change our sites settings as below: Platform 64bit usually put more performance through put than 32 bit variant and Always On will keep your site in ram which will speed up the access time in case your WordPress site is not accessed for a long time and Click on “Save”

Optimizing Site Response Time

9 Optimizing Site Response Time

Now click on “Properties” under “settings” heading and copy all the IP address under “OUTBOUND IP ADDRESSES”, we will use these IP Address and add them in MySQL Server, so that our WordPress Site can access the Server without problem.

App Service IP Addresses

10 App Service IP Addresses

Now Add All the IP Address in “Connection Security” window and click on Save, use the Same IP Address for “START IP” and “END IP”, please make sure to add 1 IP Address at a time, since we have at least 4 or more IP Addresses copied from above window.

Connection Security

11 Connection Security

Your screen will look like below, and of course, IP Addresses will be different in your case, Add All IP Address and click on “Save” Button.

Insert IP Addresses of App Service

12 Insert IP Addresses of App Service

Now we are ready to move our database from MySQL in App to Azure MySQL. Go to “MySQL in APP” and click on export.

Export MySQL Database

13 Export MySQL Database

Now fill the information, if you do not know the Username and Database and Server, then you can always get this information from the MySQL Database instance.

Enter Credentials

14 Enter Credentials

Get Values for Server name and User ID

15 Get Values for Server name and User ID

For Database name, Click on “Manage” in “MySQL in APP”

Manage MySQL in App

16 Manage MySQL in App

A new window will open, if it prompt for login, please login. In our case, we have five databases listed as below: as we know that 4 out of 5 are MySQL databases (information_schema, mysql, performance_schema, sys) so this left us with one database “localdb”

PhpMyAdmin export database

17 PhpMyAdmin export database

Now fill out your “Export” form and click save

Export to New database instance

18 Export to New database instance

If you get below error then

Export Error

19 Export Error

Add below IP address range to allow all the IP address to connect with your database server and try again.

Enable IP Address for All

20 Enable IP Address for All

If for some reason you are unable to proceed then we can proceed for Export Option, Go to “Manage” in “MySQL in APP” and click on “Export” and click “Go”, save the resultant script to the local computer.

Exporting Databases

21 Exporting Databases

Now we will use Workbench to connect with MySQL database server, follow the link to download https://www.mysql.com/products/workbench/
Installing Workbench

22 Installing Workbench

There is an excellent documentation on Azure Docs, which explains how to connect to the MySQL server-using workbench

https://docs.microsoft.com/en-us/azure/mysql/connect-workbench

Now I assume you have downloaded and successfully connected to MySQL sever. Please follow below link navigate to “Data Import” heading, just connect to server, click option “Import from Disk” navigate to the file which we saved earlier in export part and click “Start Import”, everything supposed to work fine.

https://docs.microsoft.com/en-us/azure/mysql/concepts-migrate-import-export

Importing databases using Workbench

23 Importing databases using Workbench

Now is the part for connecting your WordPress to new MySQL server. Please proceed for following, Go to database server, “Connection strings” and then copy the connection string for “Web App” and modify it as per your MySQL Server credentials.

Database={your_database}; Data Source={your_databaseserver}; User Id={your_UserId}; Password={your_password}

Copy Connection string

24 Copy Connection string

Now go to “Application Settings”, add “Connection strings”, and add connection string with Name MYSQLCONNSTR and value with your modified Web App connection string and click “Save”

Saving Connection String in Application Settings

25 Saving Connection String in Application Settings

Now switch “MySQL in App” to “Off” and click on “Save”

Shutdown MySQL in App Instance

26 Shutdown MySQL in App Instance

Now Access you Site, it should be working fine. If you are getting any errors at this point, make sure that your credentials are correct.

Successfully did the Import

27 Successfully did the Import

Congratulations! You have successfully exported your MySQL Database to Your New Azure MySQL Instance.

After you are done, Please do not forget to remove the IP Address that we added to enable all to connect to over MySQL Server.

Delete Allow All IP Addresses
Delete Allow All IP Addresses

Upcoming next Creating Azure Storage Account and configuring WordPress to use Azure Storage to store media files.