MySQL数据库与PHP搭配是实现MySQL双机热备的最佳组合,其原理主要是通过对日志的更新,用MySQL数据库(和PHP搭配之最佳组合)的SELECT的文件来操作相关功能,备机实时抓取主机的更新日志。 当然这只是其原理,实际上并不需要我们自己去处理日志,明白了原理,实
MySQL数据库与PHP搭配是实现MySQL双机热备的最佳组合,其原理主要是通过对日志的更新,用MySQL数据库(和PHP搭配之最佳组合)的SELECT的文件来操作相关功能,备机实时抓取主机的更新日志。
当然这只是其原理,实际上并不需要我们自己去处理日志,明白了原理,实施就比较容易理解了。
这样,在主机端需要开一个账号,这个账号是备机用来抓取主机的更新日志。需要有文件访问权限,在早期,刚开始实现MySQL双机热备时,就是用的文件权限。
从MySQL(和PHP搭配之最佳组合)4 开始,添加了一个专门的权限,用来做热备,这个权限本质应该还是文件读取权限,但是应该只能用来读取日志,防止一些漏洞。
对于客户端来说,就是设置这个账号,密码,主服务器地址,还有要同步的数据库名。这只是单向的,再配置一个对等的同步通道,就支持双向的热备了。
通过热备,还可以备端从主端load全部数据。这个在同步出错时可以使用。load权限是一个单独的MySQL(和PHP搭配之最佳组合)权限,这样跟热备有关的MySQL(和PHP搭配之最佳组合)的权限有2个,日志抓取和数据载入(REPLICATION SLAVE, REPLICATION CLIENT )
在服务器端开备份账号
GRANT REPLICATION SLAVE , REPLICATION CLIENT ON * . * TO "backup"@ "192.168.1.2"IDENTIFIED BY "*****"WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 ;
在服务器端打开日志功能
/etc/MySQL(和PHP搭配之最佳组合)/my.cnf
[MySQL双机热备(和PHP搭配之最佳组合)d]
<ol class="dp-xml"> <li class="alt"><span><span class="attribute">server-id</span><span> = </span><span class="attribute-value">1</span><span> </span></span></li> <li><span>log-bin </span></li> <li class="alt"> <span class="attribute">binlog-do-db</span><span> = </span><span class="attribute-value">pa</span><span> </span> </li> <li> <span class="attribute">max_binlog_size</span><span> = </span><span class="attribute-value">104857600</span><span> </span> </li> </ol>
这样,MySQL(和PHP搭配之最佳组合)会在数据目录放置pa这个库的更新日志。等待备机来抓取。
客户端设置:
/etc/MySQL(和PHP搭配之最佳组合)/my.cnf
<ol class="dp-xml"> <li class="alt"><span><span class="attribute">master-host</span><span>=</span><span class="attribute-value">192</span><span>.168.1.1 </span></span></li> <li> <span class="attribute">master-user</span><span>=</span><span class="attribute-value">backup</span><span> </span> </li> <li class="alt"> <span class="attribute">master-password</span><span>=</span><span class="attribute-value">12345</span><span> </span> </li> <li> <span class="attribute">master-port</span><span>=</span><span class="attribute-value">3306</span><span> </span> </li> <li class="alt"> <span class="attribute">master-connect-retry</span><span>=</span><span class="attribute-value">60</span><span> </span> </li> </ol>
replicate-do-db=pa 客户端会到服务器抓取pa库的更新日志,来更新本地的pa库。
几个跟热备有关的MySQL(和PHP搭配之最佳组合)命令:(需要在MySQL(和PHP搭配之最佳组合)命令行界面或query )
stop slave #停止同步
start slave #开始同步,从日志终止的位置开始更新。
SET SQL_LOG_BIN=0|1 #主机端运行,需要super权限,用来开停日志,随意开停,会造成主机从机数据不一致,造成错误
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=n # 客户端运行,用来跳过几个事件,只有当同步进程出现错误而停止的时候才可以执行。
RESET MASTER #主机端运行,清除所有的日志,这条命令就是原来的FLUSH MASTER
RESET SLAVE #从机运行,清除日志同步位置标志,并重新生成master.info
虽然重新生成了master.info,但是并不起用,最好,将从机的MySQL(和PHP搭配之最佳组合)进程重启一下,
LOAD TABLE tblname FROM MASTER #从机运行,从主机端重读指定的表的数据,每次只能读取一个,受timeout时间限制,需要调整timeout时间。执行这个命令需要同步账号有reload和super权限。以及对相应的库有select权限。如果表比较大,要增加net_read_timeout 和 net_write_timeout的值
LOAD DATA FROM MASTER #从机执行,从主机端重新读入所有的数据。执行这个命令需要同步账号有reload和super权限。以及对相应的库有select权限。如果表比较大,要增加net_read_timeout 和 net_write_timeout的值
CHANGE MASTER TO master_def_list #在线改变一些主机设置,多个用逗号间隔,比如
<ol class="dp-xml"> <li class="alt"><span><span>CHANGE MASTER TO </span></span></li> <li> <span class="attribute">MASTER_HOST</span><span>=</span><span class="attribute-value">'master2.mycompany.com'</span><span>, </span> </li> <li class="alt"> <span class="attribute">MASTER_USER</span><span>=</span><span class="attribute-value">'replication'</span><span>, </span> </li> <li> <span class="attribute">MASTER_PASSWORD</span><span>=</span><span class="attribute-value">'bigs3cret'</span><span> </span> </li> </ol>
MASTER_POS_WAIT() #从机运行
SHOW MASTER STATUS #主机运行,看日志导出信息
SHOW SLAVE HOSTS #主机运行,看连入的从机的情况。
<ol class="dp-xml"> <li class="alt"><span><span>SHOW SLAVE STATUS (slave) </span></span></li> <li><span>SHOW MASTER LOGS (master) </span></li> <li class="alt"><span>SHOW BINLOG EVENTS [ IN 'logname' ] [ FROM pos ] [ LIMIT [offset,] rows ] </span></li> <li><span>PURGE [MASTER] LOGS TO 'logname' ; PURGE [MASTER] LOGS BEFORE 'date' </span></li> </ol>
下面是Q&A时间:
MySQL双机热备怎么配置?照上面再配置一个反向的更新就行了。
不用担心本机的更改会回环回来,因为server_id就是识别这个用的.
多机热备怎么做,几台MySQL(和PHP搭配之最佳组合)服务器就像首尾相连的蛇,组成一个环装,就可以了,而且还可以作几个单向的更新,用以分担select这样的读取操作的压力,因为MySQL(和PHP搭配之最佳组合)操作中大部分是

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

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.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

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

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment