Home Database Mysql Tutorial MySQL的主从复制Replication之MyIsam和InnoDB数据复制发布

MySQL的主从复制Replication之MyIsam和InnoDB数据复制发布

Jun 07, 2016 pm 04:57 PM
mysql

MyIsam发布相对简单,把库下面所有文件拷过去就好了。复制Innodb的做法和MyIsam类似,只不过Innodb引擎下的表的数据内容不再是存

MyIsam发布相对简单,把库下面所有文件拷过去就好了。复制Innodb的做法和MyIsam类似,只不过Innodb引擎下的表的数据内容不再是存在同一的数据库目录下(MyIsam就是这么做的),Innodb引擎下,数据文件存放在ibdata1这样的文件里,而操作的log默认存放在ib_logfile0、ib_logfile1、ib_logfile2这样的文件中。
Innodb的复制发布是以下的步骤(MyIsam的更简单一点,去掉其中特殊强调的Innodb部分,就是MyIsam的复制发布了):
1、主库上,执行FLUSH TABLES WITH READ LOCK语句清空所有表和块写入语句:(当然也可以把数据库stop掉)
mysql> FLUSH TABLES WITH READ LOCK;

2、得到主库当前记录点
mysql > SHOW MASTER STATUS\G;
*************************** 1. row ***************************
            File: mysql-netprws_test-bin.000010
        Position: 54736
    Binlog_Do_DB: netprws
Binlog_Ignore_DB:
1 row in set (0.00 sec)


3、将主服务器的数据文件拷贝到从服务器上
如果有innodb引擎,须将innodb的数据文件拷贝过来,文件名可能是ibdata1

4、主库:确认文件已经生成拷贝了。
mysql> UNLOCK TABLES;


从服务器:
1.    停止用于从服务器的服务器并在其my.cnf文件中添加下面的行:
[mysqld]     
server-id=slave_id
slave_id区别Master_id值,必须为2到2^32–1之间的一个正整数值。

2.启动从服务器

3.在从服务器上设置主服务器信息
mysql> CHANGE MASTER TO
->     MASTER_HOST='master_host_name',
->      MASTER_PORT=3306,
->     MASTER_USER='replication_user_name',
->     MASTER_PASSWORD='replication_password',
->     MASTER_LOG_FILE='recorded_log_file_name',
->     MASTER_LOG_POS=recorded_log_position;

4.启动复制
mysql> START SLAVE;

5.查看复制状态
mysql> SHOW SLAVE STATUS;
mysql> SHOW PROCESSLIST;

在Innodb的情况下:
Last_Errno: 1033
此处出错是innodb的复制发布的问题,需要删除从库上原来的innodb的log文件,通常文件名为:ib_logfile0,ib_logfile1...

再重新启动从库的slave。

mysql> show slave status\G;
          ……

         Master_Log_File: mysql-netprws_test-bin.000010
        Read_Master_Log_Pos: 55786
         Relay_Log_File: localhost-relay-bin.000018
        Relay_Log_Pos: 55936
         Relay_Master_Log_File: mysql-netprws_test-bin.000010
         Slave_IO_Running: Yes
         Slave_SQL_Running: Yes

         ……

二者有为NO的,一般都是由于Master_Log_File或者Read_Master_Log_Pos的设置问题,还有就是网络问题了。通常到这里应该就OK了,如果还有问题,需要根据show slave status\G中看到的错误代码和错误提示再进行定位了。

另外从库的my.cnf中有一个设置建议用上。
read_only

这个属性打开很有必要,可以防止很多人为修改从库导致从库同步失败的问题。

采用 binlog 的方法相对来说更灵活,省心省力,而且还可以支持增量备份。

  启用 binlog 时必须要重启 mysqld。首先,关闭 mysqld,打开 my.cnf,加入以下几行:

  然后启动 mysqld 就可以了。运行过程中会产生 binlog.000001 以及 binlog.index,,前面的文件是 mysqld 记录所有对数据的更新操作,后面的文件则是所有 binlog 的索引,都不能轻易删除。关于 binlog 的信息请查看手册。

  需要备份时,可以先执行一下 SQL 语句,让 mysqld 终止对当前 binlog的写入,就可以把文件直接备份,这样的话就能达到增量备份的目的了: FLUSH LOGS;如果是备份复制系统中的从服务器,还应该备份master.info 和 relay-log.info 文件。备份出来的 binlog 文件可以用 MySQL 提供的工具 mysqlbinlog 来查看,如:/usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001该工具允许你显示指定的数据库下的所有SQL 语句,并且还可以限定时间范围,相当的方便,详细的请查看手册。

  恢复时,可以采用类似以下语句来做到: /usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001  | mysql -uyejr -pyejr db_name把 mysqlbinlog 输出的 SQL 语句直接作为输入来执行它。

linux

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks 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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

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.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

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: Key Features and Capabilities Explained MySQL: Key Features and Capabilities Explained Apr 18, 2025 am 12:17 AM

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.

See all articles