{"id":1284,"date":"2022-05-09T11:56:49","date_gmt":"2022-05-09T18:56:49","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=1284"},"modified":"2022-05-09T12:03:27","modified_gmt":"2022-05-09T19:03:27","slug":"create-and-connect-to-an-azure-sql-db","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/create-and-connect-to-an-azure-sql-db\/","title":{"rendered":"Create and connect to an Azure SQL DB in 6 easy steps"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2022\/05\/pexels-pixabay-256302-1440.jpg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-1285\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2022\/05\/pexels-pixabay-256302-1440.jpg\" alt=\"Image pexels pixabay 256302 1440\" width=\"1440\" height=\"964\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2022\/05\/pexels-pixabay-256302-1440.jpg 1440w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2022\/05\/pexels-pixabay-256302-1440-300x201.jpg 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2022\/05\/pexels-pixabay-256302-1440-1024x686.jpg 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2022\/05\/pexels-pixabay-256302-1440-768x514.jpg 768w\" sizes=\"(max-width: 1440px) 100vw, 1440px\" \/><\/a><\/p>\n<p>There are many ways in which an Azure SQL database can be created, and if you are new to the development all those different options can be quite intimidating. Let me show you, in just a few steps, how easy it is instead. This is what we are going to do:<\/p>\n<ol>\n<li>Create a resource group<\/li>\n<li>Create an Azure SQL logical server<\/li>\n<li>Create an Azure SQL database<\/li>\n<li>Configure the firewall<\/li>\n<li>Create a user<\/li>\n<li>Get the connection string<\/li>\n<\/ol>\n<p>I&#8217;ll be using the Azure Shell portal, as it provides a full experience without the need to install anything on your machine. The only thing you must have ready, is an Azure subscription.<\/p>\n<p>Open a browser and point to\u00a0<a href=\"https:\/\/shell.azure.com\/\">https:\/\/shell.azure.com<\/a>\u00a0and let&#8217;s get started.<\/p>\n<h2><a href=\"https:\/\/dev.to\/azure\/create-and-connect-to-an-azure-sql-db-9k0#create-a-resource-group\" name=\"create-a-resource-group\"><\/a>Create a Resource Group<\/h2>\n<p>A\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-resource-manager\/management\/manage-resource-groups-portal#what-is-a-resource-group\">resource group<\/a>\u00a0is a container of Azure resources. It is needed as it simplifies quite a lot the management of those resources. Let&#8217;s create one in the East US region, and name it\u00a0<code>dev-demo<\/code>:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>az group create <span class=\"nt\">-n<\/span> dev-demo <span class=\"nt\">-l<\/span> eastus\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<h2><a href=\"https:\/\/dev.to\/azure\/create-and-connect-to-an-azure-sql-db-9k0#create-an-azure-sql-logical-server\" name=\"create-an-azure-sql-logical-server\"><\/a>Create an Azure SQL logical server<\/h2>\n<p>An Azure SQL\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-sql\/database\/logical-servers?view=azuresql\">logical server<\/a>\u00a0is needed to make it easier to manage many databases. A logical server administrator can automatically access any database in the server. Let&#8217;s create a server named\u00a0<code>dev-demo-sql-srv<\/code>\u00a0and create an administrator. The administrator user should\u00a0<em>NEVER<\/em>\u00a0be used to allow applications to connect to any database hosted in that server. It is for administrative tasks only.<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>az sql server create <span class=\"nt\">-g<\/span> dev-demo <span class=\"nt\">-n<\/span> dev-demo-sql-srv <span class=\"nt\">--admin-user<\/span> devdemoadmin <span class=\"nt\">--admin-password<\/span> SomeVery_STRONG_Passw0rd! \r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<h2><a href=\"https:\/\/dev.to\/azure\/create-and-connect-to-an-azure-sql-db-9k0#create-an-azure-sql-database\" name=\"create-an-azure-sql-database\"><\/a>Create an Azure SQL database<\/h2>\n<p>Now that you have a server, you can create databases in it. The database will be named\u00a0<code>db1<\/code>\u00a0and it will be using a\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-sql\/database\/serverless-tier-overview?view=azuresql\">serverless<\/a>\u00a0(I know&#8230;it&#8217;s fun. We had to create a server before&#8230;) model, so that you&#8217;ll pay for it only when you use it (auto pause is set by default after 1 hour of no activity):<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>az sql db create <span class=\"nt\">-g<\/span> dev-demo <span class=\"nt\">-n<\/span> db1 <span class=\"nt\">-s<\/span> dev-demo-sql-srv <span class=\"nt\">--service-objective<\/span> GP_S_Gen5_2\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<h2><a href=\"https:\/\/dev.to\/azure\/create-and-connect-to-an-azure-sql-db-9k0#configure-the-firewall\" name=\"configure-the-firewall\"><\/a>Configure the firewall<\/h2>\n<p>If you plan to use the created database with some other Azure services (like, for example, Azure Functions or Web Apps or Containers) and you don&#8217;t have extremely high security policies, you can allow Azure services to connect to your database by creating this firewall rule:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>az sql server firewall-rule create <span class=\"nt\">-g<\/span> dev-demo <span class=\"nt\">-s<\/span> dev-demo-sql-srv <span class=\"nt\">-n<\/span> AllAzureServices--start-ip-address 0.0.0.0 <span class=\"nt\">--end-ip-address<\/span> 0.0.0.0\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<p>Now you need to allow\u00a0<em>your<\/em>\u00a0development machine to connect to Azure SQL. Since you&#8217;re using the cloud shell, this is really not needed for now, but I&#8217;m pretty sure at some point you&#8217;ll want to use\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/azure-data-studio\/download-azure-data-studio?view=sql-server-ver15\">Azure Data Studio<\/a>\u00a0or\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/ssms\/download-sql-server-management-studio-ssms?view=sql-server-ver15\">SQL Server Management Studio<\/a>\u00a0to connect and manage the database from your machine, and so you need to make sure the firewall will allow such connection. Open a browser and go to\u00a0<a href=\"https:\/\/ipinfo.io\/\">https:\/\/ipinfo.io\/<\/a>\u00a0and get your IP address, and then create a firewall rule using the reported IP for start and end address:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>az sql server firewall-rule create <span class=\"nt\">-g<\/span> dev-demo <span class=\"nt\">-s<\/span> dev-demo-sql-srv <span class=\"nt\">-n<\/span> MyIP <span class=\"nt\">--start-ip-address<\/span> 12.34.56.78 <span class=\"nt\">--end-ip-address<\/span> 12.34.56.78\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<h2><a href=\"https:\/\/dev.to\/azure\/create-and-connect-to-an-azure-sql-db-9k0#create-a-database-user\" name=\"create-a-database-user\"><\/a>Create a database user<\/h2>\n<p>You learned before that the administrative account should never be used to allow applications we create or work with to connect to the database, so we need to create a dedicated user. It&#8217;s easy. You need to connect to Azure SQL database using the\u00a0<code>sqlcmd<\/code>\u00a0tool (as you notice you&#8217;ll be using the administrator login here):<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>sqlcmd <span class=\"nt\">-S<\/span> dev-demo-sql-srv.database.windows.net <span class=\"nt\">-d<\/span> db1 <span class=\"nt\">-U<\/span> devdemoadmin <span class=\"nt\">-P<\/span> SomeVery_STRONG_Passw0rd!\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<p>once you are logged in into the database &#8211; you&#8217;ll see a\u00a0<code>1&gt;<\/code>\u00a0prompt, you can create a user:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight sql\"><code><span class=\"k\">create<\/span> <span class=\"k\">user<\/span> <span class=\"p\">[<\/span><span class=\"n\">app<\/span><span class=\"o\">-<\/span><span class=\"k\">user<\/span><span class=\"p\">]<\/span> <span class=\"k\">with<\/span> <span class=\"n\">password<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'4pplication_Passw0rd!'<\/span><span class=\"p\">;<\/span>\r\n<span class=\"k\">go<\/span>\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<p>Once the user has been created, you need to give it enough permission to work with the data in the database. If it just needs to read and write from tables, you can assign it the\u00a0<code>db_reader<\/code>\u00a0and\/or\u00a0<code>db_writer<\/code>\u00a0roles. If it also needs to create tables or objects, you may want to add it to the\u00a0<code>db_owner<\/code>\u00a0role:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight sql\"><code><span class=\"k\">alter<\/span> <span class=\"k\">role<\/span> <span class=\"p\">[<\/span><span class=\"n\">db_owner<\/span><span class=\"p\">]<\/span> <span class=\"k\">add<\/span> <span class=\"n\">member<\/span> <span class=\"p\">[<\/span><span class=\"n\">app<\/span><span class=\"o\">-<\/span><span class=\"k\">user<\/span><span class=\"p\">]<\/span>\r\n<span class=\"k\">go<\/span>\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<p>You can exit from the\u00a0<code>sqlcmd<\/code>\u00a0prompt just by executing the\u00a0<code>quit<\/code>\u00a0command.<\/p>\n<blockquote><p>Please note that users in the\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/security\/authentication-access\/database-level-roles?view=sql-server-ver15\">db_owner group<\/a>\u00a0are basically local administrator so they can do pretty much everything on the database. Security is a complex &#8211; but extremely important &#8211; topic, make sure you check the basics out here:\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-sql\/database\/security-overview?view=azuresql\">An overview of Azure SQL Database and SQL Managed Instance security capabilities<\/a><\/p><\/blockquote>\n<h2><a href=\"https:\/\/dev.to\/azure\/create-and-connect-to-an-azure-sql-db-9k0#get-the-connection-string\" name=\"get-the-connection-string\"><\/a>Get the connection string<\/h2>\n<p>To get the connection string that you need to use in your application to connect to Azure SQL, you can just use the following AZ command, where you can also specify for which language or library you want to connection string:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>az sql db show-connection-string <span class=\"nt\">-s<\/span> dev-demo-sql-srv <span class=\"nt\">-c<\/span> ado.net\r\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\"><\/div>\n<\/div>\n<\/div>\n<p>The command will print out the connection string. You just have to replace the values in the angular brackets, for example\u00a0<code>&lt;databasename&gt;<\/code> with your values. Make sure to use the application user you created in the step above, and you&#8217;ll be good to go!<\/p>\n<h2><a href=\"https:\/\/dev.to\/azure\/create-and-connect-to-an-azure-sql-db-9k0#want-to-learn-more\" name=\"want-to-learn-more\"><\/a>Want to learn more?<\/h2>\n<p>If you want to learn what are the other ways (using the Portal, Powershell, or AZ CLI), here you can find a detailed article:\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-sql\/database\/single-database-create-quickstart?view=azuresql&amp;tabs=azure-portal\">Quickstart: Create an Azure SQL Database single database<\/a><\/p>\n<p>And if you are wondering why you should be looking at Azure SQL, this post is for you:\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/10-reasons-to-use-azure-sql-in-your-next-project\/\">10 reasons to use Azure SQL in your next project<\/a><\/p>\n<hr \/>\n<p>Photo by <a href=\"https:\/\/www.pexels.com\/photo\/close-up-view-of-plasma-256302\/\">Pixabay<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many ways in which an Azure SQL database can be created, and if you are new to the development all those different options can be quite intimidating. Let me show you, in just a few steps, how easy it is instead. This is what we are going to do: Create a resource group [&hellip;]<\/p>\n","protected":false},"author":24720,"featured_media":1285,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[512,513],"class_list":["post-1284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","tag-connect","tag-tutorial"],"acf":[],"blog_post_summary":"<p>There are many ways in which an Azure SQL database can be created, and if you are new to the development all those different options can be quite intimidating. Let me show you, in just a few steps, how easy it is instead. This is what we are going to do: Create a resource group [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/1284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/24720"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=1284"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/1285"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}