sql azure 语法

In this article, we will review on managing database files of SQL Server running on Azure Kubernetes service.

在本文中,我们将回顾管理在Azure Kubernetes服务上运行SQL Server的数据库文件。

Please refer to “Creating a Kubernetes cluster” section in SQL Server in Azure Kubernetes Service (AKS) for creating a Kubernetes cluster using Standard_B2s size VM’s

请参考Azure Kubernetes服务(AKS)中SQL Server中的 “ 创建Kubernetes群集 ”部分,以使用Standard_B2s大小的VM创建Kubernetes群集。

We will cover the following topics of about managing database file in SQL Server running on Kubernetes cluster.

我们将介绍有关在Kubernetes集群上运行SQL Server中管理数据库文件的以下主题。

  1. Create multiple persistent volume claims and mount them to pod running SQL Server container 创建多个持久卷声明并将其安装到运行SQL Server容器的Pod中
  2. Changing the location of tempdb database files in Kubernetes 在Kubernetes中更改tempdb数据库文件的位置
  3. Changing the location master database files in Kubernetes 在Kubernetes中更改位置主数据库文件

将多个卷挂载到Pod (Mount multiple volumes to the Pod)

By default, only one volume is mounted to the pod that is running SQL Server container when we deploy SQL Server using default manifest file in SQL Server in Azure Kubernetes Service (AKS). All the data and log files of the databases reside in the same volume. In case if you want to place the data files in one volume and the log files in another volume or spread database files across different volumes, we need to create multiple volumes and mount them to the pod running SQL Server container.

默认情况下,当我们使用Azure Kubernetes Service(AKS)中SQL Server中的默认清单文件部署SQL Server时,只有一个卷安装到运行SQL Server容器的Pod 。 数据库的所有数据和日志文件都位于同一卷中。 如果要将数据文件放在一个卷中,而将日志文件放在另一个卷中,或将数据库文件分散在不同的卷中,则需要创建多个卷并将它们安装到运行SQL Server容器的容器中。

After creating the Kubernetes cluster using steps mentioned in SQL Server in Azure Kubernetes Service (AKS) and the nodes are in the ready state, create multiple volumes using below manifest files.

在Azure Kubernetes Service(AKS)中使用SQL Server中提到的步骤创建Kubernetes群集并且节点处于就绪状态后,请使用以下清单文件创建多个卷。

Open cloud shell in your Azure portal and run the following command to create pv1.yaml manifest file.

在Azure门户中打开云外壳,然后运行以下命令来创建pv1.yaml清单文件。

cat -> pv1.yaml

Paste the following code and press Ctrl + Z.

粘贴以下代码,然后按Ctrl +Z。

kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:name: azure-disk
provisioner: kubernetes.io/azure-disk
parameters:storageaccounttype: Standard_LRSkind: Managed
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: mssql-dataannotations:volume.beta.kubernetes.io/storage-class: azure-disk
spec:accessModes:- ReadWriteOnceresources:requests:storage: 8Gi

Similarly, create a pv2.yaml manifest file with the following script.

同样,使用以下脚本创建pv2.yaml清单文件。

kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:name: azure-disk
provisioner: kubernetes.io/azure-disk
parameters:storageaccounttype: Standard_LRSkind: Managed
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: mssqllogsannotations:volume.beta.kubernetes.io/storage-class: azure-disk
spec:accessModes:- ReadWriteOnceresources:requests:storage: 8Gi

Execute ls command and make sure you have both pv1.yaml and pv2.yaml files

执行ls命令并确保您同时拥有pv1.yaml和pv2.yaml文件

Now we need to apply both pv1.yaml and pv2.yaml to create two persistent volumes and volume claims.

现在,我们需要同时应用pv1.yaml和pv2.yaml来创建两个持久卷和卷声明。

Apply pv1.yaml using the below script. Applying pv1.yaml creates a persistent volume claim with name mssql-data of capacity of 8GB.

使用以下脚本应用pv1.yaml。 应用pv1.yaml会创建一个名为mssql-data的容量为8GB的持久卷声明。

kubectl apply -f pv1.yaml

Similarly, apply pv2.yaml which creates persistent volume claim with name “mssqllogs” of capacity 8GB in Azure Kubernetes Service.

同样,应用pv2.yaml在Azure Kubernetes Service中创建名称为“ mssqllogs”的容量为8GB的持久卷声明。

kubectl apply -f pv2.yaml

Now we need to create a deployment and specify both “mssql-data” and “mssqllogs” volume claims in the deployment and apply the deployment manifest.

现在,我们需要创建一个部署,并在部署中同时指定“ mssql-data”和“ mssqllogs”卷声明并应用部署清单。

Before creating a deployment, create a secret key that will be used in the deployment. Please execute the following script by replacing the password of your choice.

在创建部署之前,请创建将在部署中使用的密钥。 请通过替换您选择的密码来执行以下脚本。

kubectl create secret generic mssql --from-literal=SA_PASSWORD="yourownpassword"

Now create a deployment manifest file using the below script.

现在,使用以下脚本创建部署清单文件。

cat -> sql.yaml

Paste the following script and press CTRL + Z.

粘贴以下脚本,然后按CTRL +Z。

apiVersion: apps/v1beta1
kind: Deployment
metadata:name: mssql-deployment
spec:replicas: 1template:metadata:labels:app: mssqlspec:terminationGracePeriodSeconds: 10containers:- name: mssqlimage: mcr.microsoft.com/mssql/server:2017-latestports:- containerPort: 1433env:- name: MSSQL_PIDvalue: "Developer"- name: ACCEPT_EULAvalue: "Y"- name: MSSQL_SA_PASSWORDvalueFrom:secretKeyRef:name: mssql2key: SA_PASSWORD volumeMounts:- name: mssqldbmountPath: /var/opt/mssql- name: mssqllogmountPath: /var/opt/mssqllogvolumes:- name: mssqldbpersistentVolumeClaim:claimName: mssql-data- name: mssqllogpersistentVolumeClaim:claimName: mssqllogs
---
apiVersion: v1
kind: Service
metadata:name: rbcsql
spec:selector:app: mssqlports:- protocol: TCPport: 1433targetPort: 1433type: LoadBalancer

Now apply the sql.yaml to create a deployment which creates a pod and mount both volumes claims to it.

现在,应用sql.yaml创建一个部署,该部署创建一个pod并将两个卷声明都挂载到它。

kubectl apply -f sql.yaml

Verify the status of the pods using the below command.

使用以下命令验证Pod的状态。

kubectl get pods

Once the pod is in the running state, we can execute commands on the pod and check the files inside the volumes.

一旦Pod处于运行状态,我们就可以在Pod上执行命令并检查卷中的文件。

To run commands against pod in Azure Kubernetes Service, we need to get the pod name and replace it in the below script.

若要在Azure Kubernetes Service中针对pod运行命令,我们需要获取pod名称并将其替换为以下脚本。

kubectl exec -it mssql-deployment-6bf47d8f65-qp8dj  bash

Execute ls command against pod to retrieve files and directories. The persistent volumes are under /var/opt.

对pod执行ls命令以检索文件和目录。 永久卷在/ var / opt下。

Execute below command to navigate to /var/opt and execute ls command to list files and directories under /var/opt

执行以下命令导航到/ var / opt,执行ls命令列出/ var / opt下的文件和目录

cd /var/opt

更改tempdb数据库文件位置 (Changing the tempdb database file location)

By default, the log and data files are placed under “/var/opt/mssql/data”. the error log, agent log, and default trace files are placed under “/var/opt/mssql/log”

默认情况下,日志和数据文件位于“ / var / opt / mssql / data”下。 错误日志,代理日志和默认跟踪文件位于“ / var / opt / mssql / log”下

Now to move the log file of a tempdb database from “/var/opt/mssql/data” in the default volume “mssqldb” associated with persistent volume claim “mssql-data” to “/var/opt/mssqllog” in the new volume “mssqllog” which is associated with persistent volume claim “mssqllogs”.

现在,将新的tempdb数据库的日志文件从与持久性卷声明“ mssql-data ”关联的默认卷“ mssqldb ”中的“ / var / opt / mssql / data ”移到新的“ / var / opt / mssqllog”中体积“mssqllog”,这是与持续体积权利要求“mssqllogs”相关联。

Get the IP address of the SQL Server running in Azure Kubernetes Service using the below command and login to the SQL Server using SQL Server management studio.

使用以下命令获取在Azure Kubernetes服务中运行SQL Server的IP地址,并使用SQL Server Management Studio登录到SQL Server。

kubectl get svc

Use the IP address and the secret you created earlier to login into the SQL Server using SQL Server management studio.

使用您先前创建的IP地址和密码使用SQL Server Management Studio登录SQL Server。

Execute below T-SQL script to know the current location of the log file of tempdb database.

在T-SQL脚本下面执行,以了解tempdb数据库日志文件的当前位置。

 SELECT name, physical_name AS CurrentLocation  FROM sys.master_files  WHERE database_id = DB_ID(N'tempdb');  GO

Now execute the flowing script to change the location of the log file of a tempdb database.

现在执行执行中的脚本来更改tempdb数据库的日志文件的位置。

  ALTER DATABASE tempdb   MODIFY FILE (NAME = templog, FILENAME = '/var/opt/mssqllog/templog.ldf');  GO

Now restart the SQL Server by running the SHUTDOWN command in SQL Server management studio which will automatically create the log file of tempdb database “templog.ldf” file in the new location.

现在,通过在SQL Server Management Studio中运行SHUTDOWN命令来重新启动SQL Server,它将在新位置自动创建tempdb数据库“ templog.ldf”文件的日志文件。

更改主数据库文件的位置。 (Changing the master database file location.)

Generally moving master database files involves the following steps.

通常,移动主数据库文件涉及以下步骤。

  • Set the new location using mssql-conf 使用mssql-conf设置新位置
  • Stop the SQL Server 停止SQL Server
  • Move the files to the new location 将文件移到新位置
  • Start the SQL Server 启动SQL Server

Changing the master database file of SQL Server running on Kubernetes cluster in Azure Kubernetes Service is not a straightaway procedure.

在Azure Kubernetes Service中更改在Kubernetes群集上运行SQL Server的主数据库文件不是一个简单的过程。

In Kubernetes, you need to run commands at the pod level to access mssql-conf. to execute commands at the pod level using the below command and replace the pod name with your pod name.

在Kubernetes中,您需要在pod级别运行命令以访问mssql-conf。 使用以下命令在Pod级别执行命令,并将Pod名称替换为您的Pod名称。

kubectl exec -it mssql-deployment-576c5bdcdf-sv8r5   bash

mssql-conf is at location “/opt/mssql/bin”. Navigate to “/opt/mssql/bin” and run the following command to change the location of the master database log file.

mssql-conf位于“ / opt / mssql / bin”位置。 导航到“ / opt / mssql / bin”,然后运行以下命令来更改master数据库日志文件的位置。

Execute the following command to change the location of the master database log file.

执行以下命令来更改master数据库日志文件的位置。

./mssql-conf set filelocation.masterlogfile /var/opt/mssqllog/mastlog.ldf

Now we need to restart SQL Server. Issue a shutdown command from the SQL Server management studio.

现在我们需要重新启动SQL Server。 从SQL Server管理工作室发出关闭命令。

As soon as shutdown command was executed the SQL Server and the pod running SQL Server container in Azure Kubernetes Service is also restarted. Now we need to move the log file of the master database to a new location and start the SQL Server. But, to move the log file to a new location we need to execute the “mv” command on the pod and the pod is not in the “Running” state.

一旦执行了关闭命令,Azure Kubernetes服务中SQL Server和运行SQL Server容器的容器也将重新启动。 现在,我们需要将master数据库的日志文件移动到新位置并启动SQL Server。 但是,要将日志文件移动到新位置,我们需要在容器上执行“ mv ”命令,并且容器未处于“正在运行”状态。

In Kubernetes, as per the deployment, the pod will always check if the SQL server services are running or not. If the SQL Server is not running, the pod will restart, which will automatically restart the SQL Server inside it. But the SQL Server does not find the master database log file in the new location as we have not yet moved it. In this case, the SQL Server will never start, and the pod in Azure Kubernetes Service will go into “Error” status and keeps on restarting and we will be not able to move the log file to a new location as we need pod in “Running” status to execute “mv” command.

在Kubernetes中,根据部署,容器将始终检查SQL Server服务是否正在运行。 如果SQL Server未运行,则pod将重新启动,这将自动重新启动其中SQL Server。 但是SQL Server在新位置找不到主数据库日志文件,因为我们尚未移动它。 在这种情况下,SQL Server将永远不会启动,Azure Kubernetes Service中的Pod将进入“错误”状态并继续重新启动,我们将无法将日志文件移动到新位置,因为我们需要在“ 运行 “状态”以执行“ mv ”命令。

kubectl  logs -p mssql-deployment-576c5bdcdf-sv8r5

I tried a workaround to sort out this. The following are the steps.

我尝试了一种解决方法来解决这个问题。 以下是步骤。

  • Delete the existing deployment 删除现有部署
  • Create a dummy pod 创建一个虚拟吊舱
  • Mount the volumes to dummy pod 将卷安装到虚拟吊舱
  • Move the file to a new location 将文件移到新位置
  • Delete the dummy pod 删除虚拟吊舱
  • Create the deployment again by applying sql.yaml manifest file. 通过应用sql.yaml清单文件再次创建部署。
kubectl delete mssql-deployment

Deploy below manifest to create a dummy pod and mount the volumes.

在清单下进行部署以创建虚拟吊舱并安装卷。

kind: Pod
apiVersion: v1
metadata:name: dummy
spec:volumes:- name: mssqldbpersistentVolumeClaim:claimName: mssql-data- name: mssqllogpersistentVolumeClaim:claimName: mssqllogscontainers:- name: dummyimage: nginxports:- containerPort: 80name: "http-server"volumeMounts:- mountPath: "/var/opt/mssql"name: mssqldb- mountPath: "/var/opt/mssqllog"name: mssqllog

To run commands in the pod use the below script.

要在pod中运行命令,请使用以下脚本。

kubectl exec -it dummy  bash

Use the flowing script to move the log file of the master database to a new location in Azure Kubernetes Service.

使用流动脚本将master数据库的日志文件移动到Azure Kubernetes Service中的新位置。

mv /var/opt/mssql/data/mastlog.ldf /var/opt/mssqllog/mastlog.ldf

Verify if the file is moved or not, exit the pod and delete the dummy pod.

验证文件是否移动,退出窗格并删除虚拟窗格。

kubectl delete pod dummy

Once the dummy pod is deleted. apply the sql.yaml again.

一旦虚拟吊舱被删除。 再次应用sql.yaml。

kubectl apply -f sql.yaml

Check the status of the pod by executing the below script.

通过执行以下脚本来检查Pod的状态。

ubectl get pods

The pod is in Running status as it started SQL Server services successfully. The SQL Server services are successfully started as the log file of the master database is found in the new location.

吊舱成功启动SQL Server服务后,其处于运行状态。 由于在新位置找到了master数据库的日志文件,因此SQL Server服务已成功启动。

Login to SQL server using SQL Server management studio and check the location of the master database log file.

使用SQL Server Management Studio登录到SQL Server,并检查主数据库日志文件的位置。

SELECT name, physical_name AS CurrentLocation  FROM sys.master_files  WHERE database_id = DB_ID(N'master');  GO

结论 (Conclusion)

In this article, we have explored how to create multiple persistent volume claims in Azure Kubernetes Service, mount the persistent volume claim to the pods and move the database files of SQL server running in Kubernetes cluster from the location in the default volume to the location in another volume. In case, if you have any question or other methods/workarounds to move the master database files to a new location, please feel free to post in comment section below.

在本文中,我们探讨了如何在Azure Kubernetes Service中创建多个持久卷声明,如何将持久卷声明挂载到Pod以及如何将在Kubernetes群集中运行SQL Server的数据库文件从默认卷中的位置移动到以下位置:另一卷。 如果您有任何疑问或其他方法/解决方法将主数据库文件移动到新位置,请随时在下面的评论部分中发布。

翻译自: https://www.sqlshack.com/azure-kubernetes-service-aks-managing-sql-server-database-files/

sql azure 语法

sql azure 语法_Azure Kubernetes服务(AKS)–管理SQL Server数据库文件相关推荐

  1. sql azure 语法_Azure Kubernetes服务(AKS)中SQL Server

    sql azure 语法 In this article, we will review how to create a Kubernetes cluster in Azure Kubernetes ...

  2. sql azure 语法_Azure SQL数据同步–在Azure SQL数据库之间复制数据和架构更改

    sql azure 语法 In this article, we will review how to configure the sync group to replicate data betwe ...

  3. sql azure 语法_Azure SQL –弹性作业代理

    sql azure 语法 In this article, we will review on elastic job Agent in Azure SQL and how to configure ...

  4. sql azure 语法_Azure中的新SQL数据仓库

    sql azure 语法 介绍 (Introduction) In previous chapters, we taught how to create SQL Databases in Azure. ...

  5. sql azure 语法_Azure SQL Server自动故障转移组

    sql azure 语法 In this article, we will review how to set up auto-failover groups in Azure SQL Server ...

  6. sql azure 语法_Azure Data Studio中SQL代码段

    sql azure 语法 This article will fully cover the code snippet SQL developer productivity feature in Az ...

  7. sql azure 语法_Azure Data Studio中SQL Server Profiler

    sql azure 语法 In this article, we will explore SQL Server Profiler in Azure Data Studio in detail inc ...

  8. sql azure 语法_Azure SQL Server中的CREATE DATABASE语句概述

    sql azure 语法 In this article, we will review CREATE DATABASE statement in the Azure SQL database wit ...

  9. sql azure 语法_Azure SQL –使用Azure自动化的索引表

    sql azure 语法 This article provides an overview of indexing tables in Azure SQL database using Azure ...

最新文章

  1. textarea选中行删除_如何一键删除表格空行,这个方法才最高级!
  2. named 客户端无法解析_Outlook邮件附件无法直接打开?用这个办法轻松解决
  3. 类成员函数作为函数参数/回调函数 出现error C3867
  4. mysql --创建数据库
  5. iPhone 7卖点不多出货量下降?剧透分析师又发报告
  6. How to achieve conditional break point in your ABAP program
  7. ajax请求模拟登录
  8. 让服务器突破性能极限 阿里云神龙论文入选计算机顶会ASPLOS
  9. php如何按日期统计,关于按日获取统计信息:按日期获取统计信息 – 日期时间列 – mysql / php...
  10. 使用pickle模块打包停用词表,加快处理文本数据的速度
  11. yii2 提供接口给java_Yii2 基于RESTful架构的 advanced版API接口开发 配置、实现、测试 (转)...
  12. Scala学习之Option类
  13. 最简洁的PHP把PHP生成HTML代码
  14. 一分钟搞懂NB-IoT行业发展
  15. 计算机考试有python吗_计算机二级考试有python吗
  16. Linux指令系统文件复制到U盘
  17. python-flask 设置网页保留缓存静态文件时间
  18. 他来了他来了,英伟达发布全新Hopper架构GPU“H100”4nm制程800亿晶体管
  19. 上海地铁+android+nfc,在上海手机竟然能当交通卡用!最全攻略来了!
  20. 获取iPhone各个版本屏幕大小

热门文章

  1. java接收json字符串_JAVA后台接收前台传过来的json字符串并解析获得key 和value
  2. 一、SecureCRT 8.0 客户端连接服务器
  3. webpack的一些plugin,怎么使用webpack对项目进行优化
  4. 多个集合计算笛卡尔积-Python
  5. CTSC2017 APIO2017 THUSC2017 游记
  6. 自定义UITabBar
  7. Servlet学习的两个案例之网站访问次数的统计
  8. WebSocket使用80端口的方法
  9. 地图控件快速入门——控制地图
  10. HIT Software Construction Review Notes(1-1 Multi-Dimensional Views of Software Construction)