How to add mysql linked server to SQL Server database (Windows system)

1. Description

Typically, linked servers are configured to enable the database engine to execute Transact-SQL statements containing tables on other SQL Server instances or other database products such as Oracle. Many types of data sources can be configured as linked servers, including third-party database providers and Azure CosmosDB

2. Download the odbc driver for mysql

The official website download address
is generally to choose the latest version to download.
To choose 64-bit or 32-bit, make sure it is consistent with your sqlserver version. Use the following sql to check:
Insert image description here

3. Install mysql odbc

  Double-click the msi file to install, but you may be prompted to install Redistributable for Visual Studio, causing the installation to fail. You can download and install it from the official website.

4. Configure ODBC

4.1 Control Panel→ODBC Data Source (64-bit)→Double-click to open

Insert image description here

4.2 Add msql odbc data source

Click System DSN→Add→MySQL ODBC x.1 Unicode Driver.
Insert image description here
After clicking Finish, the mysql link page will pop up.
Insert image description here

5. Test whether the addition is successful

Insert image description here
Note : Problems that the root account may encounter: Connection Failed error. As shown below.
Insert image description here
Solution:
This error is due to a problem with the root user. The root user defaults to the localhost identity and does not support remote connections. Need to modify according to the following sql:

//如果报了上面的错,那第一次执行这个sql显示的应该是localhost,那就执行下面的update语句
select host from user where user = 'root'
//执行完这个update语句一定要重启mysql服务
update user set host = '%' where user = 'root';

6. Open SSMS and add a linked server

6.1 General

Linked server: The name can be customized for subsequent calls and access.
The data source is the name of odbc, such as here. I should fill in xntest as here.
Insert image description here

6.2 Password

Insert image description here
Insert image description here
After the above two steps, the connection can be successful. There is no success window if the connection is successful.

7. linkedserver query statement

7.1 Access using OPENQUERY

-- mysqltest为链接服务器名称
-- 里面的select需要使用''引起来,并且如果寻找where条件的字符还需要再添加一次引号
select * from openquery(mysqltest,'select * from table1 where id = ''1''')

Insert image description here

Guess you like

Origin blog.csdn.net/slb190623/article/details/132132909