如果数据库只有一份,那就是数据存储的单点,对于要求可靠性的服务来说,就存在一个单点故障的可能性,这个时候,我们就要通过复制镜像,来解决单点故障。复制还有一个额外的好处,就是可以根据主从,做读写分离,这样,就不会在写入的时候,因为锁表,而降低MySQL的并发性能,所以MySQL复制是MySQL中非常基础的一种操作。
怎么配置
配置Master
首先,要做的是确定一个Master,对于充当Master的MySQL Server来说,需要一些特定的配置才能实现,一个是开启binlog,另一个是要设置server-id。
<br> [mysqld]<br> log_bin = mysql-bin<br> server-id = 1<br>
配置Slave
配置Slave,对于Slave来说,要配置的就是一个唯一的server-id。这个id不能跟Master相同,而且,多个Slave也不能相同。配置完毕后重启。
<br> [mysqld]<br> server-id = 2<br>
创建User
在Master创建一个user,专门用于进行复制用途的。因为在Slave上,user和password会被用明文存储,所以,这个user的权限要尽可能的小,一定要不同于超级用户。
<br> create user repl@'your.domain' identified by 'password';<br> grant replication slave on *.* to repl@'your.domain';<br>
注意,这里第2句,grant语句,必须是赋权限给*.*的,因为replication slave权限是一个global privileges,所以,必须这样,如果想要限制权限在一个比较小的范围,不想复制所有的db的话,可以在my.cnf增加配置项replicate-do-db和binlog-do-db来限定数据库的范围。
获取Master的位置
在Master的数据库上,知行flush tables with read lock;语句,然后知行show master status;这时候,就可以看到当前的binlog文件名和当前知行的sql语句位置。将文件名和执行的位置都记录下来。
这时候,如果Master的数据库的内容非空的话,应该做的事情,就是使用mysqldump来导出数据。空数据库的话,就没必要做什么事情了。
建立连接
在上文空数据库的情况下,是很简单的,stop slave,直接在slave上使用change master to语句,将各项参数设置完毕后,就可以执行start slave了。
如果Master原来有数据,应该把刚才生成的dump文件,传送到slave上,然后首先stop slave,然后导入dump的数据,然后执行change master to语句,将刚刚dump前记录的bin文件和位置都设置正确,然后才能start slave,其实也不麻烦。
状态检查
在比较理想的世界里,到这里,我们的工作就结束了,但是,世界是不理想的,因为各种原因,这种replication的联系,经常会中断的。所以要时不时检查这个联系。
在Master上可以执行show master status看到的东西和上面看到的是一样的。
在Slave上可以执行show slave status,可以看到很多的信息和错误提示。一般情况下是没错的,一旦发生错误了,就应该从这里获取相应的信息来解决问题。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment