I'm trying to synchronize MySQL tables with the tables from SQL Server 2008. What I want is on way synchronization from SQL server to MySQL. I already transfered tables from SQL Server to MySQL all I need now is the PHP code that will update the table (check for changes, insert new lines, delete non-existing ones) automatically whenever run. Can anybody help me?
What may be of use to use is an ETL tool. One designed for SQL Server is SSIS. It may be possible to use this to help bring in the data that you want, but I've never used it for MySQL before. According to this post [ http://blogs.msdn.com/b/mattm/archive/2008/03/03/connecting-to-mysql-from-ssis.aspx ] it is actually possible.
Another post that may help you out: http://www.packtpub.com/article/mysql-data-transfer-using-sql-server-integration-services-ssis
Unfortuantly the last article describes it from MySQL to SQL Server, which is not the direction you want. It may be possible to get it working the other way, but you may need to find an ETL tool for MySQL. I'm not aware of any off the top of my head, though a quick Google search revealed http://www.benetl.net/ , but I can't vouch for it.
I think that PHP has nothing to do here.
You may use various decisions such as:
While I am not totally convinced, that this is a good idea, I understand this might be necessary in some use cases (see last paragraph), as we had a similar requirement short time ago.
This worked quite well:
On the sending side (no matter what it is, in your case SQL server)
On the receiving side (no matter what it is, in your case MySQL)
This scheme works quite well, even between 2 hosts that can "talk" only HTTP between them - it is also quite robust, as a lost connection (or a receiving side reboot) will just buffer up the changes in the sending side changelog table, cleaning it out when connection is reestablished.
I would recommend that you have a timestamp column in every table and every time you insert or update, update the timestamp value of each affected row. Then, you iterate over all tables checking if the timestamp is newer than the one you have in the destination database. If it´s newer, then check if you have to insert or update. Observation 1: be aware of physical deletes since the rows are deleted from source db and you have to do the same at the server db. You can solve this avoiding physical deletes or logging every deletes in a table with timestamps. Something like this: DeletedRows = (id, table_name, pk_column, pk_column_value, timestamp) So, you have to read all the new rows of DeletedRows table and execute a delete at the server using table_name, pk_column and pk_column_value. Observation 2: be aware of FK since inserting data in a table that´s related to another table could fail. You should deactivate every FK before data synchronization.