sql server 别名_SQL Server别名概述

sql server 别名

This article gives an overview of SQL Server Alias and its usage for connecting with SQL Server.

本文概述了SQL Server别名及其与SQL Server连接的用法。

介绍 (Introduction)

Many organizations follow specific naming conventions for the database servers. We might manage multiple servers, and it might be challenging to remember the specific server names. In another scenario, we might be running SQL Server on a specific port other than the default port 1433. In this case, we also require remembering the specific port number and using it in the connection string or in SSMS to make a connection.

许多组织对数据库服务器遵循特定的命名约定。 我们可能管理多个服务器,并且记住特定的服务器名称可能具有挑战性。 在另一种情况下,我们可能会在默认端口1433以外的特定端口上运行SQL Server。在这种情况下,我们还需要记住该特定端口号,并在连接字符串或SSMS中使用该端口号进行连接。

We can register the servers in SSMS and give it a friendly name for connecting it with the central management server. You can connect to a specific SQL Server instance using a familiar name from the CMS server only. SQL Server does not recognize that name, and it does not allow connections from any other server using a familiar name.

我们可以在SSMS中注册服务器,并给它起一个友好的名称,以将其与中央管理服务器连接。 您只能使用CMS服务器中的熟悉名称连接到特定SQL Server实例。 SQL Server无法识别该名称,并且不允许使用熟悉名称的任何其他服务器进行连接。

Consider one more scenario, in which we want to move the databases from server A to server B, but the application should not affect due to that change.

考虑另一种情况,在这种情况下,我们希望将数据库从服务器A移到服务器B,但是由于该更改,应用程序不会受到影响。

SQL Server配置管理器 (SQL Server Configuration Manager)

SQL Server provides a configuration utility SQL Server Configuration Manager. In this configuration manager, we can do the following tasks:

SQL Server提供了一个配置实用程序SQL Server配置管理器。 在此配置管理器中,我们可以执行以下任务:

  • View the status of SQL Server services

    查看SQL Server服务的状态
  • Start, stop, pause, resume the services

    启动,停止,暂停,恢复服务
  • Change service account and service startup (manual\automatic)

    更改服务帐户和服务启动(手动\自动)
  • Specify SQL Server port ( static\dynamic)

    指定SQL Server端口(静态\动态)
  • Manage server and client protocols

    管理服务器和客户端协议

We can launch the SQL Server configuration manager from the start menu. Alternatively, we can look for it in the following directory if Windows is in the C drive.

我们可以从开始菜单启动SQL Server配置管理器。 或者,如果Windows在C驱动器中,则可以在以下目录中查找它。

SQL Server 2019

C:\Windows\SysWOW64\SQLServerManager15.msc

SQL Server 2017

C:\Windows\SysWOW64\SQLServerManager14.msc

SQL Server 2016

C:\Windows\SysWOW64\SQLServerManager13.msc

SQL Server 2019

C:\ Windows \ SysWOW64 \ SQLServerManager15.msc

SQL Server 2017

C:\ Windows \ SysWOW64 \ SQLServerManager14.msc

SQL Server 2016

C:\ Windows \ SysWOW64 \ SQLServerManager13.msc

We require RDP on the SQL Server instance to open the SQL Server Configuration Manager. In the left menu bar, you can find the following options:

我们需要在SQL Server实例上使用RDP打开SQL Server配置管理器。 在左侧菜单栏中,您可以找到以下选项:

  • SQL Server Services

    SQL Server服务
  • SQL Server Network configuration 32-bit

    SQL Server网络配置32位
  • SQL Native client configuration 32-bit

    SQL Native Client配置32位
  • SQL Server Network configuration

    SQL Server网络配置
  • SQL Native client configuration

    SQL Native Client配置

SQL Server别名的配置 (Configuration of SQL Server alias)

Expand SQL Native client configuration, and you can find a subfolder Alias. By default, we do not have any alias defined for the SQL Server instance. In the screenshot, we can see it does not see any items for the Alias.

展开SQL Native Client配置,您可以找到子文件夹Alias 。 默认情况下,我们没有为SQL Server实例定义任何别名。 在屏幕截图中,我们可以看到它没有看到Alias的任何项目。

SQL Server alias

Right-click on Aliases and click on New Alias.

右键单击“别名”,然后单击“新别名”。

create new Alias

We require the following information for SQL Server Alias:

对于SQL Server别名,我们需要以下信息:

  • SQL Instance name

    SQL实例名称
  • SQL Server port

    SQL Server端口

You can use article Overview of SQL Server Ports to determine the various methods to identify SQL Server ports.

您可以使用“ SQL Server端口概述”来确定标识SQL Server端口的各种方法。

Expand SQL Server Network configuration and click on specific SQL instance. It opens up the protocols list. Right-click on TCP/IP and open the properties. In the IP addresses, you can find out the specific port number.

展开“ SQL Server网络配置”,然后单击特定SQL实例。 它打开协议列表。 右键单击“ TCP / IP”,然后打开属性。 在IP地址中,您可以找到特定的端口号。

In my case, I want to create SQL Server Alias for the following information:

就我而言,我想为以下信息创建SQL Server别名:

  • Instance: [Kashish\SQL1]

    实例:[Kashish \ SQL1]
  • Protocol: Named pipes

    协议:命名管道

Protocol for SQL Server instance

We can see the following windows for the New SQL Server Alias:

我们可以在“新建SQL Server别名”中看到以下窗口:

  • Alias Name: It is the familiar name for SQL instance. By default, it shows New Alias. Let’s change it to MySQLInstance

    别名:这是SQL实例的常用名称。 默认情况下,它显示“新别名”。 让我们将其更改为MySQLInstance
  • Port No: Specify port no of the specific instance

    端口号:指定特定实例的端口号
  • Protocol: We can choose from TCP\IP or Named pipe protocol. I want to connect SQL Server using named pipe protocol

    协议:我们可以选择TCP \ IP或命名管道协议。 我想使用命名管道协议连接SQL Server
  • Server: Specify the instance name. For a default instance, we can specify a server name. We need to use [servername\instance] for the named instance

    服务器:指定实例名称。 对于默认实例,我们可以指定服务器名称。 我们需要为命名实例使用[servername \ instance]

create new alias

Before we create Alias, let’s try to connect with the desired name (MySQLInstance) to make sure we cannot connect using that name.

在创建Alias之前,让我们尝试使用所需的名称(MySQLInstance)进行连接,以确保我们无法使用该名称进行连接。

Connect using alias name

It tries to connect to SQL Server, but gives the following error message. It cannot open a connection to SQL Server because the network path was not found.

它尝试连接到SQL Server,但给出以下错误消息。 由于找不到网络路径,因此无法打开与SQL Server的连接。

error in connection with alias name

Let’s configure SQL Server Alias with the information provided earlier. Once we provide the server name, it automatically takes value for the pipe name column as shown below.

让我们使用前面提供的信息配置SQL Server别名。 提供服务器名称后,它将自动为管道名称列获取值,如下所示。

alias details

Click Apply and Ok. You can see SQL Server alias in the Aliases list.

单击“应用”,然后单击“确定”。 您可以在“别名”列表中看到SQL Server别名。

configured SQL Server alias

Let’s try to connect using this SQL Server Alias name in SSMS.

让我们尝试在SSMS中使用此SQL Server别名连接。

We get the error message again that the system cannot find the file specified.

我们再次收到错误消息,提示系统找不到指定的文件。

Connection error message

Why we get this error message again? It should connect with the Alias name.

为什么我们再次收到此错误消息? 它应该与别名名称连接。

Let’s perform some troubleshooting steps. If you recall from the earlier discussion, we want to connect [Kashish\SQL1] instance using named pipes protocol. Named pipes protocol should be enabled in this case.

让我们执行一些故障排除步骤。 如果您回想起前面的讨论,我们想使用命名管道协议连接[Kashish \ SQL1]实例。 在这种情况下,应启用命名管道协议。

In the SQL Server Configuration Manager, expand SQL Server Network Configuration and click on the protocol for the specific instance. In this case, we want to connect to SQL1 instance; therefore, click on Protocol for SQL1.

在“ SQL Server配置管理器”中,展开“ SQL Server网络配置”,然后单击特定实例的协议。 在这种情况下,我们要连接到SQL1实例。 因此,请单击“ SQL1协议”。

In the following screenshot, we can see that the Named pipes protocol is disabled. We cannot connect using the named pipe protocol until it is enabled.

在以下屏幕截图中,我们可以看到已禁用命名管道协议。 在启用命名管道协议之前,我们无法使用它进行连接。

enable the protocol

Right-click on it and enable it. It gives a warning message that change will not take effect until the SQL Service is restarted.

右键单击它并启用它。 它发出警告消息,指出更改将在重新启动SQL Service之前不生效。

warning message

Click Ok and restart SQL Services for the specific instance. Right-click on SQL Service and restart it.

单击“确定”,然后为特定实例重新启动SQL Services。 右键单击“ SQL Service”,然后重新启动它。

Restart SQL Service

Try to connect SQL Server instance again using SQL Server Alias. The connection is successful now.

尝试再次使用SQL Server别名连接SQL Server实例。 现在连接成功。

connection with friendly name

Execute the following query to verify the server name and instance name:

执行以下查询以验证服务器名称和实例名称:

SELECT HOST_NAME() AS ServerName, 
        @@ServiceName AS SQLInstance;

In the following screenshot, we can verify that alias [MySQLInstance] points to [Kashish\SQL1] instance.

在下面的屏幕快照中,我们可以验证别名[MySQLInstance]指向[Kashish \ SQL1]实例。

Verify host name and instance name

We do not need to remember the SQL instance name and port no to connect. We can specify a friendly name for the SQL Server instance for connection. We call this familiar name SQL Server Alias.

我们不需要记住要连接SQL实例名称和端口号。 我们可以为连接SQL Server实例指定一个友好的名称。 我们将此名称称为SQL Server别名。

In the above steps, we explained the process of configuration of SQL Server Alias. We need to note here that this alias can be used directly from the server. You require to RDP server hosting SQL Server instance and use the alias. You cannot use the alias name from outside the server. The reason behind this is that alias is not registered in the domain name server ( DNS).

在上述步骤中,我们解释了SQL Server别名的配置过程。 我们需要在这里注意,可以直接从服务器使用此别名。 您需要为RDP服务器托管SQL Server实例并使用别名。 您不能从服务器外部使用别名。 其背后的原因是别名未在域名服务器(DNS)中注册。

In each organization, we can DNS servers to register server names, and this alias name is not registered in the DNS server. You can contact domain administrators to configure an alias (CNAME).

在每个组织中,我们都可以使用DNS服务器注册服务器名称,并且该别名未在DNS服务器中注册。 您可以联系域管理员以配置别名(CNAME)。

You can connect to an instance using the alias from outside the server as well after registering alias in DNS.

在DNS中注册别名后,也可以从服务器外部使用别名连接到实例。

删除SQL Server别名 (Delete SQL Server Alias)

You can delete alias anytime without influencing the SQL Services. Right-click on the alias name and delete it.

您可以随时删除别名,而不会影响SQL Services。 右键单击别名,然后将其删除。

Delete SQL Server Alias

It gives you the following warning. Click on Yes to remove the alias.

它给您以下警告。 单击是删除别名。

Confirm alias deletion

It removes the defined SQL Server alias.

它删除定义SQL Server别名。

delete alias

使用SQL Server客户端网络实用程序SQL Server别名 (SQL Server alias using SQL Server Client network utility)

In the above steps, we used the SQL Server configuration manager for the alias. We can use SQL Server client network utility as well. You can find this utility in the C:\Windows\SysWOW64 folder.

在上述步骤中,我们使用SQL Server配置管理器作为别名。 我们也可以使用SQL Server客户端网络实用程序。 您可以在C:\ Windows \ SysWOW64文件夹中找到此实用程序。

SQL Server Client network utility

Launch Cliconfg.exe, and it opens the following wizard.

启动Cliconfg.exe,它将打开以下向导。

Add alias in SQL Server Client network utility

In the Alias section, currently, we do not see any SQL Server alias. Click on Add, and it open add network library configuration.

当前,在“别名”部分中,我们看不到任何SQL Server别名。 单击添加,然后打开添加网络库配置

We want to connect with the following information in SSMS:

我们要在SSMS中连接以下信息:

  • SQL Alias(friendly name): SQLShack

    SQL别名(友好名称):SQLShack
  • SQL instance: [Kashish\SQL1]

    SQL实例:[Kashish \ SQL1]
  • Port Number: 51425

    端口号:51425
  • Network libraries: TCP/IP protocol

    网络库:TCP / IP协议

Specify the input in the network library configuration, as shown below.

在网络库配置中指定输入,如下所示。

Add network library configuration

Click Ok, and you can see configured sever alias as shown in the following screenshot.

单击确定,您将看到已配置的服务器别名,如以下屏幕截图所示。

Server alias configuration

Let’s connect using newly configured alias SQLShack in the SSMS. We only need to provide the alias name without SQL instance and port number. It automatically redirects the connection to the SQL Server instance on a specified port using the entry in client network utility.

让我们使用SSMS中新配置的别名SQLShack进行连接。 我们只需要提供别名名称,而无需提供SQL实例和端口号。 它使用客户端网络实用程序中的条目自动将连接重定向到指定端口上SQL Server实例。

Specify alias in SSMS

The connection is successful, and we are connected with the SQLShack alias name in SSMS.

连接成功,并且我们使用SSMS中SQLShack别名连接。

successful connection using alias

结论 (Conclusion)

SQL Server alias gives you the flexibility to choose a familiar name for the SQL Server instance. You do not require remembering the instance details such as instance name, port number, and protocol. You can register alias in DNS to use it from every client machine.

SQL Server别名使您可以灵活地为SQL Server实例选择一个熟悉的名称。 您不需要记住实例详细信息,例如实例名称,端口号和协议。 您可以在DNS中注册别名,以在每台客户端计算机上使用它。

翻译自: https://www.sqlshack.com/overview-of-sql-server-alias/

sql server 别名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值