Creating a Linked Server in Amazon RDS for SQL Server: A Step-by-Step Guide

PressRex profile image
by PressRex
Creating a Linked Server in Amazon RDS for SQL Server: A Step-by-Step Guide

A common requirement for database professionals working with SQL Server is the capability to connect seamlessly to other servers or databases. Amazon RDS for SQL Server facilitates this through the support of Linked Servers, allowing users to connect their RDS SQL Server instance to various data sources, whether these are located on-premises or in the cloud. This guide provides a comprehensive overview of the process of creating and configuring a linked server in Amazon RDS for SQL Server. It will explore practical steps, essential considerations, and best practices tailored for seasoned database professionals.

What is a Linked Server?

A Linked Server in SQL Server allows you to query remote data sources as if they were part of your local server. This can include other SQL Server instances, Oracle databases, MySQL, or even non-relational sources like Excel or ODBC-accessible systems.

In the context of Amazon RDS, linked servers are especially useful for:

  • Consolidating data from multiple systems.
  • Performing distributed queries.
  • Migrating data between environments.

Note: You cannot enable or configure a linked server using the SSMS console in Amazon RDS for SQL Server. Attempting to do so, even with the RDS_ADMIN account, will result in an error stating that the user does not have the necessary permissions. Configuration must be performed exclusively through SQL commands.

rds-srv

Step 1: Ensure Prerequisites are in Place

Before proceeding, make sure the following prerequisites are met:

  1. AWS RDS Instance Permissions: Verify that the RDS instance allows the sp_addlinkedserver and sp_addlinkedsrvlogin system stored procedures. Amazon RDS for SQL Server supports linked servers but does not allow modifying the registry or installing additional OLE DB drivers.
  2. Network Configuration:
    • Ensure proper routing and security group settings to allow connectivity if your target server is on-premises or in another VPC.
    • Use Amazon VPC Peering or AWS Direct Connect if necessary.
  3. Authentication Method: Linked servers require credentials to connect to the target system. You can choose between SQL Server authentication or Windows authentication (via pass-through).

Step 2: Configure the Linked Server

Here are the steps to create and configure a linked server.

2.1 Add the Linked Server

Use the sp_addlinkedserver procedure to define the linked server. Here's an example:

EXEC sp_addlinkedserver
@server = 'MyLinkedServer', -- Logical name for the linked server
@srvproduct = '', -- Can be left blank for SQL Server
@provider = 'SQLNCLI', -- SQL Server Native Client
@datasrc = 'target-db-instance.endpoint.rds.amazonaws.com'; -- Target server

In this example:

  • MyLinkedServer is the alias used locally to refer to the remote server.
  • SQLNCLI is the SQL Server Native Client provider.
  • target-db-instance.endpoint.rds.amazonaws.com is the DNS endpoint of the remote database.

2.2 Configure Authentication

To define login mappings, use the sp_addlinkedsrvlogin procedure:

EXEC sp_addlinkedsrvlogin
@rmtsrvname = 'MyLinkedServer', -- The linked server name
@useself = 'false', -- Disable pass-through authentication
@locallogin = NULL, -- Applies to all local users
@rmtuser = 'remote_user', -- Remote SQL Server username
@rmtpassword = 'remote_password'; -- Remote SQL Server password

This example configures SQL Server authentication by specifying a remote username and password. Adjust the credentials according to your setup.

Step 3: Test the Linked Server

After creating the linked server, you can test the connection using a simple query:

SELECT *
FROM [MyLinkedServer].[DatabaseName].[SchemaName].[TableName];

If the connection is successful, the query will return data from the remote server. If not, troubleshoot the following:

  • Network connectivity: Ensure the RDS instance can reach the target server.
  • Permissions: Verify the remote user has appropriate access.
  • Error messages: Review the SQL Server error log for details.
Link_srv

Step 4: Advanced Configurations (Optional)

Here are a few advanced things you can do with a linked server.

4.1 Change Connection Timeout

To optimize the linked server performance, consider adjusting the connection and query timeout settings. Choose values that are appropriate for your situation. Here I am setting 600 seconds (10 minutes):

EXEC sp_serveroption
@server = 'MyLinkedServer',
@optname = 'query timeout',
@optvalue = '600'; -- Set timeout to 600 seconds

4.2 Enable RPC (Remote Procedure Call)

If you need to execute stored procedures on the remote server, enable RPC with this command:

EXEC sp_serveroption
@server = 'MyLinkedServer',
@optname = 'rpc out',
@optvalue = 'true';

Step 5: Clean Up

To remove a linked server, use the following command:

EXEC sp_dropserver @server = 'MyLinkedServer', @droplogins = 'droplogins';

This ensures that the linked server and associated login mappings are completely removed.

Best Practices for Using Linked Servers

  1. Use Linked Servers Sparingly: While linked servers can be convenient, over-reliance can lead to performance bottlenecks. Consider ETL processes for large-scale data integrations.
  2. Secure Connections: Use encrypted connections (SSL/TLS) when connecting to remote databases.
  3. Monitor Usage: Use SQL Server Profiler or Extended Events to monitor linked server activity and identify potential performance issues.
  4. Optimize Queries: Avoid querying large datasets across linked servers. Instead, filter data at the source.

Conclusion

Since we cannot enable or configure a linked server using the SSMS console in Amazon RDS for SQL Server, we need to use AWS provided stored procs to create one. However, setting up a linked server in Amazon RDS for SQL Server is a straightforward process that can greatly enhance your ability to query and integrate data from different systems. By following the steps outlined in this guide, you can securely and efficiently configure a linked server.

While linked servers are powerful, always weigh their use against alternative solutions like replication, ETL pipelines, or direct API integrations to ensure optimal performance and maintainability.

The post Creating a Linked Server in Amazon RDS for SQL Server: A Step-by-Step Guide appeared first on SQLServerCentral.

Source: View source

PressRex profile image
by PressRex

Subscribe to New Posts

Lorem ultrices malesuada sapien amet pulvinar quis. Feugiat etiam ullamcorper pharetra vitae nibh enim vel.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Read More