Getting Microsoft SQL Server and mySQL to talk
May 13, 2007 by technikhil
One of the biggest things I have to do as part of my company’s transition to using mySQL is the migration of the existing data to the mySQL platform. The problem was getting Microsoft’s SQL Server 2005 and mySQL to talk in a straightforward and flexible manner. The main issue is that SQL Server 2005 does not provide ODBC drivers out of the box.
This IMO is a big backward step for SQL Server 2005. If you use the SQL Server Management Studio interface there is .NET ODBC driver available as a data-source when trying to import data but when you try to make ODBC as your data-sink these options are no longer available. You get this issue when using SSIS also so I went on another tack - linked servers.
Linked servers are simply connections to other data sources or data sinks that are maintained by the Microsoft SQL Server. These can be local or remote and are a flexible and powerful way of making data that is spread across multiple servers available. You big advantage is that you could address the data with SQL statements directly as if they were on the same machine. Linked Servers could be ODBC data-sources as long as the appropriate ODBC drivers were available.
So I went to mySQL and got the mySQL ODBC Driver. After installing the driver on the server (mySQL has detailed installation instructions here), setting up the linked server was pretty straightforward -
EXEC master.dbo.sp_addlinkedserver @server = N’MYSQL’, @srvproduct=N’MySQL’, @provider=N’MSDASQL’, @provstr=N’DRIVER={MySQL ODBC 3.51 Driver}; SERVER=127.0.0.1; DATABASE=DB_Name; USER=UserName; PASSWORD=Password; OPTION=3′
Now you have set up a linked mySQL server using ODBC. Since this is an ODBC connection selecting and inserting data to and from it is slightly different. You use the openquery function like so -
Selecting Data -
select * from openquery(MySQL, ’select * from `db_name`.`table_name`’)
Inserting Data -
INSERT into openquery(MySQL,’select * from db_name.table_name’)
select * from db_name.dbo.table_name






I have been looking for detailed instructions to do exactly this for a very long time. Thanks for the post.
HELP!
I have gotten as far as getting the server to connect via a linked server. I set all the necessary permissions for the connections but keep getting the following error
Invalid use of schema or catalog for OLE DB provider “MSDASQL” for linked server “ECWSOURCE”. A four-part name was supplied, but the provider does not expose the necessary interfaces to use a catalog or schema.
???
Any ideas…
Nothing like dedication to ones job…
I figured this out with a little headache some asprin and SQL Server Books Online. BTW THANKS for the connection via linked server idea. I was fighting with SSIS and .NET… Nothing like transact SQL Statements to make our lives easier..
You said it man - the fancy graphical user interface is spiffy but when it gets down to it T-SQL is where its at…
I’m glad I could at least get you started on the right path.
It seems like if SQL server 2005 can’t insert data in Mysql :
I use MySQL ODBC 5.1 Driver to connect to Mysql 5 works fine to select but unable to insert…