29,99 €
Despite being the cloud version of SQL Server, Azure SQL Database and Azure SQL Managed Instance stands out in various aspects when it comes to management, maintenance, and administration. Updated with the latest Azure features, Professional Azure SQL Managed Database Administration continues to be a comprehensive guide for becoming proficient in data management.
The book begins by introducing you to the Azure SQL managed databases (Azure SQL Database and Azure SQL Managed Instance), explaining their architecture, and how they differ from an on-premises SQL server. You will then learn how to perform common tasks, such as migrating, backing up, and restoring a SQL Server database to an Azure database.
As you progress, you will study how you can save costs and manage and scale multiple SQL databases using elastic pools. You will also implement a disaster recovery solution using standard and active geo-replication. Finally, you will explore the monitoring and tuning of databases, the key features of databases, and the phenomenon of app modernization.
By the end of this book, you will have mastered the key aspects of an Azure SQL database and Azure SQL managed instance, including migration, backup restorations, performance optimization, high availability, and disaster recovery.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 563
Veröffentlichungsjahr: 2021
Efficiently manage and modernize data in the cloud using Azure SQL
Ahmad Osama and Shashikant Shakya
Professional Azure SQL Managed Database Administration – Third Edition
Copyright © 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Authors: Ahmad Osama and Shashikant Shakya
Technical Reviewers: John Martin and Aaditya Pokkunuri
Managing Editors: Aditya Datar and Mamta Yadav
Technical Editor: Neha Pande
Acquisitions Editor: Ben Renow-Clarke
Production Editor: Deepak Chavan
Editorial Board: Vishal Bodwani, Ben Renow-Clarke, Arijit Sarkar, Dominic Shakeshaft, and Lucy Wan
First Published: July 2018
Second Published: July 2019
Third Published: January 2021
Production Reference: 3220221
ISBN: 978-1-80107-652-4
Published by Packt Publishing Ltd.
Livery Place, 35 Livery Street
Birmingham B3 2PB, UK
This section briefly introduces the authors and reviewers, what this book covers, the technical skills you'll need to get started, and the hardware and software requirements needed to complete all of the activities and exercises.
Despite being the cloud version of SQL Server, Azure SQL Database and Azure SQL Managed Instance stands out in various aspects when it comes to management, maintenance, and administration. Updated with the latest Azure features, Professional Azure SQL Managed Database Administration continues to be a comprehensive guide for becoming proficient in data management.
The book begins by introducing you to the Azure SQL managed databases (Azure SQL Database and Azure SQL Managed Instance), explaining their architecture, and how they differ from an on-premises SQL server. You will then learn how to perform common tasks, such as migrating, backing up, and restoring a SQL Server database to an Azure database.
As you progress, you will study how you can save costs and manage and scale multiple SQL databases using elastic pools. You will also implement a disaster recovery solution using standard and active geo-replication. Finally, you will explore the monitoring and tuning of databases, the key features of databases, and the phenomenon of app modernization.
By the end of this book, you will have mastered the key aspects of an Azure SQL database and Azure SQL managed instance, including migration, backup restorations, performance optimization, high availability, and disaster recovery.
Ahmad Osama works for Pitney Bowes Pvt. Ltd. as a technical architect and is a former Microsoft Data Platform MVP. In his day job, he works on developing and maintaining high performant, on-premises and cloud SQL Server OLTP environments as well as deployment and automating tasks using PowerShell. When not working, Ahmad blogs at DataPlatformLabs and can be found glued to his Xbox.
Shashikant Shakya is a passionate technologist with decades of experience in the sphere of databases. He works for Microsoft as a senior support engineer. In his day job, he works on Azure SQL Database, Azure Database for MySQL, and PostgreSQL. Apart from his work, he is a regular speaker at the SQLBangalore community group.
Aaditya Pokkunuri is an experienced senior database engineer with a history of working in the information technology and services industry. He has a total of 11 years' experience. He is skilled in performance tuning, MS SQL Database server administration, SSIS, SSRS, Power BI, and SQL development.
He possesses an in-depth knowledge of replication, clustering, SQL Server high availability options, and ITIL processes, as well as expertise in Windows administration tasks, Active Directory, and Microsoft Azure technologies.
He also has expertise in AWS Cloud and is an AWS solution architect associate. Aaditya is a strong information technology professional with a Bachelor of Technology degree focused on computer science and engineering from Sastra University, Tamil Nadu.
John Martin is an experienced data platform professional and Microsoft Data Platform MVP, having spent over a decade working with the Microsoft data and cloud platform technologies. In this time, John has learned how to get the most out of these platforms as well as the key pitfalls that should be avoided.
This book is designed to benefit database administrators, database developers, or application developers who are interested in developing new applications or migrating existing ones with Azure SQL Database.
Prior experience of working with an on-premises SQL server or Azure SQL database, along with a basic understanding of PowerShell scripts and C# code, is necessary to grasp the concepts covered in this book.
Professional Azure SQL Managed Database Administration is a perfect blend of deep theoretical knowledge and detailed descriptions of implementation techniques and numerous tips that are essential for making its readers ready for real-world challenges.
Hardware requirements
For the optimal learning experience, we recommend the following hardware configuration:
Windows 10/Mac/LinuxProcessor: Pentium 4, 1.8 GHz or higher (or equivalent)Memory: 4 GB RAMHard disk: 10 GB free spaceAn internet connectionSoftware requirements
We also recommend that you have the following software configuration in advance:
PowerShell 7: https://github.com/PowerShell/powershell/releasesRML Utilities: https://www.microsoft.com/download/details.aspx?id=4511SQL Server Management Studio: https://docs.microsoft.com/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver15Power BI Desktop (optional): https://powerbi.microsoft.com/downloads/Azure Data Studio: https://docs.microsoft.com/sql/azure-data-studio/download-azure-data-studio?view=sql-server-ver15Azure Az PowerShell module: https://docs.microsoft.com/powershell/azure/new-azureps-module-az?view=azps-5.2.0Code words in the text, database names, folder names, filenames, and file extensions are shown as follows.
"The query gets the details for the cpu_percent, physical_data_read_percent, log_ write_percent, workers_percent, and sessions_percent metrics."
Here's a sample block of code:
AzureMetrics
| where ResourceProvider=="MICROSOFT.SQL" | where ResourceId contains "/ SERVERS/"
| where ResourceId contains "/DATABASES/" and MetricName in ('cpu_ percent', 'physical_data_read_percent', 'log_write_percent', 'workers_ percent', 'sessions_percent')
The code bundle for this book is also hosted on GitHub at https://github.com/PacktPublishing/Professional-Azure-SQL-Database-Administration-Third-Edition. Here, you can find the YAML and other files used in this book, which are referred to as relevant instances.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We are grateful to Microsoft and their team of SMEs for reviewing the book and providing suggestions that enhanced this edition. Acknowledging their contribution, we have listed down the names of the experts who contributed to this book.
Anna Hoffman | Shreya Verma | Borko Novakovic | Denzil Ribeiro Venkata Raj Pochiraju | Morgan Oslake | Mladen Andzic | Andreas Wolter Mirek Sztajno | Joachim Hammer | David Trigano | Srini Acharya Uros Milanovic | Emily Lisa | Joe Sack | Mara-Florina Steiu | Davide Mauri
For successful completion of this edition of Professional Azure SQL Managed Database Administration,special thanks our authors, Ahmad Osama for his contribution on Azure SQL Database and Shashikant Shakya for his contribution on Azure SQL Managed Instance.
There are very few relational database systems as established and widely used as Microsoft's SQL Server. SQL Server on Microsoft Azure comes in three different flavors (commonly known as the Azure SQL family): SQL Server on Azure Virtual Machines (VM) (infrastructure as a service, or IaaS), Azure SQL Database (platform as a service, or PaaS), and Azure SQL Managed Instance (PaaS).
Each of these products has specific use cases, which makes it easy for us to move to Azure SQL whether we're starting up with a new application or migrating an existing workload to Azure.
The IaaS offering, SQL Server on Azure VM, is similar to an on-premises service where Microsoft manages the hardware, virtualization, and infrastructure, and database administrators (DBAs) manage every aspect of SQL Server.
The PaaS offerings, Azure SQL Database and Azure SQL Managed Instance, allow DBAs to focus more on monitoring, capacity planning, and tuning, while Microsoft takes care of areas such as backup, high availability, and more.
This chapter introduces the Azure SQL Database architecture, the Azure SQL Managed Instance connectivity architecture, and the differences between the SQL Database, SQL Managed Instance, and SQL Server (on-premises or using Azure VM) offerings.
In this chapter, we will be covering the following topics:
Describing the architecture of SQL DatabaseIdentifying the differences between an on-premises SQL Server, SQL Database, and SQL Managed InstanceThe connectivity architecture of SQL Managed InstanceProvisioning an Azure SQL Database and Azure SQL Managed Instance using the Azure portal and Windows PowerShellFigure 1.1 lists the tasks that you (the DBA) and Microsoft manage for Azure SQL PaaS:
Figure 1.1: Who manages what?
Fixing outages here refers to application outages that arise due to blockages, deadlocks, and broken releases, rather than infrastructure outages.
In an Azure SQL PaaS environment, the DBA works closely with application developers to understand the application and database design, help with the migration (when moving from on-premises to Azure), choose the right performance tier to start with, and then continuously monitor performance for cost optimization.
The DBA also has to work closely with DevOps and often get into DevOps' shoes to automate the release and deployment process and provision the database infrastructure.
This requires learning a new set of skills, such as familiarity with different Azure services, DevOps, and monitoring and management tools.
This chapter introduces the two Azure SQL PaaS offerings, SQL Database and SQL Managed Instance. We'll learn about the SQL Database and SQL Managed Instance architectures, provision SQL Database and SQL Managed Instance, and identify the key differences between SQL Database, SQL Managed Instance, and on-premises SQL Server.
Throughout this book, you will also learn more about the different aspects of managing and administrating SQL Database and SQL Managed Instance, such as provisioning, migration, backup, restore, security, monitoring, and performance.
Azure SQL Database is also commonly referred to as SQL Azure or SQL Database instances.
Azure SQL Database is a highly scalable, multi-tenant, and highly available Platform-as-a-Service (PaaS) or Database-as-a-Service (DBaaS) offering from Microsoft.
Azure SQL Database, first released on February 1, 2010, is a cloud database service that is based on Microsoft SQL Server.
It is compatible with most SQL Server database-level features and is optimized for Software-as-a-Service (SaaS) applications.
As organizations are adopting cloud computing and moving their applications into the cloud, Azure SQL Database offers everything that DBaaS can offer. Azure SQL Database is a DBaaS option for any organization with applications built on SQL Server databases.
SQL Database uses familiar Transact-SQL programming and a user interface that is well known and easy to adopt. As companies move their workloads to the cloud, it is important for SQL Server DBAs and developers to learn how to use Azure SQL Database for a smooth transition from SQL Server (on-premises or on Azure VM) to SQL Database.
Microsoft takes care of the operating system (OS), storage, networking, virtualization, servers, installation, upgrades, infrastructure management, and maintenance.
Azure SQL Database has the following deployment options:
Single databaseElastic poolAzure SQL Database allows users to focus only on managing data and is divided into four layers that work together to provide users with relational database functionality, as shown in Figure 1.2:
Figure 1.2: The four layers of Azure SQL Database
If you were to compare SQL Database's architecture to the on-premises SQL Server architecture, other than the service layer, the architecture is pretty similar.
The client layer acts as an interface for applications to access an SQL Database. It can be either on-premises or on Microsoft Azure. The Tabular Data Stream (TDS) is used to transfer data between an SQL Database and applications. SQL Server also uses TDS to communicate with applications. This allows applications such as .NET, ODBC, ADO.NET, Python, and Java applications to easily connect to Azure SQL Database without any additional requirements.
The service layer acts as a gateway between the client and platform layers. It is responsible for provisioning an SQL Database, user authentication, SQL Database validation, enforcing security (including firewall rules and denial-of-service attacks), billing and metering for SQL Databases, and routing connections from the client layer to the physical server hosting the SQL Database in the platform layer.
The platform layer consists of physical servers hosting SQL Databases in datacenters. Each SQL database is stored on one physical server and is replicated across two different physical servers to provide high availability.
As shown in Figure 1.2, the platform layer has two other components: Azure Service Fabric and Management Service. Azure Service Fabric is responsible for load balancing, automatic failover, and the automatic replication of SQL Databases between physical servers. Management Service takes care of an individual server's health monitoring and patch updates.
This layer is responsible for the administration of the physical hardware and the OS.
Figure 1.3 shows the platform layer:
Figure 1.3: Platform layer – nodes
The application sends a TDS request (login, DML, or DDL queries) to the SQL Database. The TDS request is not directly sent to the platform layer. The request is first validated by the SQL Gateway Service at the service layer.
The Gateway Service validates the login and firewall rules and checks for denial-of-service attacks. It then dynamically determines the physical server on which the SQL Database is hosted and routes the request to that physical server in the platform layer. Dynamic routing allows the SQL Database to be moved across physical servers or SQL instances in the event of hardware failures.
Here, a node is a physical server. A single database is replicated across three physical servers internally by Microsoft to help the system recover from physical server failures. The Azure SQL Server user connects to just a logical name.
Dynamic routing refers to routing the database request to the physical server that hosts an Azure SQL Database. This routing is done internally and is transparent to the user. If one physical server hosting the database fails, dynamic routing will route the requests to the next available physical server hosting the Azure SQL Database.
The internals of dynamic routing are out of the scope of this book.
As shown in Figure 1.3, the platform layer has three nodes: Node 1, Node 2, and Node 3. Each node has a primary replica of an SQL Database and two secondary replicas of two different SQL Databases from two different physical servers. The SQL Database can fail over to the secondary replicas if the primary replica fails. This ensures the high availability of the SQL Database.
Provisioning an Azure SQL Database refers to creating a new and blank Azure SQL Database.
In this section, we'll create a new SQL Database in Azure using the Azure portal:
Open a browser and log in to the Azure portal using your Azure credentials: https://portal.azure.com.In the left-hand navigation pane, select Create a resource:Figure 1.4: Azure pane
On the New page, under Databases, select SQL Database:Figure 1.5: Azure panel
On the SQL Database page, under the Project details heading, provide the Subscription and Resource group details. Click the Create new link under the Resource group textbox. In the pop-up box, set the Resource group name as toystore.A resource group is a logical container that is used to group the Azure resources required to run an application.
For example, the toystore retail web application uses different Azure resources, such as Azure SQL Database, Azure VMs, and Azure Storage. All of these resources can be grouped into a single resource group, say, toystore.
The SQL Database name should be unique across Microsoft Azure and should follow the following naming rules and conventions: https://docs.microsoft.com/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging:
Figure 1.6: SQL Database panel
Under the Database details heading, enter the database name and server.To create a new server, click on Create new under the Server textbox.On the New server page, provide the following details and click Select at the bottom of the page: Server name, Server admin login, Password, Confirm password, and Location.
The server name should be unique across Microsoft Azure and should follow the following naming rules and conventions: https://docs.microsoft.com/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging:
Figure 1.7: Server pane
Under the Want to use SQL elastic pool? option, select No.In Compute + storage, click Configure database and then select Standard:Figure 1.8: The Configure window
Note that you will have to click the Looking for basic, standard,premium? link for the standard option to be available:
Figure 1.9: The Configure pane
Skip the options under Networking and Additional settings.Click Review+create to continue:Figure 1.10: SQL pane provisioning panel
On the TERMS page, read through the terms and conditions and the configuration settings made so far:Figure 1.11: The TERMS page
Click Create to provision the SQL Database.Provisioning may take 2-5 minutes. Once the resources are provisioned, you'll get a notification, as shown in Figure 1.12:
Figure 1.12: Notification after provision completion
You can click Go to resource to go to the newly created SQL Database.You have now provisioned your first Azure SQL Database.
In this section, we'll learn how to connect and query the SQL Database from the Azure portal:
From the toystore pane, select Query editor (preview):Figure 1.13: toystore pane
In the Query editor (preview) pane, select Login, and under SQL server authentication, provide the username and password:Figure 1.14: The Query Editor (preview) pane
Select OK to authenticate and return to the Query editor (preview) pane:
Open C:\Code\Chapter01\sqlquery.sql in Notepad. Copy and paste the query from Notepad into the Query 1 window in the Query editor in the Azure portal.The query creates a new table (orders), populates it with sample data, and returns the top 10 rows from the orders table:
-- create a new orders table CREATE TABLE orders
(
orderid INT IDENTITY(1, 1) PRIMARY KEY,
quantity INT, sales MONEY
);
--populate Orders table with sample data
;
WITH t1
AS (SELECT 1 AS a UNION ALL
SELECT 1),
t2
AS (SELECT 1 AS a FROM t1
CROSS JOIN t1 AS b),
t3
AS (SELECT 1 AS a FROM t2
CROSS JOIN t2 AS b),
t4
AS (SELECT 1 AS a FROM t3
CROSS JOIN t3 AS b),
t5
AS (SELECT 1 AS a FROM t4
CROSS JOIN t4 AS b),
nums
AS (SELECT Row_number()
OVER (
ORDER BY (SELECT NULL)) AS n
FROM t5)
INSERT INTO orders SELECT n,
n * 10
FROM nums;
GO
SELECT TOP 10 * from orders;
Select Run to execute the query. You should get the following output:Figure 1.15: Expected output
The query editor allows us to connect and query from the Azure portal; however, it's not as strong in features as clients such as SQL Server Management Studio and Azure Data Studio.
In this section, we'll connect to and query an Azure SQL Database from SQL Server Management Studio (SSMS):
Open SSMS. In the Connect to Server dialog box, set the Server type as Database Engine, if not already selected.Under Server name, provide the Azure SQL Server name. You can find the Azure SQL Server name in the Overview section of the Azure SQL Database pane in the Azure portal:Figure 1.16: Overview pane of the toystore database
Select SQL Server Authentication as the authentication type.Provide the login and password for Azure SQL Server and select Connect:Figure 1.17: Login panel of SQL Server
You'll get an error saying Your client IP address does not have access to the server. To connect to Azure SQL Server, you must add the IP of the system you want to connect from under the firewall rule of Azure SQL Server. You can also provide a range of IP addresses to connect from:
Figure 1.18: New Firewall Rule pane
You can also sign in and add a client IP to the Azure SQL Server firewall by using the Sign In button shown in Figure 1.18 and following the instructions.
To add your machine's IP to the Azure SQL Server firewall rule, switch to the Azure portal.
Open the toystore SQL Database Overview pane, if it's not already open. From the Overview pane, select Set server firewall:
Figure 1.19: Setting the server firewall in the Overview pane
In the Firewall settings pane, select Add client IP:Figure 1.20: The Add client IP option in the Firewall settings pane
The Azure portal will automatically detect the machine's IP and add it to the firewall rule.If you wish to rename the rule, you can do so by providing a meaningful name in the RULE NAME column.
All machines with IPs between START IP and END IP are allowed to access all of the databases on the toyfactory server:
A virtual network can be used to add an SQL Database in Azure to a given network. A detailed explanation of virtual networks is out of the scope of this book.
Figure 1.21: The Firewall settings pane
Click Save to save the firewall rule.
Switch back to SSMS and click Connect. You should now be able to connect to Azure SQL Server. Press F8 to open Object Explorer, if it's not already open:Figure 1.22: Object Explorer pane
You can view and modify the firewall settings using T-SQL in the master database. Press Ctrl + N to open a new query window. Make sure that the database is set to master.To open a new query window in the master database context, in Object Explorer, expand Databases, then expand System Databases. Right-click the master database and select New Query.
Enter the following query to view the existing firewall rules:SELECT * FROM sys.firewall_rules
You should get the following output:
Figure 1.23: Existing firewall rules
The AllowAllWindowsAzureIps firewall is the default firewall, which allows resources within Microsoft to access Azure SQL Server.
The rest are user-defined firewall rules. The firewall rules for you will be different from what is shown here.
You can use sp_set_firewall_rule to add a new firewall rule and sp_delete_ firewall_rule to delete an existing firewall rule.
To query the toystore SQL Database, change the database context of the SSMS query window to toystore. You can do this by selecting the toystore database from the database dropdown in the menu:Figure 1.24: Dropdown to select the toystore database
Copy and paste the following query into the query window:SELECT COUNT(*) AS OrderCount FROM orders;
The query will return the total number of orders from the orders table. You should get the following output:
Figure 1.25: Total number of orders in the orders table
We can connect to and query Azure SQL Server from SSMS as we do for an on-premises SQL Server. However, SSMS doesn't have all of the features or options that are available in Azure SQL Database.
To delete an Azure SQL Database, an Azure SQL Server instance, and Azure resource groups, perform the following steps:
All resources must be deleted to successfully complete the activity at the end of this chapter.
Switch to the Azure portal and select All resources from the left-hand navigation pane.From the All resources pane, select the checkbox next to the toyfactory Azure SQL Server instance and then select Delete from the top menu:Figure 1.26: Deleting the toyfactory SQL Server
In the Delete Resources window, type yes in the confirmation box and click the Delete button to delete the Azure SQL Server instance and the Azure SQL Database:Figure 1.27: Confirming the deletion of the selected resources
To only delete an Azure SQL Database, check the Azure SQL Database checkbox.
To delete the Azure resource group, select Resource groups from the left-hand navigation pane:Figure 1.28: Resource groups
In the Resource groups pane, click the three dots next to the toystore resource group, and then select Delete resource group from the context menu:Figure 1.29: Delete resource group option
In the delete confirmation pane, type the resource under the TYPE THE RESOURCE GROUP NAME section, and then click Delete.We can easily delete resources using the Azure portal. However, note that we may not be able to recover the deleted resource.
Azure SQL Managed Instance is a fully managed SQL Server instance offering announced in May 2017 and made generally available on October 1, 2018.
Azure SQL Managed Instance provides nearly 100% surface area compatibility with the latest SQL Server (Enterprise Edition) database engine, providing all the PaaS benefits available with Azure SQL Database, such as automatic patching and version updates, automatic backups, high availability, and so on.
Note that Azure SQL Managed Instance is its own product within the Azure SQL family, rather than being just a deployment option for Azure SQL Database, with near 100% compatibility with on-premises SQL Server instances.
Azure SQL Managed Instance supports most of the instance-scoped features of traditional SQL Server deployment, which were previously not available in Azure SQL Database, since Azure SQL Database is scoped at the database-level. Azure SQL Managed Instance, therefore, provides easy lift-and-shift migration from an on-premises environment to the cloud.
When you migrate to an Azure SQL Managed Instance on Azure, you don't only migrate databases, you can also migrate licenses too.
You can save up to 82% on Azure SQL Managed Instance when migrating from SQL Server Enterprise or Standard edition with software assurance. For more details, please visit https://azure.microsoft.com/pricing/hybrid-benefit/ or contact Azure.
Some of the important features supported by Azure SQL Managed Instance that are not available in Azure SQL Database are as follows:
Native backup and restoreGlobal temporary tablesCross-database queries and transactionsLinked serversCLR modulesSQL agentDatabase mailHere are some recently added features:
Distributed transactionsInstance poolsInstance-level Azure Active Directory server principals (logins)Transactional replicationThreat detectionLong-term backup retentionMachine learning services (R and Python)These and other features of Azure SQL Managed Instance make it almost 100% compatible with SQL Server.
Azure SQL Managed Instance is a set of services hosted on one or more isolated virtual machines inside a virtual network subnet.
When we provision an Azure SQL Managed Instance, a virtual cluster is created. A virtual cluster can have one or more SQL Managed Instances.
Applications connect to databases via an endpoint, <mi_name>.<dns_zone>.database. windows.net, and should be inside a virtual network, a peered virtual network, or an on-premises network connected via VPN or Azure ExpressRoute.
Unlike Azure SQL Database, Azure SQL Managed Instance supports Azure Virtual Network (VNet). An Azure VNet is a logical boundary or isolation that groups resources within a specified Azure region and enables secure communication between resources, the internet, and on-premises networks:
Figure 1.30: High-level connectivity architecture for SQL Managed Instances
Figure 1.30 shows a high-level connectivity architecture for SQL Managed Instances. Let's go through it:
SQL Managed Instances are part of a virtual cluster and are in an SQL Managed Instance subnet in a virtual network in the East US region.Web and other applications in the same virtual network connect to the managed instance using a TDS private IP endpoint, for example, sqlinstance.dnszone.database.windows.net.Applications in the West US virtual network connect using the same endpoint; however, the two virtual networks are peered using global virtual network peering to allow connectivity between them. The same regional virtual network can be peered with SQL Managed Instance.Global virtual network peering support for SQL Managed Instance is new to SQL Managed Instance.
On-premises applications connect using the same endpoint via VPN or an ExpressRoute gateway.To improve the overall experience and availability, Azure applies a network intent policy on virtual network infrastructure elements. The policy plays a major role in preventing network misconfiguration and ensures normal SQL Managed Instance operations.In the previous example, we saw connectivity to SQL Managed Instance from different networks. Here, we are going to learn about virtual cluster internal communication:
Figure 1.31: High-level architecture of virtual cluster connectivity
In Figure 1.31, applications/client connects to SQL Managed Instance using a fully qualified domain name (FQDN), sqlmi_name.<dns_zone>.database.windows.net. This hostname can only be resolved within a private network. The dnz_zone ID is automatically created when the virtual cluster is deployed. The private IP belongs to the internal load balancer (ILB) of SQL Managed Instance, and the load balancer forwards traffic to the SQL Managed Instance gateway (GW) service. Since multiple instances run inside the virtual cluster, the GW service redirects SQL traffic to the correct instance based on the instance name.
Management and deployment services connect to SQL Managed Instance using a load balancer (LB) that uses a public IP address. A built-in firewall only allows traffic from Microsoft IP addresses on specified management ports. All the communication inside the virtual cluster is encrypted using TLS protocols.
SQL Managed Instance needs to be deployed in a dedicated subnet in a virtual network. The subnet must have these characteristics:
Dedicated subnet: The SQL Managed Instance subnet must be reserved only for managed instances. The subnet can't contain any other Azure services except SQL Managed Instance.Sufficient IP addresses: The SQL Managed Instance subnet must have at least 16 IP addresses and a minimum of 32 IP addresses for deployment. These IP addresses are reserved for virtual cluster resources and may vary depending on the hardware generation and SQL Managed Instance service tier. Visit https://docs.microsoft.com/azure/azure-sql/managed-instance/vnet-subnet-determine-size to determine the SQL Managed Instance subnet size.Subnet delegation: The SQL Managed Instance subnet needs to be delegated to the Microsoft.Sql/managedInstances resource provider.Network security group (NSG): SQL Managed Instance requires port 1433 for TDS traffic and ports in the range 11000-11999 for redirection connection. An NSG must be associated with the SQL Managed Instance subnet.A user-defined route (UDR) table: A prerequisite is to create a route table that will allow SQL Managed Instance to communicate with the Azure Management Service.These conditions are mandatory for SQL Managed Instance creation and management operations.
A detailed explanation of the networking requirements can be found by visiting https://docs.microsoft.com/azure/azure-sql/database/connectivity-architecture.
SQL Database and SQL Managed Instance are PaaS offerings from the Azure SQL family and therefore some of their features differ from the on-premises SQL Server. Some of the important features that differ are as follows:
SQL Database
Conventional database backup and restore statements aren't supported on SQL Database. Backups are automatically scheduled and start within a few minutes of the database being provisioned. Backups are consistent, transaction-wise, which means that you can do a point-in-time restore.
There is no additional cost for backup storage until the amount stored goes beyond 100% of the database's size.
You can reduce the backup retention period to manage backup storage costs. You can also use the long-term retention period feature to store backups in a separate Azure blob container for a much lower cost for up to 10 years.
Apart from automatic backups, you can also export the Azure SQL Database bacpac or dacpac file to Azure Storage.
SQL Managed Instance
SQL Managed Instance automatically creates database backups that are kept for the duration of a specified retention period. Native COPY_ONLY backups on Azure blob containers are allowed on SQL Managed Instance.
Backup storage is free as an equal amount of storage is reserved for SQL Managed Instance, regardless of the backup retention period.
The long-term retention period for SQL Managed Instance is a limited preview feature currently (and will be in public preview soon) and is only available for EA and CSP subscriptions.
The default recovery model for SQL Database and SQL Managed Instance is FULL, and it can't be modified to any other recovery model as in on-premises recovery models.
The recovery model is set when the master database is created, meaning when an Azure SQL Server is provisioned, the recovery model can't be modified because the master database is read-only.
To view the recovery model of an Azure SQL Database, execute the following query:
SELECT name, recovery_model_desc FROM sys.databases;
You can use either of the two methods discussed earlier in the chapter to run the query – the Azure portal or SSMS.
You should get the following output:
Figure 1.32: Recovery model of an SQL database
SQL Database doesn't have SQL Server Agent, which is used to schedule jobs and send success/failure notifications. However, you can use the following workarounds:
Create an SQL Agent job on an on-premises SQL server or on an Azure VM to connect and run on the SQL Database.Azure Automation allows users to schedule jobs in Microsoft Azure to automate manual tasks. This topic is covered in detail later in the book.Elastic Jobs is an Azure service that allows the scheduled execution of ad hoc tasks. This topic is covered in detail later in the book.Use PowerShell to automate a task and schedule PowerShell script execution with Windows Scheduler, on-premises, or Azure VM.Azure SQL Managed Instance has SQL Server Agent and can be used to schedule jobs just like with on-premises SQL Server. However, some of the actions are not allowed, such as enabling and disabling SQL Server Agent, and the SQL Server Agent process is always in the running state.
For more information, please visit https://docs.microsoft.com/azure/azure-sql/managed-instance/transact-sql-tsql-differences-sql-server#sql-server-agent.
Change Data Capture (CDC) allows you to capture data modifications to CDC-enabled databases and tables. The CDC feature is important in incremental load scenarios, such as incrementally inserting changed data to a data warehouse from an online transaction processing (OLTP) environment. CDC requires SQL Server Agent and therefore isn't available in SQL Database. However, you can use the temporal table, SQL Server Integration Services (SSIS), or Azure Data Factory to implement CDC. CDC is supported in SQL Managed Instance.
Audit logs are available for both SQL Database and SQL Managed Instance but with a few differences from on-premises SQL Server. In PaaS, file system-level access is not granted, hence audit logs need to be captured on Azure Blob Storage.
You can't enable mirroring between two SQL Databases, and the same goes for SQL Managed Instance databases. You can set up a readable secondary for an SQL Database and a failover group for SQL Managed Instance, which is better than mirroring.
Table partitioning using a partition scheme and partition functions is allowed in SQL Database; however, because of the PaaS nature of the SQL Database, all partitions should be created on a primary filegroup. You won't get a performance improvement by having partitions on different disks (spindles); however, you will get a performance improvement with partition elimination.
In SQL Managed Instance, partitions can be created with different filegroups and files for each partition, meaning better performance by having multiple files per database.
Conventional replication techniques, such as snapshot, transactional, and merge replication, can't be done between two Azure SQL Databases. However, an SQL Database can be a subscriber to an on-premises or Azure VM SQL Server instance.
However, this too has limitations. It supports one-way transactional replication, not peer-to-peer or bi-directional replication; it supports only push subscription.
Note that you should have SQL Server 2012 or above when on-premises. Replication and distribution agents can't be configured on SQL Database.
SQL Managed Instance supports snapshot, transactional, and bi-directional transactional replication. Merge replication, peer-to-peer replication, and updatable subscriptions are not supported.
The publisher and distributor need to be configured on both SQL Managed Instance and on-premises SQL Server.
Multi-part names and cross-database queries are supported on SQL Managed Instance.
For SQL Database, three-part names (databasename.schemaname.tablename) are only limited to tempdb, wherein you access a temp table as tempdb.dbo.#temp. For example, if there is a temporary table, say, #temp1, then you can run the following query to select all the values from #temp1:
SELECT * FROM tempdb.dbo.#temp1
You can't access the tables in different SQL Databases in Azure on the same Azure SQL Server using three-part names. Four-part names (ServerName.DatabaseName.SchemaName.TableName) aren't allowed at all.
You can use an elastic query to access tables from different databases from an Azure SQL Server. Elastic queries are covered in detail later in the book. You can access objects in different schemas in the same Azure SQL Database using two-part (Schemaname.Tablename) names.
To explore other T-SQL differences, visit https://docs.microsoft.com/azure/sql-database/sql-database-transact-sql-information.
SQL Server Browser is a Windows service that provides instance and post information to incoming connection requests. This isn't required because SQL Database and SQL Managed Instance listen to port 1433 only.
SQL Database and SQL Managed Instance don't support FileStream or FileTable, just because of the PaaS nature of the service. There is a workaround to use Azure Storage; however, that would require a re-working of the application and the database.
SQL CLR is supported on SQL Managed Instance with a few differences. SQL CLR allows users to write programmable database objects such as stored procedures, functions, and triggers in managed code. This provides a significant performance improvement in some scenarios. This feature is not available in SQL Database.
Resource Governor is supported on SQL Managed Instance. Resource Governor allows you to throttle/limit resources (CPU, memory, and I/O) for different SQL Server workloads. This feature is not available in SQL Database.
SQL Database comes with different service tiers, each suitable for different workloads. You should evaluate the performance tier your application workload will fit into and accordingly provision the database for that performance tier.
Local and global instance-scoped temporary tables are supported on SQL Managed Instance.
Global temporary tables are defined by ## and are accessible across all sessions. These are not supported in SQL Database.
Local temporary tables are allowed. Global temporary tables created with ## are accessible across all sessions for a particular database. For example, a global temporary table created in database DB1 will be accessible to all sessions connecting to database DB1 only.
Log shipping is the process of taking log backups on a primary server and copying and restoring them on a secondary server. Log shipping is commonly used as a high-availability or disaster-recovery solution, or to migrate a database from one SQL instance to another. SQL Database and SQL Managed Instance have built-in high availability and configurable business continuity features. Log shipping isn't supported by SQL Database and SQL Managed Instance.
SQL Profiler is supported on SQL Managed Instance. SQL Trace and Profiler can't be used to trace events on SQL Database. Currently, there isn't a direct alternative other than using dynamic management views (DMVs), monitoring using the Azure portal, and extended events.
Only a limited set of global traces is supported on SQL Managed Instance. Trace flags are special switches used to enable or disable a particular SQL Server functionality. These are not available in SQL Server.
SQL Managed Instance supports nearly all system stored procedures. SQL Database doesn't support all the system stored procedures supported in an on-premises SQL Server. System stored procedures such as sp_addmessage, sp_helpuser, and sp_configure aren't supported. In a nutshell, procedures related to features unsupported in SQL Database aren't supported.
The USE statement is used to switch from one database context to another. This isn't supported in SQL Database, but SQL Managed Instance supports the USE statement.
In this exercise, we'll provision and connect to an SQL Managed Instance. We'll also learn about virtual network support in SQL Managed Instance.
To provision an SQL Managed Instance, perform the following steps:
Log in to https://portal.azure.com using your Azure credentials.Click on +Create a resource:Figure 1.33: Creating a new resource
Search for Azure SQL in Azure Marketplace:Figure 1.34: Searching for Azure SQL in Marketplace
Select the Create option; you can see more details by clicking on the Show details option:Figure 1.35: Selecting an SQL deployment option
In the Basic tab, provide the information shown in Figure 1.36:Figure 1.36: Information required to add the SQL Managed Instance
In the Subscription box, provide your Azure subscription type. SQL Managed Instances currently support the following subscription types: Enterprise Agreement (EA), Pay-As-You-Go, Cloud Service Provider (CSP), Enterprise Dev/Test, Pay-As-You-Go Dev/Test, and subscriptions with monthly Azure credit for Visual Studio subscribers.
If you have a different subscription, you won't be able to create an SQL Managed Instance.
In the Resource Group box, choose to create a new or use an existing resource group for the SQL Managed Instance. A resource group is a logical container for all the resources in Azure.
The Managed instance name box is for the name of the managed instance you plan to create. It can be any valid name, in accordance with the naming rules at https://docs.microsoft.com/azure/architecture/best-practices/naming-conventions.
For the Region box, select the desired Azure region for the SQL Managed Instance deployment. In general, apps and managed instances should be deployed in the same Azure region to avoid network latency.
The Managed instance admin login box is for any login name, as long as it fits the naming conventions at https://docs.microsoft.com/azure/architecture/best-practices/naming-conventions.
The password can be any password that follows these rules:
Figure 1.37: Password requirements
Select Configure Managed Instance to choose compute and storage resources. Use sliders to choose the vCore and storage size.Select Azure Hybrid Benefits if you already have SQL Server licenses with software assurance. In general, this can be used while migrating from an on-premises SQL Server to SQL Managed Instance.
Select the Backup Storage type; Geo, Zone, and Locally-redundant backup storage options are available. Choose this based on the desired recovery plan as it can't be changed after instance deployment. Read more about backup storage types at https://docs.microsoft.com/azure/azure-sql/database/automated-backups-overview?tabs=single-database.
When you are finished, select Apply to save changes:
Figure 1.38: Compute + storage
After the instance resources configuration selection, move to the Networking tab to configure Virtual network / subnet for the SQL Managed Instance:Figure 1.39: Moving to the Network tab to set Virtual network / subnet
The Virtual network / subnet box is for setting the virtual network/subnet that the managed instance will be part of. If no network is provided, a new virtual network/subnet is created.
For the Connection type box, SQL Managed Instances support two connection types: Redirect and Proxy. Redirect is the recommended connection type because the client directly connects to the node hosting the database, and therefore it offers low latency and high throughput.
For the Proxy connection type, requests to the database are proxied through the SQL Database gateways.
Enable Public endpoint to allow SQL Managed Instance connectivity over the internet. By default, it's disabled until explicitly enabled.
Choose Minimal TLS Version to enforce a TLS version for the managed instance's inbound connection.
Fill out all the details in the Additional settings tab:Figure 1.40: The Additional settings tab
Collation is the SQL Server collation that the managed instance will be in.
The Time zone box denotes the time zone of the managed instance. The preferred time zone is UTC; however, this will differ from business to business.
Select Geo-Replication to use this managed instance as a secondary instance in a failover group.
In the Review + create tab, review your selection before you create the managed instance:Figure 1.41: The Review + create tab
Click Create to validate and provision the SQL Managed Instance.To monitor the progress, click the Notifications (bell) icon in the top-left corner:
Figure 1.42: Notifications icon in the instance window
As we can see, the deployment is in progress:
Figure 1.43: The Notifications pane
After the deployment is complete, a deployment complete notification will come up in the notification window:
Figure 1.44: Deployment complete notification
This section discusses provisioning Azure SQL Server and SQL Database using PowerShell. To understand the process, let's take the example of Mike, who is the newest member of the data administration team at ToyStore Ltd., a company that manufactures toys for children. ToyStore has an e-commerce web portal that allows customers to purchase toys online. ToyStore has migrated the online e-commerce portal to Microsoft Azure and is therefore moving to Azure SQL Database from an on-premises SQL Server. Mike is asked to provision the Azure SQL Database and other required resources as his initial assignment. This can be achieved by following these steps:
If you are short of time, you can refer to the C:\Code\Chapter01\Provision-AzureSQLDatabase.ps1 file. You can run this file in the PowerShell console instead of typing the code as instructed in the following steps. Open a PowerShell console and enter the full path to execute the PowerShell script. You'll have to change the Azure resource group name, the Azure SQL Server, and the Azure SQL Database name in the script before executing it.
Save the Azure profile details into a file for future reference. Press Windows + R to open the Run command window.In the Run command window, type powershell and then press Enter. This will open a new PowerShell console window:Figure 1.45: Opening up PowerShell
In the PowerShell console, run the following command:Add-AzAccount
You'll have to enter your Azure credentials into the pop-up dialog box. After a successful login, the control will return to the PowerShell window.
Run the following command to save the profile details to a file:Save-AzProfile -Path C:\Code\MyAzureProfile.json
The Azure subscription details will be saved in the MyAzureProfile.json file in JSON format. If you wish to explore the JSON file, you can open it in any editor to review its content:
Figure 1.46: Saving the Azure credentials
Saving the profile in a file allows you to use the file to log in to your Azure account from PowerShell instead of providing your credentials every time in the Azure authentication window.
Press Windows + R to open the Run command window. Type PowerShell_ISE.exe in the Run command window and press Enter. This will open a new PowerShell ISE editor window. This is where you'll write the PowerShell commands:
Figure 1.47: Run command window
In the PowerShell ISE, select File from the top menu, and then click Save. Alternatively, you can press Ctrl + S to save the file. In the Save As dialog box, browse to the C:\Code\Chapter01\ directory. In the File name textbox, type Provision-AzureSQLDatabase.ps1, and then click Save to save the file:Figure 1.48: Saving the PowerShell ISE file
Copy and paste the following lines in the Provision-AzureSQLDatabase.ps1 file one after another. The code explanation, wherever required, is given in the comments within the code snippet.Copy and paste the following code to define the parameters:param (
[parameter(Mandatory=$true)] [String] $ResourceGroup, [parameter(Mandatory=$true)] [String] $Location, [parameter(Mandatory=$true)] [String] $SQLServer, [parameter(Mandatory=$true)] [String] $UserName, [parameter(Mandatory=$true)] [String] $Password,
[parameter(Mandatory=$true)] [String] $SQLDatabase, [parameter(Mandatory=$true)] [String] $Edition="Basic", [parameter(Mandatory=$false)] [String] $AzureProfileFilePath
)
The preceding code defines the parameters required by the scripts:
ResourceGroup: The resource group that will host the logical Azure SQL Server and Azure SQL Database.
Location: The resource group location. The default is East US 2.
SQLServer: The logical Azure SQL Server name that will host the Azure SQL Database.
UserName: The Azure SQL Server admin username. The default username is sqladmin. Don't change the username; keep it as the default.
Password: The Azure SQL Server admin password. The default password is Packt@pub2. Don't change the password; keep it as the default.
SQLDatabase: The Azure SQL Database to create.
Edition: The Azure SQL Database edition. This is discussed in detail in Chapter 3, Migration.
AzureProfileFilePath: The full path of the file that contains your Azure profile details. You created this earlier in the activity.
Copy and paste the following code to log in to your Azure account from PowerShell: