Tải bản đầy đủ - 0 (trang)
4-9. Importing MySQL Data on an “Ad Hoc” Basis

4-9. Importing MySQL Data on an “Ad Hoc” Basis

Tải bản đầy đủ - 0trang

Chapter 4 ■ SQL Databases



Figure 4-21.  Configuring a MySQL linked server

6.



Click Security in the list on the left. Select the “Be made without using a Security

Context” radio button. The logon defined in the DSN provides the user ID and

password (see Figure 4-22).



212

www.it-ebooks.info



Chapter 4 ■ SQL Databases



Figure 4-22.  Linked server properties for MySQL

7.



Click OK.



You can now select data from the linked server using a standard four-part SELECT statement, like this:

SELECT * FROM MYSQLLINK...INFORMATION_SCHEMA.TABLES.



How It Works

By using ODBC, you can create a linked server to a MySQL database. In my opinion, while it is easier to define a

DSN first and use this as the data source, you can create a “DSN-less” linked server by supplying a full provider

string if you so prefer.

If you are having difficulties with certain data types, you might need to tweak the MSDASQL (Microsoft

OLEDB provider for ODBC drivers) properties. To do this, do the following:

1.



Expand Server Objects ➤ Linked Servers ➤ Providers. Then, right-click MSDASQL

and select properties. You will see a dialog box like the one shown in Figure 4-23.



213

www.it-ebooks.info



Chapter 4 ■ SQL Databases



Figure 4-23.  Configuring provider options for the Microsoft OLEDB Provider for ODBC

2.



Check Zero Level Only and/or Allow Inprocess. Click OK.



■■Note Remember that you are altering MSDASQL for all ODBC over OLEDB connections—so be careful that you

do not prevent another connection from working by modifying the Provider properties!

Now that the linked server is set up, it makes using OPENQUERY to send pass-through queries a breeze.

Use a simple code snippet like (C:\SQL2012DIRecipes\CH02\MySQLOpenQuery.sql):

SELECT * FROM OPENQUERY(MYSQLLINK, 'SELECT * FROM INFORMATION_SCHEMA.TABLES');



214

www.it-ebooks.info



Chapter 4 ■ SQL Databases



You can also use this DSN with ad hoc (OPENROWSET) queries—assuming that ad hoc queries have been

enabled as described in Recipe 1-4. This is one example: (C:\SQL2012DIRecipes\CH02\MySQLOpenRowset.sql):

SELECT * FROM OPENROWSET(N'MSDASQL', 'DSN = MySQLAdam',

'SELECT * FROM INFORMATION_SCHEMA.TABLES) AS A



Hints, Tips, and Traps





OK, so I was playing fast and loose with database security. For greater security on the

Security pane, select the Not Be Made radio button and then click Add. Set up a login

mapping between an SQL Server login and the MySQL user. The dialog box should look

like Figure 4-24.



Figure 4-24.  Configuring a security context for a MySQL linked server



215

www.it-ebooks.info



Chapter 4 ■ SQL Databases



■■Note  Once again, using SELECT * is not for production environments. It can be very useful when getting an

initial idea of the source data, however.



4-10. Importing MySQL Data Using SSIS 2005 and 2008

Problem

You are still using SQL Server 2008 (or even 2005) and need to import MySQL data.



Solution

Use ODBC over ADO.NET instead of using the Attunity ODBC connection manager, which only became available

as standard in SQL Server 2012. The following explains how to configure it.

1.



In a new or existing SSIS package, right-click in the Connection Managers tab. Select

New ADO.NET connection.



2.



Click New. Select the ODBC Data Provider from the pop-up list of providers, as shown

in Figure 4-25.



Figure 4-25.  Using an ODBC provider with an ADO.NET connection

3.



Click OK. Select the System (or User) DSN that you created previously (see Recipe 4-8).



4.



Click OK twice to complete the creation of the MySQL Connection Manager.



5.



Add a Data Flow task. Double-click to edit.



6.



Add an ADO.NET data source. Double-click to edit.



7.



Set the ADO.NET connection manager to the name that you created previously.



216

www.it-ebooks.info



Chapter 4 ■ SQL Databases



8.



Set the data access mode to SQL command. Enter the SQL to determine the source data.



9.



Click OK to confirm. Continue with your package creation.



How It Works

Should you be using SQL Server 2005 or 2008, then the approach to using the DSN is very slightly different, since

you have to use ODBC over ADO.NET. You will still have to configure a DSN, but fortunately this is the same using

any version of SQL Server because it depends on the MySQL ODBC driver.



Hints, Tips, and Traps





The same hints apply to using previous versions of SSIS as those given for SSIS 2012. This

is because they all concern the ODBC driver, and not SSIS.



4-11. Migrating Complete Tables from MySQL

Problem

You want to migrate multiple complete tables from several MySQL databases in a single, simple operation.



Solution

Use SSMA for MySQL to migrate tables from multiple tables and/or databases at once.

1.



Download the latest version of the SQL Server Migration Assistant for Sybase. It is

currently version 5.2, available at www.microsoft.com/en-us/download/details.

aspx?id=28764.



2.



Install SSMA and the SSMA extension pack. This is described in Recipes 4-5 and 4-13.



3.



Install the latest version of the MySQL ODBC driver.



4.



Run SSMS. Download and refresh the (free) license key.



You can now carry out a data migration exactly as described in Recipes 4-5 and 4-13. Without all the detail

(and to save you jumping from recipe to recipe), this means you need to do the following:

1.



Create a new project and save it.



2.



Connect to MySQL. Drill down to the databases and table(s) that you wish to transfer.



3.



Connect to SQL Server.



4.



Convert the source schema.



5.



Synchronize the schema with that of the destination database (this will create any

required tables, if they do not already exist).



6.



Migrate the data.



217

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

4-9. Importing MySQL Data on an “Ad Hoc” Basis

Tải bản đầy đủ ngay(0 tr)

×