MySQL 数据库 主从双向数据同步
测试环境:
库A:
系统:Ubuntu 8.04.3 内核2.6.24-24
MySQL版本:5.0.51a-3ubuntu5.4-log (Ubuntu)
IP:192.168.1.202
数据库B:
系统:Windows XP sp3
MySQL版本:5.0.77-community-nt-log
IP:192.168.1.39
A配置
1.修改 my.cnf文件 添加内容
[mysqld]
log-bin
server-id =1
master-host=192.168.1.39
master-user=backup
master-password='123456'
master-port=3306
master-connect-retry=10
****************************
2.创建一个同步专用的帐号。
GRANT REPLICATION SLAVE,RELOAD,SUPER,FILE ON *.* TO 'backup'@ '192.168.39'
IDENTIFIED BY '123456' ;
FLUSH PRIVILEGES ;
重启数据库。
B配置
1.修改 my.ini
log-bin
server-id = 2
master-host = 192.168.1.202
master-user =backup
master-password ='123456'
master-port = 3306
master-connect-retry=10
****************************
2.创建一个同步专用的帐号。
GRANT REPLICATION SLAVE,RELOAD,SUPER,FILE ON *.* TO 'backup'@ '192.168.202'
IDENTIFIED BY '123456' ;
FLUSH PRIVILEGES ;
重启数据库;
-------------------------------------------
分别使用
show master status;
show slave status;
查看进程的运行情况
show slave status;
大致内容如下
---+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master |
+----------------------+----------------+-------------+-------------+---------------+------------------+---------------------+--
| Connecting to master | 192.168.128.39 | backup | 3306 | 60 | mysql-bin.000008 | 106 | www-relay-bin.000002 | 98 | mysql-bin.000008 | Yes |Yes | | | | | | | 0 | | 0 | 106 | 98 | None | | 0 | No | | | | | | NULL |
+----------------------+----------------+-------------+-------------+---------------+------------------+--------------------
根据show master status; 显示的信息 对slave status进行修改。
A show master status;
File Position Binlog_Do_DB Binlog_Ignore_DB
www-bin.000002 511
进入B 修改
mysql> stop slave
mysql> change master to master_host='192.168.1.202', master_user='backup', master_password='123456', master_log_file='www-bin.000002', master_log_pos=511;
B show master status;
File Position Binlog_Do_DB Binlog_Ignore_DB
MICROSOF-9419B5-bin.000001 98
进入A 修改
change master to master_host='192.168.1.39', master_user='backup', master_password='123456', master_log_file=MICROSOF-9419B5-bin.000001', master_log_pos=98;
提示:如果修改了主服务器配置,记得删除从服务器上的master.info文件。否则从服务器使用的还是老配置,可能会导致错误。
有时候数据库同步自己会停下来,原因是发生了错误。那么用下面的语句可以避免这个问题。出现错误后忽略,如果不加这个,出现任何错误,同步进程会终止
slave-skip-errors=all
在修改的时候 202的数据库 突然久无法启动了
日志里无记录
错误信息如下
root@www:/var/lib/mysql# /usr/bin/mysqld_safe --user=mysql &
[1] 15666
root@www:/var/lib/mysql# nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[15688]: started
root@www:/var/lib/mysql# STOPPING server from pid file /var/lib/mysql/www.pid
mysqld_safe[15702]: ended
[1]+ Done /usr/bin/mysqld_safe --user=mysql
最后在/var/lib/mysql目录下发现有两个文件的所有者是root
root@www:/var/lib# chown -R mysql:mysql /var/lib/mysql
搞定 启动正常
-------------------------------
测试同步的时候 出现了点问题,我在A创建了一个数据库 B立马同步也有了那个库
但是后来 删除了 B缺没删除那个库, 现在的问题一直是
Error 'Unknown database 'test123'' on query. Default database: 'test123'. Query: 'create table `test123`.`test1`(`test1` binary )'
网上资料是说 把二进制日志删了就好, 可问题貌似没解决。。
- 最新评论