MySQL主从同步、读写分离配置步骤、问题解决(一)

2014-11-24 14:29:42 · 作者: · 浏览: 2

根据要求配置MySQL主从备份、读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记;

现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用。

为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致;

环境:192.168.0.1 (Master)

192.168.0.2 (Slave)

MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i686) using readline 5.1

1、登录Master服务器,修改my.cnf,添加如下内容;

server-id = 1 //数据库ID号, 为1时表示为Master,其中master_id必须为1到232–1之间的一个正整数值;

log-bin=mysql-bin //启用二进制日志;

binlog-do-db=data //需要同步的二进制数据库名;

binlog-ignore-db=mysql //不同步的二进制数据库名;这个同步后听说很麻烦,我没有同步;

log-bin=/var/log/mysql/updatelog //设定生成的log文件名;

log-slave-updates //把更新的记录写到二进制文件中;

slave-skip-errors //跳过错误,继续执行复制;

2、建立复制所要使用的用户;

mysql>grant replication slave on *.* to test@192.168.0.2 identified by '********'

3、重启mysql;

/usr/bin/mysqladmin -uroot shutdown;

/usr/bin/mysql_safe &

4、现在备份Master上的数据;

锁定后我直接tar.gz data这个库文件;

mysql>FLUSH TABLES WITH READ LOCK;

cd /var/lib/mysql

tar data.tar.gz data

接着直接执行了远程scp;

scp ./data.tar.gz root@192.168.0.2:/var/lib/mysql

5、登录Slave数据库服务器,修改my.cnf;

server-id = 3 //2已经被用在另一个服务器上了,如果以后要再加Slave号接着往后数就OK了;

log-bin=mysql-bin

master-host = 192.168.0.1

master-user = test

master-password = ******

master-port = 3306

master-connect-retry=60 //如果发现主服务器断线,重新连接的时间差;

replicate-ignore-db=mysql //不需要备份的数据库;

replicate-do-db=data //需要备份的数据库

log-slave-update

slave-skip-errors

6、解压刚才从Master scp过来的文件,此处不用改权限、属主,默认没有改变,可以根据实际情况进行修改;

7、上述完成后,可以启动slave了;查看slave状态;

mysql>slave start; www.2cto.com

mysql>show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.0.1

Master_User: test

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: updatelog.000001

Read_Master_Log_Pos: 106

Relay_Log_File: onlinevc-relay-bin.000013

Relay_Log_Pos: 1069

Relay_Master_Log_File: updatelog.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: data

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: 106

Relay_Log_Space: 1681

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)

ERROR:

No query specified

8、查看Master上面的状态;

mysql> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+-------