简体   繁体   中英

to sync local database and web server database in MySql

I am using PHP and Mysql for developing an application. we have two copies of database, one at local server(ie our end) and one at web server. we want to sync both the database so that if any change made in local database should also reflect on the webserver database. is that possible?? Currently we are using PHP scripts to do so..which is taking too much of time and aswell as not reliable. What can be done so that MySQL will internally fire the whole update and logic??

NOTE:- Our Local server run's on Windows, and web server is Unix based, and we are not using command line to access both the machines, actually at both the sides we use PHP application to update and maintain data(ie to add new or update data)

MySQL Replications might be what you are looking for, but i do not recommend to sync development and production databases. This can get you in trouble when continuing development after webpage has been released. The common approach is to have a server for development (dummy data, not public), testing (real data, not public) and production (real data, public).

edit : I thought the webserver one also was a master.

In this case, look at the way replication in MySQL works; it will simply copy data pushed to the local database to the webserver one.

To Sync Local Database with Web Server Database in MySql you can achieve this functionality via using Replication. Here i have some SQL Commands to perform Replication

1). Here you need two PCs if you are doing it offline for testing purpose and first PC will be your MASTER and other will be act as SLAVE.

2). Check the IP address of your Machines.

3). I am performed it on Localhost with two these following PCs

Machine-1 :- 192.168.1.20

Machine-2 :- 192.168.1.21

and then open you Mysql and Start typing these commands on both machines as given below

---------------- Commands for SERVER-1 ------------------
IP-Address e.g. = 192.168.1.20
---------------------------------------------------------

CREATE USER 'server1'@'%' IDENTIFIED BY '12345';

GRANT REPLICATION SLAVE ON *.* TO 'server1'@'%' IDENTIFIED BY '12345';

FLUSH TABLES WITH READ LOCK;

SHOW MASTER STATUS;

UNLOCK TABLES;


---------------- Commands for SERVER-2 -----------------
IP-Address e.g. = 192.168.1.21
--------------------------------------------------------

STOP SLAVE;

RESET SLAVE;

CHANGE MASTER TO
MASTER_HOST = '192.168.1.20',
MASTER_USER = 'server1',
MASTER_PASSWORD = '12345',
MASTER_PORT = 3306,
MASTER_LOG_FILE = 'Type_your_log_file_name',
MASTER_LOG_POS = TYPE_YOUR_LOG_POSITION_HERE,
MASTER_CONNECT_RETRY = 10,

START SLAVE;

SHOW SLAVE STATUS \G;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM