Jump to content

Merging Separate mySQL Databases - Any Suggestions?


Recommended Posts

hi, i'm working on a php organizer project which includes an address book. it runs on a remote server (at my webhost), coded in php and mySQL.

i'm currently having a little hiccup - a remote server would mean that the organizer would only be accessible if i am connected to the internet. if i'm out on the road with my laptop but without a web connection, the address book data would become inaccessible.

the only workaround i've come up with (its quite stupid, actually) is to have a copy of the system and database running on my laptop, which would make the data accessible on the go. that worked, until I figured that the way i was synchronizing my databases would only allow me to keep my local data updated with remote, but would not allow me to add any data when i am offline, as the sync is one way (remote overwrites local) and whatever additional local information would be lost on overwrite.

is there any way to merge the two databases together?

or does anybody have a better suggestion on how i should program the application so that it can have full functionality both online and offline? ???

Link to comment
Share on other sites

I've never used it, but there is a [url=http://dev.mysql.com/doc/refman/5.0/en/merge-storage-engine.html]merge "engine"[/url] that allows two identical (same columns & index) tables to be used as one.  It seems like there are alot of restrictions on it, but it might be helpful in your situation if you maintain two tables -- one local, one remote -- and use them as one.

Or maybe the [url=http://dev.mysql.com/doc/refman/5.0/en/replace.html]REPLACE[/url] command might be useful.  I think that this will work with new or updated data, but one possible problem is when a row has been updated on both databases.

I haven't used either of these personally, and maybe you've already considered and discarded them, but hopefully they'll help point you in the right direction.
Link to comment
Share on other sites

What I uaually do is export the newer database, clear out the old one and import the newer on in the older one, so now they are both the same. It's kind of a pain...but it works and I haven't had any problems doing that. Unfortunately there is no "sync" button for MySQL.

Link to comment
Share on other sites

thanks a million...

i've tried using the REPLACE command but i couldn't coax php into finding the changed fields (it was willing to find what was different, but not so much which one was updated), and the comparison took really long anyway.

the MERGE function looks good (thanks wildbug!), but even the mySQL ref manual "agrees" its really bug-infested.. don't think i would be working on that anytime soon. guess i have to keep the local version read-only for now.

Link to comment
Share on other sites

I'm working with the REPLACE command right now.

Without using Replication, the best case solution is to restrict updates to one addressbook OR the other but not both.

When updating the addressbook that was "read-only", you could use a process such as:

1) Copy/dump the addressbook table to the target server as, say, sourceAddressBook.
2) execute a query such as :
[code]REPLACE INTO addressBook (ID, firstName, lastName, homeTel )
SELECT ID, firstName, lastName, homeTel
  FROM  sourceAddressBook
    ID = VALUES(ID),
    firstName = VALUES(firstName),
    lastName = VALUES(lastName),
    homeTel  = VALUES(homeTel);[/code]

This assumes a primary (unique) key on ID.
So, if any record changed, it would update that record but if a new record were created, it would be inserted.

If you included a TIMESTAMP column  then you could limit the records copied from the modified database to those inserted/updated after a given TS.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.