Home Database Mysql Tutorial 工作中InnoDB引擎数据库主从复制同步心得

工作中InnoDB引擎数据库主从复制同步心得

Jun 07, 2016 pm 05:23 PM
innodb master-slave replication

近期将公司的MySQL架构升级了,由原先的一主多从换成了DRBD+Heartbeat双主多从,正好手上有一个电子商务网站新项目也要上线了,用

近期将公司的MySQL架构升级了,由原先的一主多从换成了DRBD+Heartbeat双主多从,正好手上有一个电子商务网站新项目也要上线了,用的是DRBD+Heartbeat双主一从,由于此过程还是有别于以前的MyISAM引擎的,所以这里也将其心得归纳总结了一下:

1)MySQL的replication过程是一个异步同步的过程,并非完全的主从同步,所以同步的过程中是有延迟的,如果做了读写分离的业务的话,建议也要监控此延迟时间;

2)MySQL的master与slave机器记得server-id要保持不一致,如果一样的话,replication过程中会出现如下报错:

Fatal error: The slave I/O thread stops because master and slavehave equal MySQL server ids; these ids must be different for replication to work(or the --replicate-same-server-id option must be used on slave but this doesnot always make sense; please check the manual before using it).

这个问题很好处理,即将slave机的server-id修改成跟master机器不一致即可。

3)我以前的一个误区就是,slave机器是用自己的二进制日志来完成replication过程的,其实不是这样的,根据复制的工作原理:slave服务器是copy主服务器的二进制日志到自己的中继日志,即relay-log日志(即CentOS3-relay-bin.000002这种名字的)中,然后再把更新应用用到自己的数据库上,所以slave机器是不需要开启二进制日志的,这样过程一样会成功的;除非是准备做主主架构,这才需要slave机器开启二进制日志,这个问题一直在导着我,我以一直以为slave机器搭建replication环境时是一定要开启二进制的,

4)在master机器上授权时,尽量只给某一个或某几个固定机器权限,让它们只有replication slav,replication client权限,尽量不要给grant权限;另外,虽然数据库我们一般是通过内网操作,但越是在在内网对MySQL数据库进行授权操作,越是要注意安全;

5)replication搭建过程按照正常流程走的话,一般很容易实施成功,如果出错的话,多检查下网络环境、权限问题,,一般来说整个搭建过程应该还是会比较顺利的。

在数据库设计初期,我已经将此电子商务的数据库引擎定义为InnoDB,除了数据库中原有的系统表之外,其它表全部由MyISAM转成了InnoDB,原因有二:

1)电子商务业务会涉及到交易付款,在这种基本OLTP的应用中,InnoDB应该作为核心应用表的首选存储引擎;

2)DRBD系统重启时的过程会比较缓慢,会频繁的读表,如果表引擎为MyISAM的话极有可能出现损坏情况,为了造成不必要的问题,我将数据库的表引擎由MyISAM均转成了InnoDB引擎的表。

DRBD+Heartbeat+MySQL参考以前的工作文档,搭建的比较顺利,就是在搭建replication环境时遇到了1062报错,详细过程如下:

初期参考MySQL手册操作,取master机器的快照备份,用的是--single-transaction选项,然后同步过程频繁1062报错,报错日志如下:

Last_SQL_Error: Error 'Duplicate entry 'd36ad91bff36308de540bbd9ae6f4279' for key 'PRIMARY'' on query. Default database: 'myproject'. Query: 'INSERT INTO `lee_sessions` (`session_id`, `ip_address`, `user_agent`, `last_activity`, `user_data`) VALUES ('d36ad91bff36308de540bbd9ae6f4279', '180.153.201.218', 'Mozilla/4.0', 1353394206, '')'

后来改变思路,用--master-data选项来取主master快照备份,命令如下所示:
 
mysqldump -uroot --quick --flush-logs --master-data=1 -p myproject > myproject.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

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)

what is mysql innodb what is mysql innodb Apr 14, 2023 am 10:19 AM

InnoDB is one of the database engines of MySQL. It is now the default storage engine of MySQL and one of the standards for binary releases by MySQL AB. InnoDB adopts a dual-track authorization system, one is GPL authorization and the other is proprietary software authorization. InnoDB is the preferred engine for transactional databases and supports transaction security tables (ACID); InnoDB supports row-level locks, which can support concurrency to the greatest extent. Row-level locks are implemented by the storage engine layer.

How to implement master-slave replication of MySQL database in PHP How to implement master-slave replication of MySQL database in PHP May 17, 2023 am 08:18 AM

With the rapid development of the Internet, Web applications are increasingly integrating database operations. MySQL is a world-renowned relational database system that is widely used. In highly concurrent web applications, MySQL master-slave replication is an important way to improve database performance and availability. This article will introduce how to use PHP to implement master-slave replication of MySQL database. 1. What is MySQL master-slave replication? MySQL master-slave replication refers to copying data from one MySQL database server to another server.

How MySQL sees InnoDB row format from binary content How MySQL sees InnoDB row format from binary content Jun 03, 2023 am 09:55 AM

InnoDB is a storage engine that stores data in tables on disk, so our data will still exist even after shutdown and restart. The actual process of processing data occurs in memory, so the data in the disk needs to be loaded into the memory. If it is processing a write or modification request, the contents in the memory also need to be refreshed to the disk. And we know that the speed of reading and writing to disk is very slow, which is several orders of magnitude different from reading and writing in memory. So when we want to get certain records from the table, does the InnoDB storage engine need to read the records from the disk one by one? The method adopted by InnoDB is to divide the data into several pages, and use pages as the basic unit of interaction between disk and memory. The size of a page in InnoDB is generally 16

Building a Highly Available MySQL Cluster: Best Practice Guide for Master-Slave Replication and Load Balancing Building a Highly Available MySQL Cluster: Best Practice Guide for Master-Slave Replication and Load Balancing Sep 09, 2023 am 10:57 AM

Building a Highly Available MySQL Cluster: Best Practice Guide for Master-Slave Replication and Load Balancing In recent years, with the rapid development of the Internet, the database has become one of the core data storage and processing engines for most web applications. In this scenario, high availability and load balancing have become important considerations in database architecture design. As one of the most popular open source relational databases, MySQL's cluster deployment solution has attracted much attention. This article will introduce how to implement a highly available database cluster through MySQL master-slave replication and load balancing.

How to handle mysql innodb exception How to handle mysql innodb exception Apr 17, 2023 pm 09:01 PM

1. Roll back and reinstall mysql. In order to avoid the trouble of importing this data from other places, first make a backup of the database file of the current library (/var/lib/mysql/location). Next, I uninstalled the Perconaserver 5.7 package, reinstalled the original 5.1.71 package, started the mysql service, and it prompted Unknown/unsupportedtabletype:innodb and could not start normally. 11050912:04:27InnoDB:Initializingbufferpool,size=384.0M11050912:04:27InnoDB:Complete

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Jul 26, 2023 am 11:25 AM

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Introduction: In the MySQL database, the choice of storage engine plays a vital role in system performance and data integrity. MySQL provides a variety of storage engines, the most commonly used engines include InnoDB, MyISAM and Memory. This article will evaluate the performance indicators of these three storage engines and compare them through code examples. 1. InnoDB engine InnoDB is My

How to solve phantom reading in innoDB in Mysql How to solve phantom reading in innoDB in Mysql May 27, 2023 pm 03:34 PM

1. Mysql transaction isolation level These four isolation levels, when there are multiple transaction concurrency conflicts, some problems of dirty reading, non-repeatable reading, and phantom reading may occur, and innoDB solves them in the repeatable read isolation level mode. A problem of phantom reading, 2. What is phantom reading? Phantom reading means that in the same transaction, the results obtained when querying the same range twice before and after are inconsistent as shown in the figure. In the first transaction, we execute a range query. At this time, there is only one piece of data that meets the conditions. In the second transaction, it inserts a row of data and submits it. When the first transaction queries again, the result obtained is one more than the result of the first query. Data, note that the first and second queries of the first transaction are both in the same

Data master-slave replication technology in MySQL Data master-slave replication technology in MySQL Jun 14, 2023 pm 02:10 PM

MySQL database is a very popular relational database management system that supports a variety of data replication technologies, among which the more commonly used is master-slave replication technology. This article will introduce the data master-slave replication technology in MySQL, including principles, implementation methods, common problems and countermeasures. 1. Principle of master-slave replication technology The master-slave replication technology in MySQL can copy the data of a MySQL database to other servers to achieve data backup, load balancing, read-write separation and other functions. Its basic principle is to convert the main database

See all articles