Access a self-managed on-premises SQL Server through a Linked Server

Updated at:

ApsaraDB RDS for SQL Server supports the linked server feature, which allows you to create linked servers between ApsaraDB RDS for SQL Server instances. If a network connection is available, you can also create a linked server to connect to a self-managed SQL Server instance. This feature enables cross-region data access, data consolidation and analysis, and data migration and synchronization. This topic describes how to create a linked server on an ApsaraDB RDS for SQL Server instance over a VPN to connect to a self-managed SQL Server instance.

Prerequisites

  • Your ApsaraDB RDS for SQL Server instance must meet the following requirements:

    • Instance edition: Cluster Edition and High-availability Edition. Basic Edition is not supported.

    • Instance type: general-purpose and dedicated. shared instances are not supported.

    • Billing method: subscription and pay-as-you-go. serverless instances are not supported.

  • Before you deploy a VPN Gateway, plan your network:

    • Ensure that the CIDR block of your on-premises client does not overlap with the CIDR block of the Virtual Private Cloud (VPC) that you need to access. Otherwise, communication fails.

    • Ensure that the client has internet connectivity.

Billing

Deploying a VPN Gateway incurs fees.

Step 1: Connect your VPC to the on-premises network

1.1. Create a VPN gateway instance

Create a VPN gateway

VPN Gateways are available in two forms: Enhanced and Standard. For details about their differences, see Enhanced vs. standard VPN.

Enhanced VPN gateway (console)
Important

Only the following regions currently support Enhanced VPN Gateway: China (Beijing), China (Hangzhou), China (Shanghai), China (Shenzhen), China (Chengdu), China (Hong Kong), Singapore, Malaysia (Kuala Lumpur), Indonesia (Jakarta), US (Silicon Valley), US (Virginia), Germany (Frankfurt), UK (London).

Go to the Console VPN Gateway page, switch to the Enhanced IPsec-VPN tab, and click Create Enhanced IPsec-VPN to start configuration:

  • Region: Select the region where the VPC to be connected is located.

  • VPC: Select the target VPC.

  • vSwitch 1 and vSwitch 2: You need to select the associated VPC and 2 vSwitches deployed in different zones to ensure cross-zone high availability. After IPsec-VPN is enabled, the system creates one Elastic Network Interface (ENI) under each of the 2 vSwitches as the interface for traffic exchange with the VPC using IPsec connections. Each ENI occupies 1 IP address under the vSwitch.

    China (Wuhan) supports only a single zone and cannot achieve zone-level disaster recovery. We recommend that you specify 2 different vSwitches in the zone to achieve high availability of IPsec connections.
Standard VPN gateway (console)

Go to the Console VPN Gateway page, on the Standard VPN Gateway tab, click Create VPN Gateway to start configuration:

If there is no Standard VPN Gateway tab, click Create VPN Gateway directly.
  • Region and zone: Select the region where the VPC is located.

  • Gateway Type: Select Standard. After an IPsec connection is established, the international standard commercial cryptographic algorithm (standard cryptographic algorithm) will be used.

  • Network Type: Select Public network. A public IP will be allocated to establish an IPsec connection. To establish a private IPsec connection, we recommend Use a private IPsec connection to bind the Enterprise Transit Router.

  • Tunnel: Select Dual tunnel.

  • VPC and Virtual vSwitch: You need to select the associated VPC and 2 vSwitches deployed in different zones to ensure cross-zone high availability. After IPsec-VPN is enabled, the system creates one Elastic Network Interface (ENI) under each of the 2 vSwitches as the interface for traffic exchange with the VPC using IPsec connections. Each ENI occupies 1 IP address under the vSwitch. After a VPN Gateway is created, the associated vSwitches cannot be modified.

    China (Wuhan) supports only a single zone and cannot achieve zone-level disaster recovery. We recommend that you specify 2 different vSwitches in the zone to achieve high availability of IPsec connections.
  • Maximum Bandwidth: The maximum bandwidth specification supported by a VPN Gateway varies by region. If you select a bandwidth specification of 5 Mbps or 10 Mbps, the peak bandwidth from the on-premises data center to the VPN Gateway is limited to 10 Mbps. For details, see Quotas and limitations.

  • Enable IPsec-VPN and disable SSL-VPN.

    If the created VPN Gateway does not have IPsec-VPN enabled, in the Feature Configuration column of the target VPN Gateway, click Enable after IPsec Connections.

    Enabling IPsec-VPN for an existing VPN Gateway requires you to pay the feature price difference for the remaining time of the current billing cycle.
  • Billing Cycle:

    You can choose whether to enable auto-renewal:

    • Monthly subscription: The auto-renewal cycle is 1 month.

    • Annual subscription: The auto-renewal cycle is 1 year.

API
  • Call CreateEnhancedVpnGateway to create an Enhanced VPN Gateway.

    Important

    The enhanced VPN gateway was launched in February 2024 and is currently in an invitation-only preview. To use this feature, contact your Alibaba Cloud engineer for access. Supported regions: Malaysia (Kuala Lumpur), UK (London), China (Chengdu), US (Virginia), US (Silicon Valley), China (Hong Kong), and Singapore.

  • Call CreateVpnGateway to create a Standard VPN Gateway.

Next Steps

To enable communication between the cloud VPC and the on-premises IDC, after the VPN Gateway instance is created, you also need to:

  1. Customer Gateway: Register the information of the on-premises gateway device (such as IP address and BGP AS number) to Alibaba Cloud.

  2. Create an IPsec-VPN connection: Create an IPsec encrypted tunnel between the Alibaba Cloud VPN Gateway and the on-premises gateway device to ensure that the connection can be established properly.

1.2. Create an SSL server

  1. Log on to the VPN Gateway console.

  2. In the left-side navigation pane, choose Network Interconnection > VPN > SSL Servers.

  3. In the top navigation bar, select the region where you want to create the SSL server.

    The SSL server must be in the same region as the associated VPN Gateway instance.

  4. On the SSL Servers page, click Create SSL Server.

  5. In the Create SSL Server panel, configure the following parameters and click OK.

    Parameter

    Description

    Name

    Enter a name for the SSL server.

    Resource Group

    Select the resource group of the VPN Gateway instance.

    The SSL server is added to the same resource group as the gateway.

    The access scope of the SSL-VPN connection is determined by the local network (based on the VPC CIDR block) and is unrelated to the resource group. Resources within the same VPC can be accessed over SSL-VPN regardless of the resource group they belong to.

    VPN Gateway

    Select the VPN Gateway instance.

    The selected instance must have SSL-VPN enabled.

    Local Network

    The CIDR blocks that clients can access through the SSL-VPN connection.

    A local network can be the CIDR block of a VPC, a vSwitch, an on-premises data center connected to a VPC through Express Connect, or a cloud service such as Object Storage Service (OSS) or ApsaraDB RDS.

    Click Add Local CIDR Block to add multiple CIDR blocks. You can add up to five local networks. The following IP address ranges are not supported:

    • 127.0.0.0 to 127.255.255.255

    • 169.254.0.0 to 169.254.255.255

    • 224.0.0.0 to 239.255.255.255

    • 255.0.0.0 to 255.255.255.255

    Note

    The subnet mask of a local network must be 8 to 32 bits in length.

    Client CIDR Block

    The IP address pool from which the VPN Gateway assigns addresses to clients. Not the private CIDR block of the client.

    The client CIDR block must provide at least four times the number of IP addresses as the maximum concurrent SSL connections supported by the VPN Gateway instance.

    • Click to view the reason.

      For example, if you specify 192.168.0.0/24 as the client CIDR block, the system allocates a /30 subnet, such as 192.168.0.4/30, for each connection. One IP address from this subnet is assigned to the client, and the other three are reserved by the system for network communication. Therefore, each client connection consumes four IP addresses from the pool. To ensure that all clients can obtain an IP address, the total number of IP addresses in your client CIDR block must be at least four times the maximum number of SSL connections for the VPN Gateway instance.

    • Click to view unsupported CIDR blocks.

      • 100.64.0.0 to 100.127.255.255

      • 127.0.0.0 to 127.255.255.255

      • 169.254.0.0 to 169.254.255.255

      • 224.0.0.0 to 239.255.255.255

      • 255.0.0.0 to 255.255.255.255

    • Click to view recommended client CIDR blocks for different numbers of SSL connections.

      • For 5 SSL connections, we recommend a subnet mask of 27 bits or shorter. Example: 10.0.0.0/27 or 10.0.0.0/26.

      • For 10 SSL connections, we recommend a subnet mask of 26 bits or shorter. Example: 10.0.0.0/26 or 10.0.0.0/25.

      • For 20 SSL connections, we recommend a subnet mask of 25 bits or shorter. Example: 10.0.0.0/25 or 10.0.0.0/24.

      • For 50 SSL connections, we recommend a subnet mask of 24 bits or shorter. Example: 10.0.0.0/24 or 10.0.0.0/23.

      • For 100 SSL connections, we recommend a subnet mask of 23 bits or shorter. Example: 10.0.0.0/23 or 10.0.0.0/22.

      • For 200 SSL connections, we recommend a subnet mask of 22 bits or shorter. Example: 10.0.0.0/22 or 10.0.0.0/21.

      • For 500 SSL connections, we recommend a subnet mask of 21 bits or shorter. Example: 10.0.0.0/21 or 10.0.0.0/20.

      • For 1,000 SSL connections, we recommend a subnet mask of 20 bits or shorter. Example: 10.0.0.0/20 or 10.0.0.0/19.

    Important
    • The subnet mask of the client CIDR block must be 16 to 29 bits in length.

    • The client CIDR block must not overlap with the Local Network, the VPC CIDR block, or any CIDR blocks routed on the client.

    • Use private CIDR blocks such as 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16, or their subnets. To use a public IP range, configure it as a user-defined CIDR block in the VPC for proper routing. VPC FAQ and VPC FAQ.

    • After you create the SSL server, the system adds a route for the client CIDR block to the VPC route table. Do not manually add this route, as it may disrupt SSL-VPN traffic. If a vSwitch is associated with a custom route table, the system does not automatically add the route for the client CIDR block to that custom route table. You must manually add a route entry whose destination CIDR block is the client CIDR block and whose next hop is the VPN gateway. Otherwise, hosts in the CIDR blocks associated with the custom route table cannot communicate with SSL-VPN clients.

    Advanced Configuration

    Protocol

    The protocol for the SSL-VPN connection. Valid values:

    • UDP

    • TCP (default)

    Port

    The port used by the SSL server. Valid values: 1 to 65535. Default value: 1194.

    Note

    The following ports are not supported: 22, 2222, 22222, 9000, 9001, 9002, 7505, 80, 443, 53, 68, 123, 4510, 4560, 500, and 4500.

    Encryption Algorithm

    The encryption algorithm for the SSL-VPN connection.

    • If the client runs Tunnelblick or OpenVPN 2.4.0 or later, the server and client dynamically negotiate the encryption algorithm, prioritizing the most secure mutually supported option. The algorithm specified here does not take effect.

    • If the client runs OpenVPN earlier than 2.4.0, the specified algorithm is used. Supported algorithms:

      • AES-128-CBC (default)

      • AES-192-CBC

      • AES-256-CBC

      • none

        Disables encryption.

    Compressed

    Whether to compress SSL-VPN traffic. Valid values:

    • Yes

    • No (default)

    Two-factor Authentication

    Whether to enable two-factor authentication. Disabled by default.

    Two-factor authentication requires clients to pass both SSL certificate verification and username/password authentication against an IDaaS EIAM instance. SSL-VPN two-factor authentication.

    When enabled, select an IDaaS EIAM instance and application ID for authentication.

    Click to learn about the two-factor authentication process

    image
    1. The client initiates an SSL-VPN connection request.

    2. After verifying the certificate, the VPN Gateway instance prompts the client for a username and password.

    3. After you enter the username and password on the client, the VPN software sends the credentials to the VPN Gateway instance.

    4. The VPN Gateway instance forwards the credentials to IDaaS for authentication.

    5. IDaaS authenticates the username and password and returns the result to the VPN Gateway instance.

    6. Based on the result from IDaaS, the VPN Gateway instance either allows or denies the SSL-VPN connection.

    Note
    • For first-time use, grant the required permissions through Authorization.

    • When creating an SSL server in the UAE (Dubai) region, bind an IDaaS EIAM 2.0 instance in the Singapore region to reduce cross-region latency.

    • IDaaS EIAM 1.0 instances are no longer available for purchase. If one exists in your account, you can still bind it after enabling two-factor authentication.

      If no IDaaS EIAM 1.0 instance exists in your account, only IDaaS EIAM 2.0 instances can be bound.

    • Binding an IDaaS EIAM 2.0 instance may require a VPN Gateway upgrade. [Change Notice] SSL-VPN two-factor authentication supports IDaaS EIAM 2.0.

    Custom Client DNS

    Configure DNS settings for all clients centrally. Available in some regions only. Configure DNS settings for clients.

1.3. Create an SSL client certificate

  1. Log on to the VPN Gateway console.

  2. In the navigation pane on the left, choose Interconnections > VPN > SSL Clients.

  3. In the top navigation bar, select the region of the SSL client.
  4. On the SSL Clients page, click Create SSL Client.

  5. On the Create SSL Client panel, configure the SSL client certificate based on the following information, and then click OK.

    Configuration

    Description

    Name

    Enter a name for the SSL client certificate.

    Resource Group

    Select the resource group of the SSL server.

    The SSL client certificate must belong to the same resource group as the SSL server.

    SSL Server

    Select the SSL server to associate with this certificate.

1.4. Download the SSL client certificate

  1. Log on to the VPN Gateway console.

  2. In the navigation pane on the left, choose Interconnections > VPN > SSL Clients.

  3. In the top navigation bar, select the region of the SSL client.
  4. On the SSL Clients page, find the SSL client certificate that you want to download and click Download Certificate in the Actions column.

1.5. Configure the client

  1. Download and install an OpenVPN client.

  2. Decompress the downloaded certificate package and copy the files to the config folder in the OpenVPN installation directory.

  3. Click Connect to initiate a connection.

1.6. Test the connection

  1. Create an ECS instance in the same VPC.

  2. In the OpenVPN client, run the ping command to test the connectivity to the ECS instance.

    Note
    • Make sure that the security group rules for the ECS instance allow remote client connections. Set the authorization object to the client CIDR block that you specified in the SSL server configuration. You must also specify the service port of the on-premises database you want to access. For more information, see Security group configuration examples.

    • If the connection fails, a firewall on the local host might be blocking it. You must configure the firewall to allow remote connections.

Step 2: Create a linked server

Using SSMS

  1. Connect to an ApsaraDB RDS for SQL Server instance by using SQL Server Management Studio (SSMS).

  2. In SSMS, run the following SQL command to create a linked server:

    DECLARE
        @linked_server_name sysname = N'yangzhao_slb', -- The name of the linked server, used to identify the remote server.
        @data_source sysname = N'****.sqlserver.rds.aliyuncs.com,3888 ', -- The IP address and port of the self-managed SQL Server database. Format: <IP>,<Port>. Example: 10.1.10.1,1433.
        @user_name sysname = N'ay15', -- The username to connect to the self-managed SQL Server database.
        @password nvarchar(128) = N'******', -- The password for the specified username.
        -- Other options for the linked server, provided in XML format.
        @link_server_options xml = N'
            <rds_linked_server>
                <config option="data access">true</config>
                <config option="rpc">true</config>
                <config option="rpc out">true</config>
            </rds_linked_server>'
    ;
    
    -- Call the sp_rds_add_linked_server stored procedure to create the linked server.
    EXEC sp_rds_add_linked_server
        @linked_server_name,
        @data_source,
        @user_name,
        @password,
        @link_server_options;
  3. Run the following SQL command to view the list of configured linked servers:

    SELECT * FROM [yangzhao_slb].master.sys.servers;

    测试Linked Server

Using DMS

  1. Connect to an ApsaraDB RDS for SQL Server instance by using DMS.

  2. In DMS, run the following SQL command to create a linked server:

    -- Call the sp_rds_add_linked_server stored procedure to create the linked server.
    EXEC sp_rds_add_linked_server
        @linked_server_name = N'yangzhao_slb', -- The name of the linked server, used to identify the remote server.
        @data_source = N'10.1.10.1,1433',   -- The IP address and port of the self-managed SQL Server database. Format: <IP>,<Port>. Example: 10.1.10.1,1433.
        @user_name = N'ay15', -- The username to connect to the self-managed SQL Server database.
        @password = N'******', -- The password for the specified username.
        -- Other options for the linked server, provided in XML format.
        @link_server_options = N'
            <rds_linked_server>
                <config option="data access">true</config>
                <config option="rpc">true</config>
                <config option="rpc out">true</config>
            </rds_linked_server>'
    ;
  3. Run the following SQL command to view the list of configured linked servers:

    SELECT * FROM [yangzhao_slb].master.sys.servers;

FAQ

Query performance variation

  • Scenario: After you enable a linked server between two ApsaraDB RDS for SQL Server instances in the same region and VPC but in different zones, the execution time for the same query statements increases. When you test query performance using different methods, such as querying from an ECS instance in the same zone, querying from DMS, or querying another RDS instance in a different zone by using a linked server, the query performance varies. The following examples show the performance of different query methods:

    • Querying an RDS instance by using an SSMS connection: No cross-instance network transmission is involved. This method provides the lowest latency and the fastest speed.

    • Querying an RDS instance by using DMS: DMS returns only a small amount of data (up to 3,000 rows are returned). Because the amount of transmitted data is small, the query is fast.

    • Querying an RDS instance from an ECS instance in the same zone: The network latency within the same zone is low, resulting in better performance than cross-zone queries.

    • Cross-zone query between RDS instances: This involves cross-zone network transmission. The network latency is high and the data volume is large, resulting in poor performance.

  • Cause analysis: Network latency and data volume affect query performance. Query speeds rank as follows: local query > same-zone query > cross-zone query. The details are as follows:

    • Network latency: The network latency of cross-zone queries is higher than that of same-zone queries.

    • Data volume: The larger the amount of data transmitted, the more significant the impact of network latency on performance.

  • Performance optimization suggestions:

    • Avoid cross-zone queries whenever possible. Prioritize deploying instances in the same zone. To change the zone of an ApsaraDB RDS for SQL Server instance, see Migrate an instance across zones.

    • Reduce the amount of transmitted data and optimize query logic, for example, by adding filter conditions or using paged queries.

Related operations