mysql集群搭建配置


1. 确保主从服务是版本一致的。

2. 主服务器创建一个MySQL帐号为同步专用,并且授予replicationslave权限。

Mysql > grant replication slave on *.* to 'repl'@'192.168.0.184' identified by '1234+asdf';

Mysql > FLUSH PRIVILEGES ;

3. 主服务器开启binlog,并且设置server-id = 1

 [mysqld]                        

log-bin = mysql-bin

server-id = 1

4. 从服务器配置文件,增加主服务器的配置

[mysqld]             

log-bin = mysql-bin

server-id = 2

master-host = 192.168.0.209

master-user = repl

master-password = 1234+asdf

master-port = 3306

master-connect-retry = 60 #60s repl once

slave_skip_errors = 1062 #1062 ingore dup key #all ignore all error

replicate-do-db = TQ  #

#replicate-ignore-db = mysql #

#replicate-do-table = otherdb

5. 获得主服务器的日志名和偏移量。 动态增加从服务器

master status;

mysql> Show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysqld-bin.000014 | 655898544 | | |

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

1 row in set (0.00 sec)

6. 热备master上的数据到从服务器。

7. 启动从服务器,停止复制stop slave

8. 修改复制的文件和偏移量

CHANGE MASTER TO MASTER_LOG_FILE='mysqld-bin.000014',MASTER_LOG_POS=655898544;


9. 从服务器上启动slave线程start slave





查看主从服务器运行状态


主服务器:

1. show slave status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysqld-bin.000014 | 655898544 | | |

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

当前二进制日志记录了偏移量655898544的位置,该值减去这一时间点时从服务器上的Read_Master_Log_Pos,就可以得知IO线程的延时。

 

2. Show processlist;
查看到有一个commad是Binlog Dump的线程,查看运行状态。 如: Has sent all binlog to slave; waiting for binlog to be updated


 

从服务器:

1. show slave status;

主要观察Slave_IO_Running 和Slave_SQL_Running这两个状态是否为yes。

Slave_IO_Running:这个进程负责从服务器从主服务器上读取binlog日志,并且写入中继日志。

Slave_SQL_Running:这个进程负责读取中继日志,并且根据日志执行响应sql同步数据。

如果有一个为no,则复制出错。 错误原因从last_error中看到,或者查看错误日志。

 

2. Show processlist;

查看到有一个两个user是system user的线程,一个是Slave_IO_Running ,另外一个是Slave_SQL_Running。

其中Slave_SQL_Running线程中的time值,它记录了从服务器当前执行的sql时间戳和系统时间之间的差距。




主从同步维护


如果主服务器繁忙或者其他原因导致主从数据严重不一致,进而影响使用的情况下。 可以采用主从同步维护,一般是在负载较低的时候暂时阻塞主数据库的更新,强制主从数据库更新同步。

步骤:

1. 主服务器:flush table with read lock;(会阻塞数据库的所有更新操作)

2. 主服务器:Show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysqld-bin.000014 | 10000 | | |

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

3. 从服务器:select master_pos_wait('mysqld-bin.000014','10000');(这个语句会阻塞从服务器,直到达到指定的这个文件和偏移量,成功返回0,超时返回-1)

4. 主服务器:unlock tables;

 



切换主从服务器


1. 确保从服务器已经执行了relay log中的全部更新。 查看命令Show processlist的状态是Waiting for master to send event

2. 从服务器执行 STOP SLAVE IO_THREAD。 直到Show processlist查看到状态是Has read all relay log。

3. 从服务器执行STOP SLAVE。

4. 从服务器执行RESET MASTER。

5. 切换dns指向。 正常运行。

6. 主服务器修复正常,配置成从服务器(参考配置部分)或者重新搭建为主服务器,注意数据完整。