InnoDB引擎数据库主从复制同步新的分享
近期将公司的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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



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

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

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.

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

The methods for viewing SQL database errors are: 1. View error messages directly; 2. Use SHOW ERRORS and SHOW WARNINGS commands; 3. Access the error log; 4. Use error codes to find the cause of the error; 5. Check the database connection and query syntax; 6. Use debugging tools.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.
