SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Scheduled data transfer from SQL Server to MySQL

Scheduled data transfer from SQL Server to MySQL

john barnett
john barnett
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1423 Visits: 750
I work in an environment where from time to time I have to export data from a SQL Server database to a MySQL database. Currently this is a fiddly manual process (done with linked tables both ways in Access, with a local temp table).
Needless to say I have been looking into automating this, and in doing so have found major shortfalls in the ability of SSIS to export data from SQL Server to a MySQL database (going the other way is no problem at all).

The most reliable way I have found so far is via a Powershell script. The script below uses the AdventureWorks 2008 database as its source and expects the target tables to be present.

# Function to transfer data from MS SQL SErver to MySQL
# requires MySQL Connector for .Net 6.2.3 and Powershell 1.0 runtime installed
# no ODBC data source required
# MySql Connector for .Net homepage: http://dev.mysql.com/downloads/connector/net/
# J Barnett, Sep-Oct 2010.

function CopyDataFromMSSqlToMySql {

##$file = "C:\\Program Files\\MySQL\\MySQL Connector Net 6.2.3\\Assemblies\\MySql.Data.dll\"

# define connection strings
$mssql_connectionString = "Server=(local);Integrated Security=SSPI;Database=Adventureworks"
$mysql_connectionString = "server=localhost;uid=xxxxxx;pwd=yyyyyy;database=mydb;"

# Create MS SqlConnection object and define connection string
$mssql_con = New-Object System.Data.SqlClient.SqlConnection
$mssql_con.ConnectionString = $mssql_connectionString

# Create SqlCommand object, define command text, and set the connection
$mssql_cmd = New-Object System.Data.SqlClient.SqlCommand
$mssql_cmd.CommandText = "select EmployeeID, replace (LoginID, '\', '\\') as loginid, cast(CAST(YEAR(birthdate) AS VARCHAR(4)) + '-' +
CAST(MONTH(birthdate) AS VARCHAR(2)) + '-' + CAST(DAY(birthdate) AS VARCHAR(2)) as varchar (10)) BirthDate from HumanResources.employee;"
$mssql_cmd.Connection = $mssql_con

# Create SqlDataReader
$dr = $mssql_cmd.ExecuteReader()

If ($dr.HasRows)

# connect to MySQL

$mysql_connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$mysql_connection.ConnectionString = $mysql_connectionString
$mysql_cmd = New-Object MySql.Data.MySqlClient.MySqlCommand

Write-Host "Number of fields: " $dr.FieldCount

While ($dr.Read())
$employeeid = $dr["employeeid"]
$loginid = $dr["loginid"]
$birthdate = $dr["birthdate"]

##$mysql_command = "insert into employees (employeeid, loginid, birthdate) values ('$employeeid', '$loginid', '$birthdate' )"
$mysql_command = "replace into employees (employeeid, loginid, birthdate) values ('$employeeid', '$loginid', '$birthdate')"
Write-Host $mysql_command # don't run it yet

$mysql_cmd.connection = $mysql_connection
$mysql_cmd.CommandText = $mysql_command
$mysql_da = New-Object MySql.Data.MySqlClient.MySqlDataAdapter ($mysql_cmd)
$dataSet = New-Object System.Data.DataSet
$mysql_da.Fill($dataSet, "test1")


# Close MySQL connection
Write-Host "No matching source data"


# Close the data reader and the connection

NB for those of you who don't usually deal with MySQL this contains a few nuggets you may not be familiar with that I have had to deal with:
1. Dates are retrieved by Powershell as dd/mm/yyyy whereas they need to be in ANSI format to be inserted into the MySQL database without errors.
2. "replace into" is MySQL specific syntax. It basically combines an insert into (to add rows that don't exist) and an update on the primary key value where it differs.
3. The single \ character is doubled up as it is treated as a delimiter otherwise.

I would be the first to admit I am not an SSIS guru (which would be my ideal way), but does anybody have any ideas on how to improve on this or ideas on getting a functional equivalent working via integration services (or a better way altogether)? Our MySQL Servers are linux based and they don't have drivers installed for connecting to MSSQL servers (and I can't seem to persuade the administrators of those server to install them either).



You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum