CentOS下Mysql实现数据库主从同步
一、环境
A服务器: 192.168.10.42 主服务器master CentOS 5 Mysql 5.1.34
B服务器: 192.168.10.68 副服务器slave CentOS 5 Mysql 5.0.56
二、设置Master服务器
1、编辑Master上的/etc/my.cnf文件
在
# Replication Master Server (default)
# binary logging is required for replication
添加如下内容:
log-bin=/var/log/mysql/updatelog
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
2、在Master服务器的Mysql上增加同步帐号
mysql>GRANT PRIVILEGES on *.* [email protected] IDENTIFIED BY '123456'
mysql>FLUSH PRIVILEGES;
3、重新启动mysql
service mysql restart;
4、创建需要同步的数据库sycronDb
三、设置Slave服务器
1、修改/etc/my.cnf文件
server-id = 2
master-host = 192.168.0.1
master-user = back
master-password = back
master-port = 3306
replicate-ignore-db=mysql
replicate-do-db=sycronDb;
2、重新启动mysql
service mysql restart
3、在Slave服务器上不用创建sycronDb,同步程序会自动创建数据库.
四、启动同步
1、在Master服务器上
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000016 | 1033 | sycronDb | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
2、在Slave服务器上
mysql>slave start;
mysql>CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000016',MASTER_LOG_POS=1033;
mysql> show slave status/G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.42
Master_User: back
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000016
Read_Master_Log_Pos: 1033
Relay_Log_File: localhost-relay-bin.000021
Relay_Log_Pos: 243
Relay_Master_Log_File: mysql-bin.000016
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: sycronDb
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1033
Relay_Log_Space: 1474
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
3、测试同步
在Master服务器上执行Insert、Update、Delete、Create Table等操作测试同步,成功!
五、总结
这次仅实现了Master-Slave的单向同步。双向同步,只要把B服务器当Master数据库反向设置一边即可.
结合rsync实现文件同步,可以实现完整的Web站点备份