近期将公司的MySQL架构升级了,由原先的一主多从换成了DRBD+Heartbeat双主多从,正好手上有一个电子商务网站新项目也要上线了,用的是DRBD+Heartbeat双主一从,由于此过程还是有别于以前的MyISAM引擎的,所以这里也将其心得归纳总结了一下:
1)MySQL的replication过程是一个异步同步的过程,并非完全的主从同步,所以同步的过程中是有延迟的,如果做了读写分离的业务的话,建议也要监控此延迟时间;
2)MySQL的master与slave机器记得server-id要保持不一致,如果一样的话,replication过程中会出现如下报错:
代码如下:
Fatal error: The slave I/O threadstopsbecause master and slavehave equal MySQL server ids; these ids mustbedifferent for replication to work(or the --replicate-same-server-id optionmustbe used on slave but this doesnot always make sense; please check themanualbefore 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. Defaultdatabase: '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
附注:--master-data的用法为:通过此参数来备份SQL文件时会建议一个slavereplication,当其值为1时,SQL文件中会记录change master语句;当其值为2时,change master会被写成SQL注释,--master-data在没有使用--single-transaction选项的情况下会自动使用lock-all-tables选项(即这二代选项不要搭配使用)。如何查找SQL中的的LOG_FILE及LOG_POS呢?我们可以用如下命令(请注意change单词要写成大写的),如下所示:
代码如下:
grep "CHANGE"myproject.sql
命令显示结果如下:
代码如下:
CHANGE MASTER TOMASTER_LOG_FILE='mysql-bin.000008',MASTER_LOG_POS=106;
接下来的replication过程就不详细说明了,同步完成后我们经过相当长时间的观察,再也没1062报错了,如下所示:
代码如下:
mysql> show slave status \G;
*************************** 1.row***************************
Slave_IO_State: Waitingformaster to send event
Master_Host: 192.168.11.174
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 27880
Relay_Log_File:centos3-relay-bin.000002
Relay_Log_Pos: 28025
Relay_Master_Log_File: mysql-bin.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 27880
Relay_Log_Space: 28182
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
工作中InnoDB引擎数据库主从复制同步心得以前的项目也比较多的牵涉到InnoDB数据库的备份及replication,较多的一个做法是停库进行replication,虽然也是解决问题的一种思路,但毕竟属于停机维护,在一些特殊应用场景中是不允许的,我们应该多尝试采用mysqldump这种逻辑备份方式来取master主机快照。

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

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