TableCache设置过小造成MyISAM频繁损坏_MySQL
转自老王的博客
前些天说了一下如何修复损坏的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来判断参数是否合理

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

When a Win11 user uses credentials to log in, he or she receives an error message stating that your credentials cannot be verified. What is going on? After the editor investigated this problem, I found that there may be several different situations that directly or indirectly cause this problem. Let's take a look with the editor.

So, you took some great photos at your last party, but unfortunately, most of the photos you took were of red eyes. The photo itself is great, but the red eyes in it kind of ruin the image. Not to mention, some of those party photos might be from your friends’ phones. Today we'll look at how to remove red eye from photos. What causes the red eyes in the photo? Red-eye often occurs when taking photos with flash. This is because the light from the flash shines directly into the back of the eye, causing the blood vessels under the eye to reflect the light, giving the effect of red eyes in the photo. Fortunately, with the continuous advancement of technology, some cameras are now equipped with red-eye correction functions that can effectively solve this problem. By using this feature, the camera takes pictures

A Comprehensive Guide to PHP 500 Errors: Causes, Diagnosis, and Fixes During PHP development, we often encounter errors with HTTP status code 500. This error is usually called "500InternalServerError", which means that some unknown errors occurred while processing the request on the server side. In this article, we will explore the common causes of PHP500 errors, how to diagnose them, and how to fix them, and provide specific code examples for reference. Common causes of 1.500 errors 1.

Many friends always encounter blue screens when using computer operating systems. Even the latest win11 system cannot escape the fate of blue screens. Therefore, today I have brought you a tutorial on how to repair win11 blue screens. No matter whether you have encountered a blue screen or not, you can learn it first in case you need it. How to fix win11 blue screen method 1. If we encounter a blue screen, first restart the system and check whether it can start normally. 2. If it can start normally, right-click "Computer" on the desktop and select "Manage" 3. Then expand "System Tools" on the left side of the pop-up window and select "Event Viewer" 4. In the event viewer, we will You can see what specific problem caused the blue screen. 5. Then just follow the blue screen situation and events

1. Press win+r to open the run window, enter [regedit] and press Enter to open the registry editor. 2. In the opened registry editor, click to expand [HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun]. In the blank space on the right, right-click and select [New - String Value], and rename it to [systray.exe]. 3. Double-click to open systray.exe, modify its numerical data to [C:WindowsSystem32systray.exe], and click [OK] to save the settings.

Title: Table'table_name'ismarkedascrashedandshouldberepaired-How to solve MySQL error: The table is marked as crashed and needs to be repaired. Specific code examples are required. Article text: MySQL is a powerful relational database management system that is widely used in various websites and The application is under development. However, due to various reasons, sometimes we may encounter some errors and problems. in

If your computer often displays a blue screen after starting up, it may be caused by a serious error (stop code 0xF4) caused by the Windows system to the csrss.exe file. Now let’s see how to fix it! How to fix the csrss.exe blue screen. First, press the "Ctrl+Alt+Del" keys at the same time. At this time, the interface from the Microsoft Windows Task Manager will pop up. Click the "Task Manager" tab, and the screen will list all running programs and the resources they occupy and other information. Click again to enter the "Processes" tab, click "Image Name" again, and then find the "csrss.exe" file in the list. Click the "End Process" button
