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

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

Jun 07, 2016 pm 06:00 PM
mysql database

MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护日志文件的一个索引以跟踪日志循环。

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

在实际项目中,两台分布于异地的主机上安装有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可以登录的帐号:

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

1.2 配置数据库参数

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

在[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文件

在[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:

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

在ServA上重启MySQL:

1.4 查看数据库同步状态

在MySQL命令提示符下执行:

将显示同步进程的状态,如下所示,两行蓝色字体为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从主数据库读取log数据失败后,等待多久重新建立连接并获取数据。

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

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

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

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

一般获得如下的信息:

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上检查当前的数据库列表:

得到的日志列表如下:

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

| Log_name | File_size |

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

| localhost-bin.000001 | 495 |

| localhost-bin.000002 | 3394 |

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

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

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

在slave主机上运行:

在master主机上运行:

在slave主机上运行:

在master主机上运行:

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

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

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles