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

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

Jun 01, 2016 pm 01:19 PM
linux Host database server project

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to solve nginx403 error How to solve nginx403 error Apr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to clean nginx error log How to clean nginx error log Apr 14, 2025 pm 12:21 PM

The error log is located in /var/log/nginx (Linux) or /usr/local/var/log/nginx (macOS). Use the command line to clean up the steps: 1. Back up the original log; 2. Create an empty file as a new log; 3. Restart the Nginx service. Automatic cleaning can also be used with third-party tools such as logrotate or configured.

See all articles