MySQL5.5配置Master-Slave复制
操作系统 VMWare 10 CentOS6.5
Master192.168.52.128
Slave192.168.52.129
ps: 主从配置必须确保Master和Slave都运行bin-log
mysql> show variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.00 sec)
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000011 | 348 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.05 sec) |
mysql -u root -proot
grant replicationslave on *.* to ‘slaveone’ @ ‘192.168.52.129’ identified by ‘slaveone’
service mysql stop
( 配置文件my.cnf的获取方法 cp/usr/share/mysql/my-huge.cnf /etc/my.cnf)
vim /etc/my.cnf 在该配置文件的最末尾处插入下面语句
# Master Config server-id = 1 log-bin = mysql-bin binlog-do-db = test binlog-ignore-db = mysql |
service mysql stop
vim /etc/my.cnf 在该配置文件的最末尾处插入下面语句
# Slave Config server-id = 2 replicate-do-db = test replicate-ignore-db = mysql |
service mysqlstart
mysql -uroot -proot
Change master tomaster_host=192.168.52.128,
master_user=’slaveone’,
master_password=’slaveone’,
master_log_file=’mysql-bin.000011’ #这个二进制日志文件名需要查看Master当前binlog状态
master_log_pos=348#这个二进制日志文件位置需要查看Master当前binlog状态
这里可以使用mysqldump工具将Master数据库(表)数据导出为.sql文件,然后在Slave上执行,以达到Master和Slave的同步。关于mysqldump的用法请自行百度或查看mysql官方文档。
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.52.128 Master_User: slaveone Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000011 Read_Master_Log_Pos: 348 Relay_Log_File: localhost-relay-bin.000022 Relay_Log_Pos: 494 Relay_Master_Log_File: mysql-bin.000011 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: 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: 348 Relay_Log_Space: 800 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.06 sec)
ERROR: No query specified |
如果查询结果中Slave_IO_Running:Yes,Slave_SQL_Running: Yes,那么证明已经达到主从同步状态,如果没有,请确保是否开启slave:
mysql > start slave;
如果还没有达成两个参数均为Yes,请进行以下检查:
ü 检查两台服务器是否能够正确连接网络。
ü 检查Slave服务器是否能够ping通Master服务器。
ü 检查Slave服务器上show slavestatus\G 命令中的Last_IO_Error: 和 Last_SQL_Error: 检查出现Error的原因,自行上网搜索进行解决。