azure 使用

介绍 ( Introduction )

The sqlcmd is a very powerful tool to run SQL Server scripts, T-SQL commands. It is also very useful in disaster recovery situations like restoring the master database.

sqlcmd是运行SQL Server脚本(T-SQL命令)的非常强大的工具。 在灾难恢复情况下(例如还原主数据库),它也非常有用。

The sqlcmd can also be used in the Cloud, specifically with SQL Server Azure. In this new article, we will describe how to connect from a local machine to an Azure Virtual Machine (VM) with SQL Server Installed using sqlcmd.

sqlcmd也可以在云中使用,尤其是在SQL Server Azure中。 在这篇新文章中,我们将介绍如何使用安装了sqlcmdSQL Server从本地计算机连接到Azure虚拟机(VM)。

要求 ( Requirements )

You can watch the requirements configured step by step in my article related to migrate a database to Azure.

您可以观看我的文章中逐步配置的要求,这些要求与将数据库迁移到Azure有关。

  • An Azure VM with SQL Server installed.安装了SQL Server的Azure VM。
  • The port 1433 enabled if the default instance is used or the port of the instance used.如果使用默认实例或使用实例的端口,则启用端口1433。
  • The Endpoint enabled for the Azure VM.为Azure VM启用了端点。
  • The remote subnet enabled for our machine. 为我们的机器启用了远程子网。
  • Enable the SQL authentication and create a database user.启用S​​QL身份验证并创建数据库用户。
  • Adventureworks database installed.Adventureworks数据库 。

入门 ( Getting started )

如何使用sqlcmd连接到Azure机器 ( How to connect to an Azure Machine with sqlcmd )

  1. First of all, connect to an Azure VM with SQL Server. To do this, go to the command line and write this:

    首先,使用SQL Server连接到Azure VM。 为此,请转到命令行并输入以下内容:

    sqlcmd -U daniel -S sqlshack.cloudapp.net -P ”yourpassword” -d master

    sqlcmd -U丹尼尔-S sqlshack.cloudapp.net -P“ yourpassword” -d管理员

    The command runs the sqlcmd executable, with the SQL user Daniel, with the SQL Servername sqlshack.cloudapp.net with the password of your user and connecting to the system database master.

    该命令使用SQL用户Daniel和SQL Server名称sqlshack.cloudapp.net(使用您的用户密码)运行sqlcmd可执行文件,并连接到系统数据库主数据库。

如何使用sqlcmd在Azure中创建数据库 ( How to create a database in Azure with sqlcmd )

  1. Now let’s start creating a simple database. You can do that by using the create database command.
    1> create database sqlshack
    2> go
    You can verify the database created using the SQL Server Management Studio.

    现在让我们开始创建一个简单的数据库。 您可以使用create database命令来实现。
    1>创建数据库 sqlshack
    2>走
    您可以验证使用SQL Server Management Studio创建的数据库。

    Figure 1. The sqlshack database installed.

    图1. 安装 sqlshack 数据库。

  2. Alternatively, you could verify using the sp_databases stored procedure.
    1> sp_databases
    2> go

    或者,您可以使用sp_databases存储过程进行验证。
    1> sp_databases
    2>走

如何使用命令行在本地Azure上运行脚本 ( How to run a script using the command line locally to Azure )

  1. Another very useful parameter is the –i which is the input parameter. We can run sql server scripts using this parameter. In order to test it, let’s quit the sqlcmd.1> quit
  2. 另一个非常有用的参数是–i,它是输入参数。 我们可以使用此参数运行sql服务器脚本。 为了测试它,让我们退出sqlcmd。 1>退出
  3. Now, we will create a script called backupsqlshack.sql with this content.

    现在,我们将使用此内容创建一个名为backupsqlshack.sql的脚本。

    
    BACKUP DATABASE [sqlshack]
    TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\sqlshack.bak'
    WITH NOFORMAT, NOINIT,  NAME = N'sqlshack-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,
    STATS = 10
    GO
  4. Run the scripts with the following command line:

    使用以下命令行运行脚本:

    sqlcmd -U daniel -S sqlshack.cloudapp.net -P ”yourpassword” -d master -i “c:/scripts/backupsqlshack.sql”

    sqlcmd -U丹尼尔-S sqlshack.cloudapp.net -P“ yourpassword” -d master -i“ c:/scripts/backupsqlshack.sql”

    The command line with execute the sqlcmd with the Azure credential for the SQL database and run the backupsqlshack.sql script which will create a backup for the sqlshack database. You may need to modify the path of the backupsqlshack.sql and the patch of the backup in the .sql file.

    使用命令行执行带有SQL数据库的Azure凭据的sqlcmd并运行backupsqlshack.sql脚本,该脚本将为sqlshack数据库创建备份。 您可能需要修改.sql文件中的backupsqlshack.sql路径和备份补丁。

  5. In order to verify that the backup of the VM Azure machine was created, connect to your Azure VM.

    为了验证是否已创建VM Azure计算机的备份,请连接到您的Azure VM。

    Figure 2. Connecting to an Azure VM

    图2.连接到Azure VM

  6. Look for the path of the .bak file of the path specified in step 2.

    查找在步骤2中指定的路径的.bak文件的路径。

    Figure 3. The backup created on the VM

    图3.在VM上创建的备份

  7. 如何双击备份 ( How to backup with a double click )

    1. We learned how to create a backup en the section above. Now let’s create a batch file in the Windows Desktop to run the sqlcmd commands.我们在上一节中了解了如何创建备份。 现在,让我们在Windows桌面中创建一个批处理文件以运行sqlcmd命令。
    2. In .bat file named automaticbackup.bat write this:

      在名为自动备份.bat的.bat文件中,编写以下代码:

      sqlcmd -U daniel -S sqlshack.cloudapp.net -P “yourpassword” -d master -i “c:/scripts/backupsqlshack.sql”

      sqlcmd -U丹尼尔-S sqlshack.cloudapp.net -P“ yourpassword” -d master -i“ c:/scripts/backupsqlshack.sql”

      I am assuming that you already have the backupsqlshack.sql script created on the step 2 of the How to run a script using the command line locally to Azure section.

      我假设您已经在“如何在Azure本地使用命令行运行脚本”部分的第2步中创建了backupsqlshack.sql脚本。

    3. Save the automaticbackup.bat file in your local machine desktop and double click on it.

      将automaticbackup.bat文件保存在本地计算机桌面中,然后双击它。

      Figure 4. The batch file to automatically backup a SQL Server in an Azure VM

      图4.用于在Azure VM中自动备份SQL Server的批处理文件

    4. 如何使用本地sqlcmd到Azure将T-SQL结果保存在输出文件中 ( How to save your T-SQL results in an output file using your local sqlcmd to Azure )

      1. The –o parameter helps you to store the output in a file. The following example shows you how to check the fragmentation percentage of a file. For this example, we need 2 things. The database ID, the object ID. I will use a table from the Adventureworks Database which contains some index and data already created. Alternatively, you can use your own tables with indexes if you already have some created.-o参数可帮助您将输出存储在文件中。 以下示例显示了如何检查文件的碎片百分比。 对于此示例,我们需要两件事。 数据库ID,对象ID。 我将使用Adventureworks数据库中的表,其中包含一些已创建的索引和数据。 或者,如果已经创建了一些表,则可以使用自己的表和索引。
      2. First of all, create a script named fragmentationpercentage.sql that retrieves the information of your percentage of fragmentation:

        首先,创建一个名为fragmentationpercentage.sql的脚本,该脚本检索您的碎片百分比信息:

        
        DECLARE @db_id SMALLINT;
        DECLARE @object_id INT;
        set @db_id= DB_ID(N'AdventureWorks2014');
        SET @object_id = OBJECT_ID(N'AdventureWorks2014.Person.Address');SELECT index_id,avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats(@db_id, @object_id, NULL, NULL , 'LIMITED');GO

        The sys.dm_db_index_physical_stats function requires two values:

        sys.dm_db_index_physical_stats函数需要两个值:

        • The Database ID, which can be retrieved using the DB_ID() function. This function gives you the ID of a specified database name. In this example, the Adventureworks2014 database.数据库ID,可以使用DB_ID()函数进行检索。 此函数为您提供指定数据库名称的ID。 在此示例中,Adventureworks2014数据库。
        • The OBJECT_ID() functions returns de ID of an specified object. In this example, the Person.Address table is the object.

          OBJECT_ID()函数返回指定对象的ID。 在此的示例Person.Address表是对象。

        The next step is to use the function specified the database id and object id.

        下一步是使用指定数据库ID和对象ID的函数。

      3. Once we have all ready, let’s invoke the script and store the results in a file name results.txt:

        准备就绪后,让我们调用脚本并将结果存储在文件名results.txt中:

        sqlcmd -U daniel -S sqlshack.cloudapp.net -P “yourpassword” -d master -i “c:\scripts\fragmentationpercentage.sql” -o “c: \scripts\results.txt”

        sqlcmd -U丹尼尔-S sqlshack.cloudapp.net -P“ yourpassword” -d master -i“ c:\ scripts \ fragmentationpercentage.sql” -o“ c:\ scripts \ results.txt”

        The command line connects to Azure and executes the fragmentationpercentage.sql script and stores the results in the results.txt file. This file is stored locally.

        命令行连接到Azure并执行fragmentationpercentage.sql脚本,并将结果存储在results.txt文件中。 该文件存储在本地。

      4. In order to verify the file, in your local machine go to the path used in the step 3 (in my example c:\scripts

        为了验证文件,请在本地计算机上转到步骤3中使用的路径(在我的示例中为c:\ scripts

      5. Open the file and verify the results

        打开文件并验证结果

      6. As you can see, it is very easy to store results periodically from the azure machine in our local machine.如您所见,将Azure机器定期将结果存储在我们的本地机器中非常容易。

      限制条件 ( Restrictions )

      There are some limitations in SQL Azure. For example, the maximum number of Databases in Azure is 150 per server.

      SQL Azure中有一些限制。 例如,Azure中的最大数据库数量是每台服务器150个。

      In addition, there is a limitation per size according to the Database Edition. For example, the Basic Tier is limited to 2 GB, the standard tier 250 GB and the premium tier 500 GB.

      另外,根据数据库版本,每个大小都有限制。 例如,基本层限制为2 GB,标准层限制为250 GB,高级层限制为500 GB。

      For more information about the different limitations in Azure, verify the references the Azure SQL Database General Guidelines and Limitations and the Service tier.

      有关Azure中不同限制的更多信息,请验证对Azure SQL数据库通用准则和限制以及服务层的引用。

      结论 ( Conclusion )

      In this article, we saw how to work connect to an Azure VM with SQL Server installed using sqlcmd.

      在本文中,我们看到了如何通过使用sqlcmd安装到装有SQL Server的Azure VM。

      We also learned how to create a database in Azure using sqlcmd and how to run a script. Specifically we run a backup script. We also learned how to execute the commands in a batch file.

      我们还学习了如何使用sqlcmd在Azure中创建数据库以及如何运行脚本。 具体来说,我们运行一个备份脚本。 我们还学习了如何在批处理文件中执行命令。

      The other example was how to get the fragmentation information of an Azure table in a local file.

      另一个示例是如何在本地文件中获取Azure表的碎片信息。

      As you can see, the process to automate Azure tasks with sqlcmd is very simple and easy. If you have more questions, ask in the comments.

      如您所见,使用sqlcmd自动化Azure任务的过程非常简单。 如果您还有其他问题,请在评论中提问。

翻译自: https://www.sqlshack.com/how-to-work-with-the-command-line-and-azure-to-automate-tasks/

azure 使用

azure 使用_如何使用命令行和Azure自动执行任务相关推荐

  1. azure 使用_如何使用JavaScript在Azure上开始使用SignalR

    azure 使用 The other day, some fine developers at my company were getting ready to roll out a status u ...

  2. jenkins 手动执行_想知道如何用Jenkins自动执行Python脚本输出测试报告?

    前言在用python做自动化测试时,我们写好代码,然后需要执行才能得到测试报告,这时我们可以通过 Jenkins 来进一步完成自动化工作.借助Jenkins,我们可以结合 Git/SVN 自动拉取代码 ...

  3. access重命名自动保存的宏_如何将宏保存为自动执行的宏计算机二级access的一个题目。 题目完整为“请重命名宏mTest,并保存为自动执行宏。”...

    共回答了19个问题采纳率:100% 关于宏 如果在 Microsoft Word 中反复执行某项任务,可以使用宏自动执行该任务.宏是一系列 Word 命令和指令,这些命令和指令组合在一起,形成了一个单 ...

  4. mysql on azure 链接_如何通过Python从Azure函数连接到azuremysql

    根据您的描述,我认为您的问题是关于如何在Azure功能应用程序中安装Python第三方模块.在 具体步骤如下: 第一步: 登录kudu:https://Your_APP_NAME.scm.azurew ...

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

    sql azure 语法 This article will provide an overview covering programmatically moving databases on the ...

  6. angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器

    angular过滤字符 介绍 (Introduction) In this article, we will create an optical character recognition (OCR) ...

  7. azure服务器_如何使用Blazor WebAssembly实施Azure无服务器

    azure服务器 介绍 (Introduction) In this article, we will learn how to implement Azure serverless with Bla ...

  8. azure db 设置时区_将数据迁移到Azure Cosmos DB

    azure db 设置时区 In the previous article, Start your journey with Azure Cosmos DB, we provided you with ...

  9. sql azure 语法_如何将SQL数据迁移到Azure Cosmos DB

    sql azure 语法 In this article, I have explained the step by step process of migrating data from SQL S ...

最新文章

  1. 2020年投入200000000美元,华为不只挖掘年薪百万的“天才少年”
  2. AdminLTE组件之表格DataTable
  3. String s=new String(abc)创建了2个对象的原因
  4. poj 1703(种类并查集)
  5. 美国智能家居止步不前 原因是产品过于碎片化
  6. batch-size 深度学习笔记
  7. Android studio中单项选择,Android studio单选按钮、复选按钮
  8. java异常处理语句是,java 异常处理
  9. 11.string容器
  10. debian分区方案(就这个看着靠谱点)转
  11. 除了大家知道的navicat,再介绍两款免费的数据库连接工具
  12. 兼容pmbus的降压DC/DC模块提供更高的输出电流
  13. SQLyog中文版安装教程
  14. 数资问题【抽屉问题】
  15. python切换环境_python如何变换环境
  16. mac如何把html转成word,Pages怎么保存为word格式 pages保存格式教程
  17. 用apktool反编译,修改添加smali文, 再打包apk遇到的64k问题的解决方法 Unsigned short value out of range: 65536
  18. 【leetcode_easy_$】577. Employee Bonus
  19. 人体经络气血运行规律
  20. 中国手术标记笔市场趋势报告、技术动态创新及市场预测

热门文章

  1. Unity3d 屏幕空间人体皮肤知觉渲染次表面散射Screen-Space Perceptual Rendering Subsurface Scattering of Human Skin...
  2. Fragment的生命周期同一Activity下不同Fragment之间的通信
  3. centos6.5 最小化安装无法上网
  4. 在登陆AD的机器上测试模拟经过验证的用户
  5. 在想的事情......
  6. window safari 怎么进入响应式_Web前端新手怎么入门 如何用CSS做响应式布局
  7. gels imagej 图片处理_如何用ImageJ分析运动细胞?
  8. 35岁以上的IT人士如果有一天被公司裁员了,该怎么办?
  9. 两个员工,一个做事认真但效率低,一个迟到早退但效率高,只能留一个我该留哪个?
  10. 细节真的能决定成败么?