Home Database Mysql Tutorial Mysql热备份-主从形式

Mysql热备份-主从形式

Jun 07, 2016 pm 04:25 PM
mysql backup

Mysql热备份---主从模式 注:转载请注明出处。 在上家公司四台服务器使用mysql的热备份机制,一台宕机其它还可以工作。防止以往总结如下: 1、本博文试验环境xp系统。利用热备份机制功能可以实现两个数据库同步,主从模式(A-B),互相备份模式(A=B)的功能。 my

Mysql热备份---主从模式
注:转载请注明出处。
  
    在上家公司四台服务器使用mysql的热备份机制,一台宕机其它还可以工作。防止以往总结如下:
1、本博文试验环境xp系统。利用热备份机制功能可以实现两个数据库同步,主从模式(A->B),互相备份模式(AB)的功能。
mysql 数据库同步复制功能的设置都在mysql的配置文件中体现
2、下面我们来看看如何配置数据同步(A->B):
假设数据库A为主机(将向B提供同步服务,即B中的数据来自A):
A机器:
IP = 192.168.1.101
B机器:
IP = 192.168.1.102
(1).在A机器中有数据库如下:
//数据库A
CREATE DATABASE backup_db;
USE backup_db;
CREATE TABLE `backup_table` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) character set utf8 NOT NULL,
`sex` varchar(2) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#A机器的my.cnf(或my.ini)中应该配置:
server-id=1
log-bin=c:\mysqlback #同步事件的日志记录文件
binlog-do-db=backup_db #提供数据同步服务的数据库
(2).在B机器中有数据库如下:
//数据库B
CREATE DATABASE backup_db;
USE backup_db;
CREATE TABLE `backup_table` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) character set utf8 NOT NULL,
`sex` varchar(2) character set utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
注:数据库A和B的数据库结构一定要相同,否则无法构成同步。
#B机器的my.cnf(或my.ini)中应该配置:
server-id=2
master-host=192.168.1.101 #主机A的地址
master-user=ym #主机A提供给B的用户,该用户中需要包括数据库backup_db的权限
master-password=ym #访问密码
master-port=3306 #端口,主机的MYSQL端口
master-connect-retry=60 #重试间隔60秒
replicate-do-db=backup_db #同步的数据库
(3).完成了以上配置之后,将A的mysql数据的权限给B。
A机器:
mysql>GRANT FILE ON *.* TO IDENTIFIEDBY ‘ym’;
(4).重启AB数据库,后:
B机器:
mysql>slave start;
查看同步配置情况
A机器:
mysql>show master status;
B机器:
mysql>show slave status;
(5).在A中的backup_db.backup_table表中插入一些数据,查看B中的backup_db.backup_table表是否同步了数据改动。如果没有看到同步数据结果,即同步不成功,请查看错误(如下)。
当有错误产生时*.err日志文件(可到mysql安装目录下找),同步的线程退出。当纠正错误后重复步骤(4)。
3、实现双向热备(AB):
将以上的(1)-(5)步骤按A-B双向配置即可。



1、异步复制基本原理

从MySQL3.23.15以后,MySQL支持单向的异步复制。也就是说,1台MySQL服务器充当Master(主库),1台或多台 MySQL服务器充当Slaves(从库),数据从Master向Slaves进行异步复制。注意,这种复制是异步的,有别于MySQL的同步复制实现 (这种实现称做MySQL集群,MySQL Cluster)。

当主库有更新的时候,主库会把更新操作的SQL写入二进制日志(Bin log),并维护一个二进制日志文件的索引,以便于日志文件轮回(Rotate)。在从库启动异步复制的时候,从库会开启两个I/O线程,其中一个线程连 接主库,要求主库把二进制日志的变化部分传给从库,并把传回的日志写入本地磁盘。另一个线程则负责读取本地写入的二进制日志,并在本地执行,以反映出这种 变化。较老的版本在复制的时候只启用一个I/O线程,实现这两部分的功能。

有几个关键性的地方需要注意:

- 主库必须启用Bin log,主库和从库必须有唯一的Server Id
- 从库必须清楚了解从主库的哪一个Bin log文件的哪一个偏移位置起开始复制
- 从库可以从主库只复制指定的数据库,或者数据库的某些数据表
- 主库和从库的数据库名称可以不一样,不过还是推荐使用一样的名称
- 主库和从库的MySQL版本需保持一致



MySQL数据库数据同步的方法
设置好新的master服务器IP.
首先,将slave这边的数据库表内容 phpmyadmin 或者直接 mysql命令进行truncate清空{删除那些bin-log文件,如果innodb则直接删掉数据库文件,然后重新创建表},之后,让 SLAVE START 或者重起MySQL服务,以便先同步一次,这样等会儿才不会出现 "Duplicate entry 重复记录" 这样的错误,
同步完成后,从master那边导出完整的.sql文件(InnoDB使用sql导出,MyISAM应当直接打包目录就可以了),将导出来的.sql导入slave这边应当就OK了
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles