mysql主从同步 如何同步已有的数据,
现在的问题是,两台主机能实现同步,比如在A服务主机里新建一个表或者数据库,在B从服务器里可同步出来,但是原来没做同步之前,A主机的数据库里的表(数据)等等,不会自动同步到...
现在的问题是,两台主机能实现同步,比如在A服务主机里新建一个表或者数据库,在B从服务器里可同步出来,但是原来没做同步之前,A主机的数据库里的表(数据)等等,不会自动同步到B服务器里,这怎么弄,我主要就是为了把旧的数据同步到B中,而且能随着更新
展开
2个回答
展开全部
--主机开两个窗口,一个进入mysql,一个是shell
--主机阻断写操作
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 1529881 | openser | mysql,test |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
--另外一个窗口导出主机数据库
mysqldump -u root -p123456 --opt -R openser > openser20121203.sql
--刚才的窗口主机解锁
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql>
--打包数据文件到从机
drop database openser;
create database openser;
mysql -u root -p123456 openser < openser20121127.sql
--从机操作
SLAVE STOP;
reset slave;
CHANGE MASTER TO MASTER_HOST='192.168.21.26',
MASTER_USER='repl_user',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=1529881;
start slave;
show slave status\G
--主机阻断写操作
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 1529881 | openser | mysql,test |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
--另外一个窗口导出主机数据库
mysqldump -u root -p123456 --opt -R openser > openser20121203.sql
--刚才的窗口主机解锁
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql>
--打包数据文件到从机
drop database openser;
create database openser;
mysql -u root -p123456 openser < openser20121127.sql
--从机操作
SLAVE STOP;
reset slave;
CHANGE MASTER TO MASTER_HOST='192.168.21.26',
MASTER_USER='repl_user',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=1529881;
start slave;
show slave status\G
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询