Read The Fantastic Manual (a.k.a., RTFM):
No way you should consider trying to do something like this using your own coding. Let MySQL do it for you.
And note, please, that the replication types described there are the *simple* types supported by MySQL. MySQL also supports clustered replication, but you wouldn't likely use that if you only have two servers.