SQL 如何使用非默认端口创建链接服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1445125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Linked Server with non-default port
提问by r?ph
I want to create a Linked Server in MS SQL Server 2000 to a MS SQL 2005 Server which runs on port x (not the default port 1433). But it doesn't work, as I can't specify the port anywhere!?
我想在 MS SQL Server 2000 中创建一个链接服务器到一个在端口 x(不是默认端口 1433)上运行的 MS SQL 2005 服务器。但它不起作用,因为我无法在任何地方指定端口!?
Using sqlcmd (specifying port x), I can connect to the server without problems - but I can't set it up as a Linked Server.
使用 sqlcmd(指定端口 x),我可以毫无问题地连接到服务器 - 但我无法将其设置为链接服务器。
How can this be done?
如何才能做到这一点?
采纳答案by Shane Castle
In the new linked server dialog, choose "Other data source", select "Microsoft OLE DB Provider for SQL Server" as your provider name, then use this as your provider string:
在新的链接服务器对话框中,选择“其他数据源”,选择“Microsoft OLE DB Provider for SQL Server”作为您的提供者名称,然后将其用作您的提供者字符串:
Data Source=192.168.1.100,1433;Network Library=DBMSSOCN;Initial Catalog=yourdbname;User ID=username;Password=password;
Replace the IP and "1433" with your IP and port number.
用您的 IP 和端口号替换 IP 和“1433”。
More info on connection strings: http://support.microsoft.com/kb/238949
有关连接字符串的更多信息:http: //support.microsoft.com/kb/238949
回答by Charles Fleming
Another way to achieve this (assuming that you have set up 8080 in SQL Server) is with the following code:
实现此目的的另一种方法(假设您已在 SQL Server 中设置了 8080)是使用以下代码:
EXEC sp_addlinkedserver 'myserver', '', 'SQLNCLI', 'xx.xx.xx.xx,8080'
EXEC sp_addlinkedsrvlogin 'myserver', 'FALSE', NULL, 'user', 'pwd'
回答by NightShovel
Based on Shane's suggestion, adding an alias worked for me.
根据 Shane 的建议,添加别名对我有用。
(SQL Server 2008 R2):
(SQL Server 2008 R2):
- Open SQL Server Configuration Manager
- Expand "SQL Server Configuration Manager (local)"
- Expand "SQL Native Client 10.0 Configuration (32bit)"
- Click "Aliases"
- Right-click in the right-side alias list, and choose "New Alias"
- "Alias Name" is whatever you want to reference the linked server as.
- "Port No" is your non-default port.
- "Protocol" depends, but you can most likely leave this as "TCP/IP".
- "Server" is the address of the server you're trying to connect to (not including port).
- 打开 SQL Server 配置管理器
- 展开“SQL Server 配置管理器(本地)”
- 展开“SQL Native Client 10.0 配置(32位)”
- 点击“别名”
- 右键单击右侧别名列表,然后选择“新建别名”
- “别名”是您想要引用链接服务器的任何名称。
- “端口号”是您的非默认端口。
- “协议”取决于,但您很可能将其保留为“TCP/IP”。
- “服务器”是您尝试连接的服务器的地址(不包括端口)。
(repeat the steps for "SQL Native Client 10.0 Configuration" (minus the '32bit' text))
(重复“SQL Native Client 10.0 配置”的步骤(减去'32bit'文本))
Adding an alias this way allowed me to add a linked server with the Server Type as "SQL Server", without configuring provider options, etc.
以这种方式添加别名允许我添加服务器类型为“SQL Server”的链接服务器,而无需配置提供程序选项等。
回答by smoore4
Note that 4-part queries will look similar to this:
请注意,由 4 部分组成的查询将类似于以下内容:
SELECT * FROM [SQLSERVER,14333].[DATABASE].[dbo].[Table1]
回答by joym8
I had to do this today as well (add a linked server with non-default port). In my case it was adding a SQL Server 2014 linked server to a SQL Server 2016.
我今天也必须这样做(添加一个带有非默认端口的链接服务器)。就我而言,它是将 SQL Server 2014 链接服务器添加到 SQL Server 2016。
Steps using SQL Server Management Studio:
使用 SQL Server Management Studio 的步骤:
- Open SSMS and go to Server Objects > Linked Server > New Linked Server
Use this format for the Linked Server
ip-address-of-linked-server\instance-name,non-default-port
or,192.168.10.5\dev-sql,25250
. Instance name is required only if that instance is not the default instance on target linked server. Also, you can replace ip address by host name if the linked server is on your local network.Select SQL Server for Server Type
- Add any credentials required to connect using the Security tab
- Query the new server using the format just like SQLDBA specified above.
- 打开 SSMS 并转到服务器对象 > 链接服务器 > 新建链接服务器
将此格式用于链接服务器
ip-address-of-linked-server\instance-name,non-default-port
或,192.168.10.5\dev-sql,25250
。仅当该实例不是目标链接服务器上的默认实例时才需要实例名称。此外,如果链接服务器在您的本地网络上,您可以用主机名替换 ip 地址。选择 SQL Server 作为服务器类型
- 添加使用“安全”选项卡进行连接所需的任何凭据
- 使用上面指定的 SQLDBA 格式查询新服务器。
Same thing using T-SQL:
使用 T-SQL 同样的事情:
EXEC master.dbo.sp_addlinkedserver @server = N'192.168.10.5\dev-sql,25250', @srvproduct=N'SQL Server'
EXEC master.dbo.sp_addlinkedserver @server = N'192.168.10.5\dev-sql,25250', @srvproduct=N'SQL Server'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'192.168.10.5\dev-sql,25250',@useself=N'False',@locallogin=NULL,@rmtuser=N'my_username',@rmtpassword='my_pswd'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'192.168.10.5\dev-sql,25250',@useself=N'False',@locallogin=NULL,@rmtuser=N'my_username',@rmtpassword='my_pswd'