Creating a Linked Server with a MySQL Database

This is my third Linked Server demo post, and this time, we’re connecting to a MySQL database.

Here’s a simple diagram of the setup covered in this post, configuring a Linked Server to a MySQL Database:

Linked Server to MySQL

For this setup, I’ve covered it all in previous blog posts:
Installing SQL Server on Windows
Installing MySQL on Windows
Testing Connectivity to Remote Database Server Ports

All of the above are necessary prerequisites for configuring this MySQL Linked Server. You could run on Linux for both MySQL and SQL Server (as from SQL 2017), but for this demo I’ve gone with a Windows Server setup.

The following is included in this guide to setup a Linked Server to MySQL:
Step 1: Create MySQL Linked Server Login
Step 2: Configure ODBC Data Source Settings
Step 3: Create a Linked Server to a MySQL Database
Step 4: Verify MySQL Linked Server Connectivity


Step 1: Create Linked Server Login on MySQL

You can create a MySQL user either through MySQL Workbench or via the command line. For this demo, I’ll use the MySQL CLI.

1. First, connect to MySQL:

2. Create MySQL User with Permissions
In the example below, I’m creating a user and GRANTING SELECT capabilities for all tables within the database.

-- Create Linked Server user
CREATE USER 'linked_server_sql1'@'172.31.1.%' IDENTIFIED BY 'eCh0Ch4rl1E';

-- Grant SELECT on all tables in milk from specific user subnet
GRANT SELECT ON milk.* TO 'linked_server_sql1'@'172.31.1.%';
Create MySQL User for Linked Server

This grants read-only access to all tables in the milk database for connections coming from the 172.31.1.0 network range. If testing locally, replace 172.31.1.% with localhost.

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 app which logs in as root.

MySQL Windows localhost Test

Now querying the database I want as part of the Linked Server:

MySQL Query Test

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

MySQL Write Permissions Test

We could have also done this testing by logging in via MySQL Workbench.

That’s our Linked Server MySQL login tested and ready!

Step 2: Configure ODBC Data Source on SQL Server

1. Download & Install MySQL ODBC Driver
The latest MySQL ODBC driver can be found here.

Install MySQL ODBC Connector Windows

2. Run ODBC Data Sources as Administrator.

Open ODBC DataSources (64-bit) as Admin

3. Navigate to the System DSN tab and click Add.

ODBC Data Source Administrator

4. Select MySQL ODBC Unicode Driver.

MySQL ODBC Unicode Driver Windows Setup

5. Enter the connection details:
Server: MySQL_IP_or_ServerName
User: MySQL_LinkedServer_Login_01
Password: SecurePassword123
Database: DatabaseName01

MySQL Connection Configuration

6. Click Test to ensure successful connectivity, then click OK.

MySQL Data Source Add

Step 3: Create Linked Server in SQL Server

1. In SSMS, expand Server Objects, right-click Linked Servers, and select New Linked Server.

SSMS New Linked Server

2. Configure the following:
Linked Server Name: Choose a meaningful name (e.g., MYSQL_SERVER_1).
Provider: Select Microsoft OLE DB Provider for ODBC Drivers.
Data Source: Enter the ODBC Data Source name configured earlier.

Configuring MySQL Linked Server SSMS

3. Under Security, enter the MySQL login details created earlier.

Linked Server Login Configuration

4. Leave Server Options as defaults.

SSMS New Linked Server Options

Before we proceed and create this Linked Server, we can script out the Linked Server creation:

Script Out MySQL Linked Server
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

Once ready, execute the script or click OK in the Linked Server configuration window.

Step 4: Verify Linked Server & Run Queries

1. Check Linked Server in SSMS
– In SSMS Object Explorer, navigate to Server Objects → Linked Servers.
– Expand the new Linked Server and browse the database objects.

Linked Server Objects in SQL Management Studio

2. Query MySQL Database Using OPENQUERY
The query below executes against MySQL from SQL Server. If everything is configured correctly, you should see your test data.

-- MySQL Linked Server Example Query
SELECT * FROM OPENQUERY(MYSQL_SERVER_1, 'SELECT * FROM milk.animal');

Checkout the MS Docs for more information on OPENQUERY.

SQL Server OPENQUERY Example MySQL

I hope this configuration guide was a good one for you, and you got SQL Server setup with a Linked Server to your MySQL Server.

If you have any troubles along the way feel free to comment below and I’ll try help!


Comments

3 responses to “Creating a Linked Server with a MySQL Database”

  1. PIYUSH PATHAK Avatar
    PIYUSH PATHAK

    Very Nice Peter!! Very Helpful.

  2. Joe Avatar
    Joe

    Can a linked server (MSSQL -> MySQL) be created without ODBC install? Specifically in AWS, where both machines are on RDS. I don’t believe ODBC can be installed in that environment.

    1. pete Avatar
      pete

      Hi Joe, sorry delayed reply.

      RDS should have built-in drivers for connectivity for Linked Servers. I would imagine that’s the case but I’ve not yet configured RDS MSSQL > MySQL.

      https://aws.amazon.com/blogs/database/configure-linked-servers-on-amazon-rds-custom-for-sql-server/
      I’d run though this and then reach out to AWS Support if there’s issues.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts
Categories
Tags

Always On Availability Groups (AAG) (4) AWS (4) AWS Redshift (6) Database Admin (72) Database Backups & Recovery (14) Database Mirroring (2) Error Messages (5) Failover Cluster Instances (FCI) (1) Git Commands (6) Importing & Exporting Data (2) Linked Servers (3) Linux Administration (2) Logging & Monitoring (1) Microsoft Patching (2) MySQL (4) Postgres (6) PowerShell Scripts (2) SQL Certificates & Encryption (3) SQL Server Agent (5) SQL Server CDC (2) SQL Server Data Types (2) SQL Server Management Studio (SSMS) (17) SQL Server Networking (3) SQL Server on Linux (1) SQL Server Patching (2) SQL Server Performance Tuning (6) SQL Server Processes (SPIDs) (7) SQL Server Replication (2) SQL Server Scripts (13) SQL Server Security (4) SQL Server Storage (10) Windows Admin (20) Windows Authentication (2) Windows Automation (1) Windows Events (2) Windows Firewall (4) Windows Subsystem for Linux (WSL) (18)