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
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
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>
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主机上的日志文件。
在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存储引擎的数据库上有效。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools