I have two completely different MySQL databases and they both have their own user table schemes. I want to synchronize user tables (in real time) so, when a user is added to either database, the other o开发者_运维知识库ne should be updated accordingly. My question is, is it possible to do that kind of synchronization? If yes, what are/is the effective ways/way to do this?
If you want this to happen in the database whenever a row is inserted/updated, then it sounds like a candidate for a TRIGGER. This will allow you to write code so that whenever a change happens to table A, you can automatically make the 'mirror' change to table B. Be careful, since you're going to have triggers going in both directions, that you identify a way of telling that an insert/update is coming from the trigger of the other table, and you don't get caught in an infinite loop.
Yes, it's possible, but since your schema are different, the best way is just to do it manually; in the code that updates one of the databases, simply have it appropriately update the other database. This is pretty much necessary because your schemas are different; you're effectively implementing a manual mapper which is invoked at update time.
There ARE other ways to do it, but this is the simplest to put in place, and is very effective.
Edit: Okay, other ways to do this: Have a regular job (cronjob or similar) which queries one table based upon update since last query, and propagates those updates to the second table; this method suffers from potential, lag, however. Alternatively, you could do something based upon triggers for each user table, but I'd recommend avoiding this approach, since it introduces some potentially serious execution time increases, depending on how the triggers are implemented. But I'd still say the far simplest way is to modify your user table update code to modify both tables instead of just one.
精彩评论