Tag: Linked Servers

  • Creating a Linked Server with a MySQL Database

    Creating a Linked Server with a MySQL Database

    This is my 3rd Linked Server demo post, and this time it’s to a MySQL database.

    Noteworthy weblinks:
    # My Linked Server Blog Posts.
    # Microsoft Docs – Linked Servers.

    Here is a simple diagram of the setup in this post.

    The above was setup similar to what’s included in some of my previous posts:
    # Install SQL Server on Windows.
    # Install MySQL on Windows.
    # Test Connectivity to Remote Servers.

    Once the above is done, this post runs through the following steps to get a Linked Server configured:

    MySQL
    # Create MySQL Linked Server Login.
    SQL Server
    # Configure ODBC Data Source Settings.
    # Create a Linked Server to a MySQL Database.


    Create Linked Server Login on MySQL

    As always, this kind of task can be done using a management tool like MySQL WorkBench or via command.

    First up, I’ll get connected to MySQL via CLI.

    CREATE USER and then GRANTING SELECT capabilities for all tables* within a ‘milk’ database.  The first SQL steps here for me are to create the test database & table.

    -- Create test database & table
    CREATE DATABASE milk;
    USE milk;
    CREATE TABLE animal (id INT, type VARCHAR(50), breed VARCHAR(50), age DATE);
    INSERT INTO animal (1,'cow','white with black spots','2009-10-06');
    INSERT INTO animal VALUES (2,'cow','black with white spots','2011-12-16');
    
    -- Create Linked Server user
    CREATE USER 'linked_server_sql1'@'172.31.1.%' IDENTIFIED BY 'eCh0Ch4rl1E';
    GRANT SELECT ON milk.* TO 'linked_server_sql1'@'172.31.1.%';

    My connections are within the 172.31.1.0 network, so I’m adding in a wildcard (%) for all addresses on this range to be allowed to connect with this login.

    If I want to test this new MySQL login locally, I can create the user using ‘localhost’ too. Below I’m opening the mysql.exe file rather than using the MySQL Command Line Client application which logs in as root.

    Put on the User hat and do what you need to do.

    I only wanted read-only access, which I can confirm with a quick write test.

    That’s our Linked Server MySQL login ready!

    The above can also be done with a tool like MySQL WorkBench too; run the same commands above in a query window or via the Users and Privileges window. The new user account below would be able to connect from any host address.


    Configure ODBC Data Source Settings

    The MySQL ODBC driver download can be found here. Download & install on the SQL Server host machine.

    Note; at the time of writing this the most recent MySQL ODBC Driver version is 8.0.20. I’ve had to install version 8.0.18 as the 2 most recent versions were showing an error message on install.

    Run ODBC Data Sources as Administrator.

    Click into the System DSN tab and click Add.

    Select the MySQL ODBC Unicode driver.

    Enter all details required as shown. If you can see the database names the connection is all good so click OK.


    Create Linked Server to a MySQL Database

    Within SSMS; expand Server Objects, right-click Linked Servers and select New Linked Server.

    The Linked server name at the top of the next window is what you’ll need to include in queries, so choose that wisely.

    Then choose a provider for ODBC and enter the Data Source name which you named within the ODBC driver settings above.

    Enter the MySQL login details as created above within this post.

    I’m leaving all Server Options as defaults.

    Before I hit OK I’m scripting this out to code.

    USE [master]
    GO
    EXEC master.dbo.sp_addlinkedserver 
    	@server = N'MYSQL_SERVER_1', 
    	@srvproduct=N'', 
    	@provider=N'MSDASQL', 
    	@datasrc=N'MySQL_Linked_Server'
    
    USE [master]
    GO
    EXEC master.dbo.sp_addlinkedsrvlogin 
    	@rmtsrvname = N'MYSQL_SERVER_1', 
    	@locallogin = NULL , 
    	@useself = N'False', 
    	@rmtuser = N'linked_server_sql1', 
    	@rmtpassword = N'eCh0Ch4rl1E'
    GO

    When ready, take a breath, then hit OK or execute the SQL.

    Have a look in the SSMS Object Explorer to view the new Linked Server. We should be able to drill into the database & objects we have permission to.

    Now run queries to our MySQL Server using OPENQUERY.


  • Creating a Linked Server with a Postgres Database

    Creating a Linked Server with a Postgres Database

    In my previous post, I created a Link Server to another SQL Server instance This time, it’s a link to a Postgres database!

    This guide can be followed from start to finish using the links below.

    # Install SQL Server (separate post).
    # Install PostgreSQL (separate post).
    # Test Connectivity to Remote Server (previous Linked Server post).
    # Create Linked Server Login on Postgres.
    # Install the Postgres ODBC Driver.
    # Configure ODBC Data Source Settings.
    # Create Linked Server to a Postgres Database.


    Create Linked Server Login on Postgres

    As always, this kind of task can be done using a management tool like pgAdmin 4 or via command (psql in this case).

    Postgres Create New User

    Making this one a Superuser for quickness.

    Postgres User Options

    And for the post, here’s the above being created using psql.

    psql Create and View Users

    Install Postgres ODBC Driver

    The ODBC driver must be installed on both ends – This is a local set-up though, so I only need to install this once. You can use Stack Builder to install it, or download the package on its own (link here) which I’m doing below.

    Postgres ODBC Driver Download

    Click famous Next button.

    psqlODBC Setup Wizard

    Accept License stuff.

    psqlODBC License Agreement

    Specify directory & we don’t need the documentation.

    psqlODBC Feature Selection

    And then install!

    psqlODBC Installation
    psqlODBC Install Wizard

    Configure ODBC Data Source Settings

    We need to point the ODBC driver on the SQL Server to the Postgres database.

    Run ODBC Data Sources as Administrator.

    Run ODBC Source as Administrator

    Click into the System DSN tab and click Add.

    ODBC Data Source System DSN

    Select the PostgreSQL Unicode driver.

    Create New Postgres ODBC Data Source

    Enter all details required as shown. I created a ‘sales’ database for this test which I’m connecting to. 

    ODBC Data Source Settings for Postgres

    Always worth it to test.

    ODBC Test Connection

    Create Linked Server to a Postgres Database

    (Within SQL Server Management Studio’s Object Explorer)

    Expand Server Objects, right-click Linked Servers and select New Linked Server.

    SSMS New Linked Server

    The Linked server name at the top of the next window is what you’ll need to include in queries, so choose that wisely.

    Then choose a provider for ODBC, enter any semi-relevant Product name, and then enter the Data Source name which you entered within the ODBC driver settings.

    SSMS New Linked Server Postgres

    Enter the Postgres login details as created above within this post.

    SSMS Linked Server Login Details

    I’m ignoring the Server Options tab for now and rolling with the defaults.

    When ready, take a breath, then hit OK!

    Nothing will display if it’s successful, so have a go at testing the connection.

    SSMS Test Linked Server Connection
    Linked Server Connection Test

    We’re now linked with the Sales database that’s in Postgres!

    Linked Server Query with Postgres

  • Create a Linked Server to SQL Server

    Create a Linked Server to SQL Server

    Linked Servers in SQL Server allow you to query external databases, such as remote SQL Server instances, Oracle, ODBC or MS Access databases.

    This blog post contains a demo on how to create a Linked Server to another SQL Server, with links to Microsoft Documentation throughout.

    I also have more Linked Server demos if of interest:
    -> Create a Linked Server with a MySQL Database
    -> Create a Linked Server with a Postgres Database

    The following steps should help walk you through creating a Linked Server to another MS SQL Server:
    # Test Connectivity to Remote Server
    # Create a SQL Login for Linked Server
    # Create a Linked Server to SQL Server

    Test Connectivity to Remote Server

    An important step before we begin any SQL Server stuff, we should confirm line-of-sight to the remote server. On this local setup, I have nothing to worry about, but if configuring this in the real world, you may have to arrange network rules to be added.

    The network information we’ll need is the server address (IP Address or FQDN) and port number. The SQL Server default port is 1433, so we just need the server address to test the connection using PowerShell’s Test-NetConnection cmdlet.

    The SQL Server default port is 1433, so we just need the server address to test the connection using PowerShell’s Test-NetConnection cmdlet.

    Test-NetConnection PowerShell

    Both the Ping and TCPTest failed as expected.

    Use this script if running PowerShell on an OS older than Windows 8/Windows Server 2012.

    Other note-worthy default database server ports include:
    # PostgreSQL (5432)
    # MySQL (3306)
    # Oracle (1521)
    # Sybase (5000)
    # DB2 (50000)

    Create a SQL Login for the Linked Server

    We need an account on the server we’re linking to. I’m using my SQL Server 2019 (PETE-PC\SQL2019) as my master server, creating the new login on the PETE-PC instance as shown on the image below.

    Create Linked Server Login

    I’m selecting SQL Server authentication as I’m not on a domain and unticking the password reset on the next login.

    Linked Server Login Details

    I’ll test a read-only case by selecting db_datareader on the AdventureWorks database.

    Linked Server Login Permissions

    Once we click OK the account is ready for use.

    Create a Linked Server to SQL Server

    Open Server Objects, right-click Linked Servers and select New Linked Server.

    Create Linked Server SSMS

    Select SQL Server as the server type and enter the ‘remote’ server name

    New Linked Server

    Into the Security tab next, enter the remote login details (as created above).

    New Linked Server Login SSMS

    We can hit OK, or script it out before running. This is a condensed version.

    Create Linked Server SQL Script

    TSQL can be found here.

    The 3 system stored procedures used are:

    # sp_addlinkedserver
    # sp_serveroption
    # sp_addlinkedsrvlogin

    We can verify the link by querying the new linked database.

    Query Linked Server

    And finally, verify permissions.

    Linked Server Permission Denied