Migrate MySQL to Microsoft SQL Server

Migrating MySQL Databases to SQL Server / SQL Azure

To successfully migrate objects and data from MySQL databases to SQL Server or SQL Azure, use the following process:

  1. Working with SSMA Projects (MySQL to SQL).

    After you create the project, you can set project conversion, migration, and type mapping options. For more information about project settings, see Setting Project Options (MySQL to SQL). For information about how to customize data type mappings, seeMapping MySQL and SQL Server Data Types (MySQL to SQL)

  2. Connecting to MySQL (MySQL to SQL)
  3. Connecting to SQL Server(MySQL to SQL)
  4. Mapping MySQL Databases to SQL Server Schemas(MySQLToSQL)
  5. Connecting to SQL Azure
  6. Optionally, Assessing MySQL Databases for Conversion (MySQL to SQL) to assess database objects for conversion and estimate the conversion time.
  7. Converting MySQL Databases (MySQL to SQL)
  8. Synchronization
  9. You can do this in one of the following ways:
    • Save a script and run it on SQL Server or SQL Azure.
    • Synchronize the database objects.
  10. Migrating MySQL Data into SQL Server/ SQL Azure
  11. If necessary, update database applications.

 

Getting Started with SSMA for MySQL(MySQLToSQL)

SQL Server Migration Assistant (SSMA) for MySQL lets you quickly convert MySQL database schemas to SQL Server or SQL Azure schemas, upload the resulting schemas into SQL Server or SQL Azure, and migrate data from MySQL to SQL Server or SQL Azure.

This topic introduces the installation process, and then helps familiarize you with the SSMA user interface.

To use SSMA, you first must install the SSMA client program on a computer that can access both the source MySQL database and the target instance of SQL Server or SQL Azure. Then, install the MySQL providers (MySQL ODBC 5.1 Driver (trusted)) on the computer that is running SSMA Client Program. For installation instructions, see Installing SSMA for MySQL(MySqlToSql)

To start SSMA, click Start, point to All Programs, point to SQL Server Migration Assistant for MySQL, and then click SQL Server Migration Assistant for MySQL. The first time that you start SSMA, a licensing dialog box will appear. You must license SSMA by using a Windows Live ID before you can use SSMA. Licensing instructions are included with the installation instructions in the Installing SSMA for MySQL Client topic.

After SSMA is installed and licensed, you can use SSMA to migrate MySQL databases to SQL Server or SQL Azure. It helps to become familiar with the SSMA user interface before you start. The following diagram shows the user interface for SSMA, including the metadata explorers, metadata, toolbars, output pane, and error list pane:

SSMA for MySql Graphical User InterfaceTo start a migration, you must:

  1. Create a new project.
  2. Connect to a MySQL database.
  3. After a successful connection, MySQL schemas will appear in MySQL Metadata Explorer. Right-click objects in MySQL Metadata Explorer to perform tasks such as create reports that assess conversions to SQL Server/SQL Azure.

You can also perform these tasks by using the toolbars and menus.

You must also connect to an instance of SQL Server. After a successful connection, a hierarchy of SQL Server databases will appear in SQL Server Metadata Explorer. After you convert MySQL schemas to SQL Server schemas, select those converted schemas in SQL Server Metadata Explorer, and then synchronize the schemas with SQL Server.

You must connect to SQL Azure if you have selected SQL Azure from the Migrate to dropdown in new project dialog box. After a successful connection, a hierarchy of SQL Azure databases will appear in SQL Azure Metadata Explorer. After you convert MySQL schemas to SQL Azure schemas, select those converted schemas in SQL Azure Metadata Explorer, and then synchronize the schemas with SQL Azure.

After you synchronize converted schemas with SQL Server or SQL Azure, you can return to MySQL Metadata Explorer and migrate data from MySQL schemas into SQL Server or SQL Azure databases.

For more information about these tasks and how to perform them, see Migrating MySQL Databases to SQL Server / SQL Azure.

The following sections describe the features of the SSMA user interface.

Metadata Explorers

SSMA contains two metadata explorers to browse and perform actions on MySQL and SQL Server databases.

MySQL Metadata Explorer

MySQL Metadata Explorer shows information about MySQL schemas. By using MySQL Metadata Explorer, you can perform the following tasks:

SQL Server or SQL Azure Metadata Explorer

SQL Server or SQL Azure Metadata Explorer shows information about an instance of SQL Server or SQL Azure. When you connect to an instance of SQL Server or SQL Azure, SSMA retrieves metadata about that instance and stores it in the project file.

You can use this Metadata Explorer to select converted MySQL database objects, and then synchronize those objects with the instance of SQL Server or SQL Azure.

For more information, see Synchronization (MySQL to SQL Server / SQL Azure)

Metadata

To the right of each metadata explorer are tabs that describe the selected object. For example, if you select a table in MySQL Metadata Explorer, nine tabs will appear: Table, SQL, Type Mapping, Data, Settings, Charset Mapping, SQL Modes, Properties, andReport. The Report tab contains information only after you create a report that contains the selected object. If you select a table in SQL Server Metadata Explorer, three tabs will appear: Table, SQL and Data.

Most metadata settings are read-only. However, you can alter the following metadata:

  • In MySQL Metadata Explorer, you can alter type mappings, Charset Mapping, SQL Modes. To convert the altered type mappings or Charset Mapping or SQL Modes, make changes before you convert schemas.
  • In SQL Server Metadata Explorer, you can alter the table and index properties on the Table tab. To see these changes in SQL Server, make these changes before you load the schemas into SQL Server.

Changes made in a metadata explorer are reflected in the project metadata, not in the source or target databases.

Toolbars

SSMA has two toolbars: a project toolbar and a migration toolbar.

The Project Toolbar

The project toolbar contains buttons for working with projects, connecting to MySQL, and connecting to SQL Server or SQL Azure. These buttons resemble the commands on the File menu.

Migration Toolbar

The following table shows the migration toolbar commands:

Button Function
Create Report Converts the selected MySQL objects to SQL Server or SQL Azure objects, and then creates a report that shows how successful the conversion was.

This command is disabled unless objects are selected in MySQL Metadata Explorer.

Convert Schema Converts the selected MySQL objects to SQL Server or SQL Azure objects.

This command is disabled unless objects are selected in MySQL Metadata Explorer.

Migrate Data Migrates data from the MySQL database to SQL Server or SQL Azure. Before you run this command, you must convert the MySQL schemas to SQL Server or SQL Azure schemas, and then load the objects into SQL Server or SQL Azure.

This command is disabled unless objects are selected in MySQL Metadata Explorer.

Stop Stops the current process.

Menus

The following table shows the SSMA menus.

Menu Description
File Contains commands for working with projects, connecting to MySQL, and connecting to SQL Server or SQL Azure.
Edit Contains commands for finding and working with text in the details pages. To open Manage Bookmarks dialog, on the Edit menu click Manage Bookmarks. In the dialog you will see a list of existing bookmarks. You can use the buttons on the right side of the dialog to manage the bookmarks.
View Contains the Synchronize Metadata Explorers command. That synchronizes the objects between MySQL Metadata Explorer and SQL Server or SQL Azure Metadata Explorer. Also contains commands to show and hide the Output and Error List panes and an option Layouts to manage with the Layouts.
Tools Contains commands to create reports, convert schema, refresh from database, migrate objects and data, and Save as Script. Also provides access to the Global Settings, Default Project Settings and Project Settings dialog boxes.
Help Provides access to SSMA Help, to a dialog box for setting usage reporting options, and to the About dialog box.

Output Pane and Error List Pane

The View menu provides commands to toggle the visibility of the Output pane and the Error List pane:

  • The Output pane shows status messages from SSMA during object conversion, object synchronization, and data migration.
  • The Error List pane shows error, warning, and informational messages in a sortable list.

Introduction

This article describes a few simple steps in order to migrate MySQL into Microsoft SQL Server 2005. The technique is very easy, but useful if you plan to move your data from MySQL and upgrade it finally to a Microsoft SQL Server environment.

Background

Initially, I started my search for an article on CodeProject regarding MySQL->MS SQL migration without any success. I had an old PHPbb forum running, that needed to be upgraded to a Microsoft environment entirely. I could have just kept MySQL and Apache server, but instead I decided to migrate the entire concept of PHPbb to a YAF-forum.

Setup ODBC Connection to MySQL Database

This article will not go through how to setup a MySQL or Microsoft SQL server, but make sure you have downloaded at least the MySQL ODBC Connector from here.

For this article, I downloaded the MySQL ODBC Connector 5.1.

The setup of this connector is pretty simple:

  • Open your ODBC Data Source Administrator from the Control Panel -> Administrative Tools. Under the tab labelled as “System DSN”, press the “Add” button.Setup_ODBC1.jpg
  • On the “Create New Data Source” dialog that appeared, choose MySQL ODBC 5.1 Driver and then press the “Finish” button.Setup_ODBC2.jpg
  • After that, a MySQL connection configuration dialog will appear. Add your MySQL database account information in it, preferably the “root” account which has full access to your databases in MySQL. In this case, my database is called “tigerdb“. Do not change the port to anything other than 3306, unless during your MySQL server installation, you have defined something else.Setup_ODBC3.jpg
  • Press the “Test” button to ensure your connection settings are set properly and then the “OK” button when you’re done.

Create a Microsoft SQL Link to your MySQL Database

In this state, you are ready to establish a link towards MySQL database from your Microsoft SQL Server Management Studio. Open a query window and run the following SQL statement:

 Collapse | Copy Code
EXEC master.dbo.sp_addlinkedserver 
@server = N'MYSQL', 
@srvproduct=N'MySQL', 
@provider=N'MSDASQL', 
@provstr=N'DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; _
	DATABASE=tigerdb; USER=root; PASSWORD=hejsan; OPTION=3'

This script will produce a link to your MySQL database through the ODBC connection you just created in the previous stage of this article. The link will appear in the Microsoft SQL Server Management Studio like this:

Create_link.jpg

If it doesn’t show up in the treeview, press the refresh button.

Import Data between the Databases

Create a new database in Microsoft SQL Server. I called mine “testMySQL“. In the query window, run the following SQL statement to import table shoutbox from the MySQL database tigerdb, into the newly created database in Microsoft SQL called testMySQL.

 Collapse | Copy Code
SELECT * INTO testMySQL.dbo.shoutbox
FROM openquery(MYSQL, 'SELECT * FROM tigerdb.shoutbox')

That’s it!

Basic Steps for Migration of MySQL to SQL Server

To start a migration, you’ll need to perform the following high level steps:

  1. Create a new project.
  2. Connect to a MySQL database.
  3. After a successful connection, MySQL schemas will appear in MySQL Metadata Explorer. Right-click objects in MySQL Metadata Explorer to perform tasks such as create reports that assess conversions to SQL Server 2008 R2 Express. You can also perform these tasks by using the toolbars and menus.

You’ll then connect to your instance of SQL Server 2008 R2 Express. After a successful connection, a hierarchy of your existing databases will appear in SQL Server Metadata Explorer. After you convert MySQL schemas to SQL Server schemas, select those converted schemas in SQL Server Metadata Explorer, and then synchronize the schemas with SQL Server.

After you synchronize converted schemas with SQL Server 2008 R2 Express, you can return to MySQL Metadata Explorer and migrate data from MySQL schemas into target database.

Let’s walk through the specifics.

Create a MySQL Migration Project

To get started, you’ll create your new project using the File | New Project command.

02 Create Project

You’ll enter in your project name and then confirm that you are migrating to SQL Server. The Migrate To dropdown also allows you to choose SQL Azure, but that’s for another post. Once you make your selection, you are locked into the target backend.

Connect to a MySQL Database

To Connect to your MySQL instance, you’ll issue the File | Connect to MySQL command or click on the tool bar button that launches the following dialog:

03 Connect to MySQL

If you forgot to to install the MySQL ODBC driver mentioned at the beginning of this blog, simply go to the download site, install the driver, and then issue the Connect to MySQL command.

Create Report of Potential Conversion Issues

Once you are connected, you’ll see the MySQL instance in the MySQL Metadata Explorer. You’ll want to expand the Databases node along with the Sakila database node and then check the box next to Sakila. This selects the database you want to migrate. Next, right click on the Sakila database and select the Create Report command or press the Create Report command on the toolbar as shown below.

04 Create Report

Here is an example of the Assessment Report for the Sakila database.

05 Assessment Report

The Assessment Report window contains three panes:

  • The left pane contains the hierarchy of objects that are included in the assessment report. You can browse the hierarchy, and select objects and categories of objects to view conversion statistics and code.
  • The content of the right pane depends on the item that is selected in the left pane.

    If a group of objects is selected, such as schema, the right pane contains a Conversion statistics pane and Objects by Categories pane. The Conversion Statistics pane shows the conversion statistics for the selected objects. The Objects by Categories pane shows the conversion statistics for the object or categories of objects.

    If a function, procedure, table or view is selected, the right pane contains statistics, source code, and target code.

    • The top area shows the overall statistics for the object. You might have to expand Statistics to view this information.
    • The Source area shows the source code of the object that is selected in the left pane. The highlighted areas show problematic source code.
    • The Target area shows the converted code. Red text shows problematic code and error messages.
  • The bottom pane shows conversion messages, grouped by message number. You can click Errors, Warnings, or Info to view categories of messages, and then expand a group of messages. Click an individual message to select the object in the left pane and display the details in the right pane.

In future blog posts, we’ll work through the specific problems that are in this report. For now, we’ll ignore the problematic objects for the schema and data migration steps. For now, close the report and then uncheck Functions, Procedures and Views nodes to take them out of the conversion. Then uncheck the tables with errors as shown below.

06 Ignore Errors

Go ahead and click on the Create Reports command to verify that there are no errors.

Connect to SQL Server

It’s time to connect SSMA to your SQL Server 2008 R2 Express instance. For the Server name, you’ll need the server name and instance for the target server. Since we are using the WPI installation of SQL Server 2008 R2 Express, you’ll enter in the server name as .\SQLEXPRESS.

You can select an existing database to migrate to using the Database control. You can also type in the name of a new database. In this case, use Sakila as shown below.

07 Connect to SQL Server

Once you click connect, SSMA prompts you if you want to create the database. Choose Yes to create the new database. When connecting to SQL Server Express instances, you’ll receive the following warning indicating that you won’t be able to use the server-side data migration engine. This engine is used for larger migration projects.

08 No SQL Agent

You can Continue from this dialog to start the actual migration process.

Convert Schema

Now that you’ve connected to the target SQL Server instance, SSMA enables the Convert Schema command. Click the Convert Schema command. Once the conversion is finished, you should see the SQL Server Metadata Explorer populated with the tables listed in bold as shown below.

09 Convert schema

Synchronize with Database

To write the tables to the target, select the dbo node in the SQL Server Metadata Explorer and then issue the Tools | Synchronize with Database command. SSMA displays the Synchronize with Database dialog as shown below. In this example, the Tables node was manually expanded to show that no tables are actually on the database at this time.

10 Sync with database

When you click OK, SSMA issues the CREATE TABLE statements to create the objects on the SQL Server target. There are some errors in this example because many of the tables selected have foreign key relationships to some of the tables that we excluded earlier. These errors can be ignored for now.

Migrate Data

The last step is to migrate the data into the tables. To complete the migration, select the Tables node within the MySQL Metadata Explorer for the Sakila database. Then issue the Tools | Migrate Data command or press the command on the toolbar. The Data Migration process requires you to connect to the MySQL database and to the SQL Server database again. SSMA then proceeds with the data migration process and displays the Data Migration Reports as shown below.

11 Migrate Data

Using SQL Server Management Studio

The migrated tables are now ready on the target SQL Server instance. To see the results, launch SQL Server Management Studio (SSMS) and connect using the server name as .\SQLEXPRESS. Expand out the Databases node to see the Sakila database. Expand the out the Sakila database tables and then right click on the actor table and issue the Select Top 1000 Rows command to view the data as shown below.

12 Verifying the results

SQL Server Management Studio that is part of the WPI is a free rich Windows client tool from Microsoft that offers a rich development and management experience like  SQLyog and MONyog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s