sql azure 语法_如何使用Azure门户,Cloud Shell和T-SQL复制Azure SQL数据库

sql azure 语法

This article will provide an overview covering programmatically moving databases on the Azure Portal while avoiding common problems with users and logins.

本文将概述如何在Azure门户上以编程方式移动数据库,同时避免用户和登录常见问题。

介绍 (Introduction)

Sometimes we need to create copies of a database when we migrate a database from our testing environment to Production or maybe when we want to create a new database based on a template database.

有时,当我们将数据库从测试环境迁移到生产环境时,或者当我们想要基于模板数据库创建新数据库时,我们需要创建数据库的副本。

Or sometimes to test the database and make some experiments without affecting the production database or to have a replica to reduce the overhead or for security reasons.

或者有时可以测试数据库并进行一些实验,而不会影响生产数据库,或者具有副本以减少开销或出于安全原因。

In Azure, it is possible to create Azure SQL database copies between Azure SQL Servers. If you have an Azure AD authentication, it can be a straight forward process. However, if you use a SQL Authentication, you may require additional steps. In this article, we will show some tips to migrate Azure SQL logins and users between different Azure SQL Servers using the portal, Cloud Shell and T-SQL.

在Azure中,可以在Azure SQL服务器之间创建Azure SQL数据库副本。 如果您具有Azure AD身份验证,则可能是一个简单的过程。 但是,如果您使用SQL身份验证,则可能需要其他步骤。 在本文中,我们将显示一些使用门户,Cloud Shell和T-SQL在不同Azure SQL Server之间迁移Azure SQL登录名和用户的提示。

In this article, we will show how to do the following tasks using the Azure Portal:

在本文中,我们将展示如何使用Azure门户执行以下任务:

  1. Copy a database using the Azure Portal

    使用Azure门户复制数据库
  2. How to migrate logins from one Azure SQL Server to another (common problems) using the Azure Portal

    如何使用Azure门户将登录名从一个Azure SQL Server迁移到另一个(常见问题)
  3. Show how to avoid login migration problems using the Azure Portal

    显示如何使用Azure门户避免登录迁移问题
  4. How to copy an Azure SQL database using the Cloud Shell using the Azure Portal

    如何使用Azure门户使用Cloud Shell复制Azure SQL数据库
  5. How to copy an Azure SQL database using T-SQL using the Azure Portal

    如何使用Azure门户使用T-SQL复制Azure SQL数据库

要求 (Requirements)

  1. Azure Portal Azure门户
  2. An Azure SQL Source Database

    Azure SQL源数据库
  3. An Azure SQL target database

    Azure SQL目标数据库
  4. A local SQL Server Management Studio (SSMS)

    本地SQL Server Management Studio(SSMS)

开始吧 (Get started)

如何使用Azure门户复制数据库 (How to copy a database using the Azure Portal)

We will first copy a database with a login from a source Azure SQL Server to another.

我们首先将具有登录名的数据库从源Azure SQL Server复制到另一个数据库。

In this example, we have a source database named sqlshackdb stored in the server sqlshackserver:

在此示例中,我们在服务器sqlshackserver中存储了一个名为sqlshackdb的源数据库:

Azure Portal Overview

You can search the SQL Servers in Azure by clicking on the > icon and selecting SQL servers in the Azure Portal:

您可以通过单击>图标并在Azure门户中选择SQL服务器来搜索Azure中SQL Server:

/wp-content/uploads/2017/06/word-image-7.jpeg

Our Azure SQL Server name is sqlshackdestination. This will be the destination Server where we will create a copy of the source database:

我们的Azure SQL Server名称是sqlshackdestination。 这将是目标服务器,我们将在其中创建源数据库的副本:

SQL Server in Azure Portal

We will create a login to verify common problems when we copy an Azure SQL Database to another Azure SQL Server using the Azure Portal. To do this, we will enable the firewall rule to connect with a local SSMS:

当我们使用Azure门户将Azure SQL数据库复制到另一个Azure SQL Server时,我们将创建一个登录来验证常见问题。 为此,我们将启用防火墙规则以与本地SSMS连接:

Firewall control in Azure Portal

In SSMS, connect to a Database Engine:

在SSMS中,连接到数据库引擎:

/wp-content/uploads/2017/06/word-image-10.jpeg

Specify your credentials to connect to the Azure SQL Source database:

指定您的凭据以连接到Azure SQL Source数据库:

/wp-content/uploads/2017/06/word-image-11.jpeg

The following code will create an Azure SQL Login named “psmith” and a database user associated to that login:

以下代码将创建一个名为“ psmith”的Azure SQL登录名以及与该登录名关联的数据库用户:

CREATE LOGIN psmith
WITH PASSWORD = 'ThisIsmySupperstrongpwd!#' 
GO
 
CREATE USER psmith
FOR LOGIN psmith
WITH DEFAULT_SCHEMA = dbo
GO

In the Azure Portal, select the database and press the copy icon:

在Azure门户中,选择数据库,然后按复制图标:

/wp-content/uploads/2017/06/word-image-12.jpeg

Specify the Target server. The name of the database copy. You can also choose the price tier for the new database and press OK:

指定目标服务器。 数据库副本的名称。 您还可以为新数据库选择价格等级,然后按确定:

/wp-content/uploads/2017/06/word-image-13.jpeg

If everything is OK, a copy of the database should be created:

如果一切正常,则应创建数据库的副本:

/wp-content/uploads/2017/06/word-image-14.jpeg

如何使用Azure Portal将登录名从一个Azure SQL Server迁移到另一个(常见问题) (How to migrate logins from one Azure SQL Server to another (common problems) using Azure Portal)

To connect to the target server using SSMS, you will need to enable the firewall:

要使用SSMS连接到目标服务器,您将需要启用防火墙:

/wp-content/uploads/2017/06/word-image-15.jpeg

If we check the logins in the source and destination, we will notice that only the database owner was migrated, but not the login “jsmith”:

如果我们检查源和目标中的登录名,则会注意到仅迁移了数据库所有者,而未迁移“ jsmith”登录名:

/wp-content/uploads/2017/06/word-image-16a.jpeg

This is because the logins are stored in the master database. That is why, if you need to migrate the logins, you may need to migrate them separately.

这是因为登录名存储在master数据库中。 因此,如果您需要迁移登录名,则可能需要分别迁移它们。

The following query can generate scripts to create the logins manually in Source Server and in the master database:

以下查询可以生成脚本以在Source Server和master数据库中手动创建登录名:

 
  SELECT 'create login '+name +' with password =''Mypws1234!!''' from sys.sql_logins
where type='s'

Basically, the T-SQL query will generate CREATE LOGIN sentences with the same password for each user. The query shows all the logins of type SQL Server (type=’s’).

基本上,T-SQL查询将为每个用户生成具有相同密码的CREATE LOGIN语句。 该查询显示所有SQL Server类型的登录(类型='s')。

You will need to copy the results of the query and execute the sentences in the target server to generate the logins.

您将需要复制查询结果并在目标服务器中执行语句以生成登录名。

To list the SQL Server logins, you can run the following query in the master database:

若要列出SQL Server登录名,可以在master数据库中运行以下查询:

select * from sys.sql_logins

Note that the option MUST_CHANGE used to change the password is not supported in Azure SQL.

请注意,Azure SQL不支持用于更改密码的选项MUST_CHANGE。

To avoid this problem, you can create contained database users. In the next section, we will show how to do it.

为避免此问题,您可以创建包含的数据库用户。 在下一节中,我们将展示如何做到这一点。

如何创建包含的用户并避免登录迁移问题 (How to create contained users and avoid login migration problems)

In a contained database, the database users can login directly without a SQL login. You can have a database user with a password. If you create contained users, when you copy a database from a source to a destination, you do not need extra effort to copy the logins separately.

在一个包含的数据库中,数据库用户可以直接登录而无需SQL登录。 您可以使数据库用户具有密码。 如果创建包含的用户,则在将数据库从源复制到目标时,不需要额外的精力来分别复制登录名。

The following example will show how to create a database user without a traditional login in the master database:

以下示例将显示如何在主数据库中创建没有传统登录名的数据库用户:

CREATE USER containeduser WITH PASSWORD = 'ThisIsmySupperstrongpwd!#';

As you can see, the login is not required with this option. The database user includes a password. You can optionally add it to a database role if necessary. The disadvantage with a contained user is that if you cannot assign

如您所见,此选项不需要登录。 数据库用户包括密码。 如有必要,您可以选择将其添加到数据库角色。 包含用户的缺点是,如果您无法分配

If you create a copy of the database as we did in figure 7 and 8, you will have a new database, but now you will have a user named “containeduser” that can login directly:

如果像图7和8一样创建数据库的副本,您将拥有一个新数据库,但是现在您将拥有一个名为“ containeduser”的用户,可以直接登录:

/wp-content/uploads/2017/06/word-image-17a.jpeg

To login with a database contained user, go to SSMS and specify the Azure SQL Server name and login credentials:

若要使用包含的数据库用户登录,请转到SSMS并指定Azure SQL Server名称和登录凭据:

/wp-content/uploads/2017/06/word-image-18a.jpeg

You will need to specify the database. Press the Options >> button and in the Connect to database combo box, specify the database name:

您将需要指定数据库。 按选项>>按钮,然后在“连接到数据库”组合框中,指定数据库名称:

/wp-content/uploads/2017/06/word-image-19.jpeg

如何在Azure Portal中使用Cloud Shell复制Azure SQL数据库 (How to copy an Azure SQL database using the Cloud Shell in Azure Portal)

If you want to automate administrative tasks, the Cloud Shell will help you a lot. Now, you can run bash to automate your tasks and this year you will also be able to run PowerShell using the console. With bash, you can automate tasks using the command line. As we explained in a previous chapter, you do not need to install the Azure Client in the local machine to run the Cloud Shell. Azure now allows you to run the Shell using the Azure Portal.

如果您想自动化管理任务,Cloud Shell将为您提供很多帮助。 现在,您可以运行bash来自动化您的任务,今年您还可以使用控制台运行PowerShell。 使用bash,您可以使用命令行自动执行任务。 如前一章所述,您无需在本地计算机上安装Azure客户端即可运行Cloud Shell。 Azure现在允许您使用Azure门户运行命令行管理程序。

To test this feature, press the >_ icon:

要测试此功能,请按> _图标:

/wp-content/uploads/2017/06/word-image-20a.jpeg

You will connect to the Cloud Shell:

您将连接到Cloud Shell:

/wp-content/uploads/2017/06/word-image-21.jpeg

In the Cloud Shell, we will create a copy of the Azure SQL database using the following commands:

在Cloud Shell中,我们将使用以下命令创建Azure SQL数据库的副本:

az sql db copy –dest-name azdbcopy -n sqlshackdb -g mynewgp -s sqlshackserver –dest-resource-group group1 –dest-server sqlshackdestination

az sql数据库副本–目标名称azdbcopy -n sqlshackdb -g mynewgp -s sqlshackserver –dest-resource-group group1 –dest-server sqlshackdestination

az sql db copy is the command to copy an Azure SQL database.

az sql db copy是用于复制Azure SQL数据库的命令。

Azdbcopy is the name of the copy in the destination database.

Azdbcopy是目标数据库中副本的名称。

Sqlshackdb is the source database to be copied

Sqlshackdb是要复制的源数据库

Mynewgp is the name of the source resource group where the source Azure sql database was created.

Mynewgp是在其中创建源Azure sql数据库的源资源组的名称。

Sqlshackserver is the name of the Azure SQL Server Source.

Sqlshackserver是Azure SQL Server源的名称。

Group1 is the name of the target resource group.

Group1是目标资源组的名称。

Finally, sqlshackdestination is the name of the target Azure SQL Server where we will create the database copy.

最后, sqlshackdestination是我们将在其中创建数据库副本的目标Azure SQL Server的名称。

If everything is OK, a destination database named “sqlshackdestination” will be displayed:

如果一切正常,将显示一个名为“ sqlshackdestination”的目标数据库:

/wp-content/uploads/2017/06/word-image-22.jpeg

如何使用T-SQL复制Azure SQL数据库 (How to copy an Azure SQL database using T-SQL)

You can also create a database copy using T-SQL. The following example shows how to create the copy in SSMS. You can run this sentence in the master database:

您也可以使用T-SQL创建数据库副本。 以下示例显示了如何在SSMS中创建副本。 您可以在master数据库中运行以下语句:

CREATE DATABASE tsqldatabasecopy AS COPY OF sqlshackserver.sqlshackdb

Where tsqldatabasecopy is the destination database and sqlshackserver.sqlshackdb is the name of the Azure SQL Server and the Azure SQL Database name separated by a “.”.

其中,tsqldatabasecopy是目标数据库,而sqlshackserver.sqlshackdb是Azure SQL Server的名称,Azure SQL数据库的名称以“。”分隔。

You can verify that the new database was created successfully in SSMS or the Azure Portal:

您可以验证新数据库是否已在SSMS或Azure门户中成功创建:

/wp-content/uploads/2017/06/word-image-23.jpeg

结论 (Conclusion)

In this article, we learned how to create a copy of an Azure SQL database using the Azure Portal, the Cloud Shell and T-SQL.

在本文中,我们学习了如何使用Azure门户,Cloud Shell和T-SQL创建Azure SQL数据库的副本。

We also learned how to create contained users to avoid the problems to migrate logins from one Azure SQL Server to another. We also create a query to generate code to create Azure SQL logins to manually create logins in the destination Server.

我们还学习了如何创建包含的用户,以避免将登录名从一个Azure SQL Server迁移到另一个Azure SQL Server的问题。 我们还创建查询以生成代码以创建Azure SQL登录名,以便在目标服务器中手动创建登录名。

The Azure Portal is a powerful and effective medium to manage your SQL Azure databases. I hope this article helps you understand how to utilize it more effectively, for common tasks like moving databases.

Azure门户是管理SQL Azure数据库的强大而有效的媒介。 我希望本文能帮助您了解如何有效地利用它来完成诸如移动数据库之类的常见任务。

Other articles in this series:

本系列的其他文章:

翻译自: https://www.sqlshack.com/copy-azure-sql-database-using-azure-portal-cloud-shell-t-sql/

sql azure 语法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值