InnoDB 是一个非常不错的 MySQL 的存储引擎,目前使用非常广泛基本所有的网站和项目,我想都会优先选择这个,这个也有很好的诊断和微调的工具.我发现其中一个缺点,就是磁盘空间管理时设计非常低效.这个设计成给所有数据都存到 ibdata1 文件,所以这个文件的存储空间会不断的扩展.InnoDB 并不会收缩这些空间,就算你删除表和数据库.
所以我们需要注意我们的配置.最好一开始就使用 innodb_file_per_table 的选项.这样可以使得更加灵活性的给数据存到每个单表的数据库. 我非常不幸地, 最开始我的数据并没有考虑这个所以没有打开这个参数.后来我测试过,在之后打开这个参数根本不能生效.所以只能 dump 整个数据库,然后在启动这个参数后重新恢复数据库才行.这时我们在想有没有法子,不用关掉数据库的服务时就能完成这个工作.
目前看来只能使用 mysqldump 输出,然后才能恢复.所以我们只能使用 MySQL 的主从模式来切换,才能最大限度地减少停机时间.
所以,我使用的基本步骤是:
- 配置为当前的原始数据库为 Master 数据库.
- 使用 Xtrabackup 来备份你的原始数据库.
- 恢复你的备份和到第二个 MySQL 的实例上.
- 恢复,但不同步,然后运行 mysqldump 在第二个做为 Slave 的数据库上然后导出.
- 停止第二个数据库实例,并删除数据库.
- 创建一个新的数据库,使用前面导出的数据来恢复 MySQL 的实例,记得先要打开 innodb_file_per_table 的选项恢复.
- 配置这个数据库为 slave 然后运行复制.
- 当复制完成时,给这个 slave 切换成主,然后重新配置你的客户端使用这个实例
- 现在可以停止主数据库和删除它.
下面是详细的步骤
准备,配置数据库主从
这个看以前的 Blog 有讲怎么在线做 MySQL 的主从.接下来在 Slave 上做转换的操作
Dump 出 Slave 上的数据
假设你根据我以前的文章都做好了.现在配置好 slave 了.记得使用 innobackupex-1.5.1 -apply-log 恢复后,然后复制到数据库的目录.并修改权限.
这时记得先不要设置同步.直接启动数据后.直接使用 MySQL 的 mysqldump 来导出整个数据库.
123 |
service mysqld start
service mysqld stop
|
创建新的数据库
在新的机器上.安装新的数据库文件.只保留 dump_data.sql.然后使用 mysql_install_db 来建权限数据,MySQL 的数据库
1 |
|
创建新的配置文件
vim /etc/mysql/my.cnf
添加以下行到新的文件:
innodb_file_per_table = 1server-id = 2bin-log = 1
我在次声明,这是成功的关键,innodb_file_per_table 会让过一会导入的数据按表来存, binlog 是为了以后切主的时候使用.
启动 MySQL的并配置 Slave 的同步
先要启动数据库
1 | server mysqld start |
恢复导出的数据库
1 |
|
配置复制
因为刚导入数据库,所以要从现在开始复制,需要在主数据库上根据我以前的文章,给这个设置好权限,然后我们在 Slave 配置一下 slave 的设置
1234567 |
master_port=3306,
master_log_pos=3874; start slave;
|
这个中的 master_log_file 和 master_log_pos 是根据我以前文章刚开始使用 innobackupex 做主数据备份时 xtrabackup_binlog_info 中的信息
1 |
|
当MySQL显示 Seconds_Behind_Master: 0 时,这就 Slave 就同步的和主数据库一样了.
主从切换
现在备份完 Salve 也同步成和主一样,这时因为使用了 innodb_file_per_table 的参数,所以可以切成主了.直接在主数据库中锁一下数据库
1 |
|
然后查看主数据库的状态,写 binlog 到那个文件,什么位置了.
1 | SHOW master STATUS; |
接着到 Slavle 的数据库上看看是不同步到一样的状态了.
1 | SHOW slave STATUS /G; |
确保主从同步的一样后,先到 Slave 上停止 Slave 的复制变成新的 master,这时才方便切成主,所以要在新的 master上执行
1234 |
stop slave
reset slave reset maters
|
之所以要在新的master上执行change master to master_host=”及reset slave,主要是为了断开与老的master之间连接信息.
现在切完了,可以根据以前设置 msater 的经验来给原来的主数据库设置成这个的 slave .也可以给这个重新备份一个出来,在恢复了.

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

SublimeText3 Chinese version
Chinese version, very easy to use

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),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

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