Jump to content

Archived

This topic is now archived and is closed to further replies.

thomas

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? ???

Share this post


Link to post
Share on other sites
Synchronization is always an issue if you need offline mode as well; "merging" can be done, but it's tedious... not sure you have much a choice, though.

Share this post


Link to post
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.

Share this post


Link to post
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.

-Chris

Share this post


Link to post
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.

:)

Share this post


Link to post
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
ON DUPLICATE KEY UPDATE
    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.

Brian

Share this post


Link to post
Share on other sites
Not a bad idea... if both are read/write, then even timestamps don't help much.

Share this post


Link to post
Share on other sites

×

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.