Home > Database > Mysql Tutorial > MySQL 数据库两台主机同步实战(linux)_MySQL

MySQL 数据库两台主机同步实战(linux)_MySQL

WBOY
Release: 2016-06-01 13:19:14
Original
1042 people have browsed it

bitsCN.com

当一个从服务器连接到主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知下一次更新。

在实际项目中,两台分布于异地的主机上安装有MySQL数据库,两台服务器互为主备,客户要求当其中一台机器出现故障时,另外一台能够接管服务器上的应用,这就需要两台数据库的数据要实时保持一致,在这里使用MySQL的同步功能实现双机的同步复制。

以下是操作实例:

1、数据库同步设置

主机操作系统:RedHat Enterprise Linux 5

数据库版本:MySQL Ver 14.12 Distrib 5.0.22

前提:MySQL数据库正常启动

假设两台主机地址分别为:

ServA:10.240.136.9

ServB:10.240.136.149

1.1 配置同步账号

在ServA上增加一个ServB可以登录的帐号:

MySQL>GRANT all privileges ON *.* TO tongbu@'10.240.136.149' IDENTIFIED BY '123456';
Copy after login

在ServB上增加一个ServA可以登录的帐号:

MySQL>GRANT all privileges ON *.* TO tongbu@'10.240.136.9' IDENTIFIED BY '123456';
Copy after login

1.2 配置数据库参数

1、以root用户登录ServA,修改ServA的my.cnf文件

vi /etc/my.cnf
Copy after login
Copy after login

在[MySQLd]的配置项中增加如下配置:

1 default-character-set=utf8<br>2 <br>3 log-bin=MySQL-bin<br>4 <br>5 relay-log=relay-bin<br>6 <br>7 relay-log-index=relay-bin-index<br>8 <br>9 server-id=1<br>10 <br>11 master-host=10.240.136.149<br>12 <br>13 master-user=tongbu<br>14 <br>15 master-password=123456<br>16 <br>17 master-port=3306<br>18 <br>19 master-connect-retry=30<br>20 <br>21 binlog-do-db=umsdb<br>22 <br>23 replicate-do-db=umsdb<br>24 <br>25 replicate-ignore-table=umsdb.boco_tb_menu<br>26 <br>27 replicate-ignore-table=umsdb.boco_tb_connect_log<br>28 <br>29 replicate-ignore-table=umsdb.boco_tb_data_stat<br>30 <br>31 replicate-ignore-table=umsdb.boco_tb_log_record<br>32 <br>33 replicate-ignore-table=umsdb.boco_tb_workorder_record
Copy after login

2、以root用户登录ServB,修改ServB的my.cnf文件

vi /etc/my.cnf
Copy after login
Copy after login

在[MySQLd]的配置项中增加如下配置:

1 default-character-set=utf8<br>2 <br>3 log-bin=MySQL-bin<br>4 <br>5 relay-log=relay-bin<br>6 <br>7 relay-log-index=relay-bin-index<br>8 <br>9 server-id=2<br>10 <br>11 master-host=10.240.136.9<br>12 <br>13 master-user=tongbu<br>14 <br>15 master-password=123456<br>16 <br>17 master-port=3306<br>18 <br>19 master-connect-retry=30<br>20 <br>21 binlog-do-db=umsdb<br>22 <br>23 replicate-do-db=umsdb<br>24 <br>25 replicate-ignore-table=umsdb.boco_tb_menu<br>26 <br>27 replicate-ignore-table=umsdb.boco_tb_connect_log<br>28 <br>29 replicate-ignore-table=umsdb.boco_tb_data_stat<br>30 <br>31 replicate-ignore-table=umsdb.boco_tb_log_record<br>32 <br>33 replicate-ignore-table=umsdb.boco_tb_workorder_record
Copy after login

1.3 手工执行数据库同步

假设以ServA为主服务器,在ServB上重启MySQL:

service MySQLd restart
Copy after login
Copy after login

在ServB上用root用户登录MySQL,执行:

MySQL> stop slave;<p> </p><p>MySQL> load data from master;</p><p>MySQL> start slave;</p>
Copy after login

在ServA上重启MySQL:

service MySQLd restart
Copy after login
Copy after login

1.4 查看数据库同步状态

在MySQL命令提示符下执行:

MySQL> show slave status/G
Copy after login
Copy after login

将显示同步进程的状态,如下所示,两行蓝色字体为slave进程状态,如果都为yes表示正常;红色字体表示同步错误指示,如果有问题会有错误提示:

1 *************************** 1. row ***************************<br>2 <br>3 Slave_IO_State: Waiting for master to send event<br>4 <br>5 Master_Host: 10.21.2.90<br>6 <br>7 Master_User: tongbu<br>8 <br>9 Master_Port: 3306<br>10 <br>11 Connect_Retry: 30<br>12 <br>13 Master_Log_File: localhost-bin.000005<br>14 <br>15 Read_Master_Log_Pos: 39753882<br>16 <br>17 Relay_Log_File: localhost-relay-bin.000062<br>18 <br>19 Relay_Log_Pos: 9826663<br>20 <br>21 Relay_Master_Log_File: localhost-bin.000005<br>22 <br>23 Slave_IO_Running: Yes<br>24 <br>25 Slave_SQL_Running: Yes<br>26 <br>27 Replicate_Do_DB: bak,umsdb<br>28 <br>29 Replicate_Ignore_DB:<br>30 <br>31 Replicate_Do_Table:<br>32 <br>33 Replicate_Ignore_Table: umsdb.boco_tb_connect_log,umsdb.boco_tb_menu,umsdb.boco_tb_workorder_record,<p> </p><p>umsdb.boco_tb_data_stat,umsdb.boco_tb_log_record<br>34 <br>35 Replicate_Wild_Do_Table:<br>36 <br>37 Replicate_Wild_Ignore_Table:<br>38 <br>39 Last_Errno: 0<br>40 <br>41 Last_Error:<br>42 <br>43 Skip_Counter: 0<br>44 <br>45 Exec_Master_Log_Pos: 39753882<br>46 <br>47 Relay_Log_Space: 9826663<br>48 <br>49 Until_Condition: None<br>50 <br>51 Until_Log_File:<br>52 <br>53 Until_Log_Pos: 0<br>54 <br>55 Master_SSL_Allowed: No<br>56 <br>57 Master_SSL_CA_File:<br>58 <br>59 Master_SSL_CA_Path:<br>60 <br>61 Master_SSL_Cert:<br>62 <br>63 Master_SSL_Cipher:<br>64 <br>65 Master_SSL_Key:<br>66 <br>67 Seconds_Behind_Master:</p>
Copy after login

3、数据库同步测试

配置完数据库后进行测试,首先在网络正常情况下测试,在ServA上进行数据库操作,和在ServB上进行数据库操作,数据都能够同步过去。

拔掉ServB主机上的网线,然后在ServA上做一些数据库操作,之后再恢复ServB的网络环境,但是在ServB上却看不到同步的数据,通过命令show slave status/G查看发现Slave_IO_Running的状态是No,这种状态持续很长一段时间,数据才能同步到ServB上去。这是什么问题呢?同步延迟不会这么大吧。后来通过网上查找相关资料,找到一个同步延迟相关的参数:

--slave-net-timeout=seconds
Copy after login

参数含义:当slave从主数据库读取log数据失败后,等待多久重新建立连接并获取数据。

于是在配置文件中增加该参数,设置为60秒

slave-net-timeout=60
Copy after login

重启MySQL数据库后测试,该问题解决。

4、 数据库同步失效的解决

当数据同步进程失效后,首先手工检查slave主机当前备份的数据库日志文件在master主机上是否存在,在slave主机上运行:

MySQL> show slave status/G
Copy after login
Copy after login

一般获得如下的信息:

1 *************************** 1. row ***************************<br>2 <br>3 Slave_IO_State: Waiting for master to send event<br>4 <br>5 Master_Host: 10.21.3.240<br>6 <br>7 Master_User: tongbu<br>8 <br>9 Master_Port: 3306<br>10 <br>11 Connect_Retry: 30<br>12 <br>13 Master_Log_File: MySQL-bin.000001<br>14 <br>15 Read_Master_Log_Pos: 360<br>16 <br>17 Relay_Log_File: localhost-relay-bin.000003<br>18 <br>19 Relay_Log_Pos: 497<br>20 <br>21 Relay_Master_Log_File: MySQL-bin.000001<br>22 <br>23 Slave_IO_Running: Yes<br>24 <br>25 Slave_SQL_Running: Yes<br>26 <br>27 Replicate_Do_DB: bak<br>28 <br>29 Replicate_Ignore_DB:<br>30 <br>31 Replicate_Do_Table:<br>32 <br>33 Replicate_Ignore_Table:<br>34 <br>35 Replicate_Wild_Do_Table:<br>36 <br>37 Replicate_Wild_Ignore_Table:<br>38 <br>39 Last_Errno: 0<br>40 <br>41 Last_Error:<br>42 <br>43 Skip_Counter: 0<br>44 <br>45 Exec_Master_Log_Pos: 360<br>46 <br>47 Relay_Log_Space: 497<br>48 <br>49 Until_Condition: None<br>50 <br>51 Until_Log_File:<br>52 <br>53 Until_Log_Pos: 0<br>54 <br>55 Master_SSL_Allowed: No<br>56 <br>57 Master_SSL_CA_File:<br>58 <br>59 Master_SSL_CA_Path:<br>60 <br>61 Master_SSL_Cert:<br>62 <br>63 Master_SSL_Cipher:<br>64 <br>65 Master_SSL_Key:<br>66 <br>67 Seconds_Behind_Master: 0<br>其中Master_Log_File描述的是master主机上的日志文件。
Copy after login

在master上检查当前的数据库列表:

MySQL> show master logs;
Copy after login

得到的日志列表如下:

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

| Log_name | File_size |

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

| localhost-bin.000001 | 495 |

| localhost-bin.000002 | 3394 |

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

如果slave主机上使用的的Master_Log_File对应的文件在master的日志列表中存在,在slave主机上开启从属服务器线程后可以自动同步:

MySQL> start slave;
Copy after login

如果master主机上的日志文件已经不存在,则需要首先从master主机上恢复全部数据,再开启同步机制。

在slave主机上运行:

MySQL> stop slave;
Copy after login
Copy after login

在master主机上运行:

MySQL> stop slave;
Copy after login
Copy after login

在slave主机上运行:

MySQL> load data from master;

MySQL> reset master;

MySQL> start slave;

Copy after login

在master主机上运行:

MySQL> reset slave;<p> </p><p>MySQL>start slave;</p>
Copy after login

注意:LOAD DATA FROM MASTER目前只在所有表使用MyISAM存储引擎的数据库上有效。

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template