转自老王的博客
前些天说了一下如何修复损坏的MyISAM表,可惜只会修复并不能脱离被动的境地,只有查明了故障原因才会一劳永逸。
如果数据库服务非正常关闭(比如说进程被杀,服务器断电等等),并且此时恰好正在更新MyISAM表,那么发生损坏的概率就比较大。今天我要说的是另一种情况:频繁的打开关闭MyISAM表文件造成MyISAM表损坏。
什么时候会出现频繁的打开关闭MyISAM表文件的情况呢?
先查看当前系统的table_cache设置,它的作用就是缓存表文件描述符,降低打开关闭表的频率,如果这个参数设置得过小,那么很快就会被占满,再有新请求过来的时候,就不得不关闭一些已打开的表以便为新请求腾出空间,从而出现频繁的打开关闭MyISAM表文件的情况:
mysql> show variables like 'table%';
再查看当前系统的打开表的情况:
mysql> show status like 'open%';
有两项关键的结果:Open_tables和Opened_tables,他们的名字类似,其含义的区别在于:
Open_tables:表示当前打开的表数目。
Opened_tables:表示累计已经打开的表数目。
那么如何判断table_cache是否设置合理呢?其判断尺度如下:
如果Opened_tables远大于Open_tables,并且Open_tables很接近table_cache,那么就说明table_cache偏小。
还要注意设置操作系统的参数,因为即便你把table_cache设置得很大,一旦超过了操作系统的限制也没用,可以按如下方法查询当前值:
ulimit -n
设置方法也很简单,比如设置成8k,可以这样:
vi /etc/security/limits.conf
* hard nofile 8192
* soft nofile 8192
这样设定比在/etc/rc.local里设定ulimit -n 8192更合理一些(参考链接)。
MySQL运行稳定后,查看open_files_limit参数:
mysql> show variables like '%open%';
在大量使用MyISAM的环境里,应该保证open_files_limit表类型至少是table_cache的二到三倍,这是因为每个MyISAM表都包括三个文件:一个表定义文件,一个表索引文件,一个表数据文件,详细介绍可以参考文档。而在Innodb的环境里,一个表只有一个文件,明白这些基本知识对解决问题很有帮助。
具体的数据库文件打开情况可以用lsof来查看:
lsof | grep MYI 或者 lsof | grep MYD
可以发现索引文件描述符是客户端共享的,数据文件则不是,你可以这样确认这一点:
lsof | egrep -i 'myd|myi' | awk '{++state[$NF]} END {for(key in state) print state[key], "/t", key}' | sort -nr
为了保险点,或许还要查查内核的相关参数,比如fs.file-max,这些细节往往会影响到MySQL:
sysctl -a | grep "file"
注意到以上这些因素,问题差不多就能解决了。不过还要注意一点,table cache不是越大越好:
http://www.freshbooks.com/blog/2008/09/09/now-were-flying/
http://www.mysqlperformanceblog.com/2009/11/16/table_cache-negative-scalability/
http://www.mysqlperformanceblog.com/2009/11/26/more-on-table_cache/
BTW:如果你比较懒惰,也可以用MySQL Performance Tuning Primer Script来判断参数是否合理

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

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

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development 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.