ERROR 1018 (HY000): Can't read dir of '.' (errno: 24) and other jazz funk classics

本文详细介绍了如何通过调整Linux系统和MySQL配置,解决MySQL连接过多导致的'Too many open files'错误。包括查看当前用户打开文件数、修改limits.conf文件增加文件限制、以及调整MySQL配置中的open-files-limit和table_cache参数。
 

Errno: 24 means 'Too many open files'. Everything you do in linux is a file (or rather a file descriptor).
Every mysql connection, every apache connection etc.

By default ubuntu allows users to open 1024 files. The hard limit is 8192. So if you start to hear the melodies of errno 24 its probably time to start increasing this limit

To see how many files a user has open try this

sudo lsof -u mysql

edit the file in /etc/security/limits.conf and add these two mysql lines in at the bottom

#@student        -       maxlogins       4
mysql   soft    nofile  4096
mysql   hard    nofile  8192
# End of file

There is some debate as to whether you need to reboot for this to take effect (i say debate, i rebooted, and didnt google for it as i was in a rush ymmv).

Once this is set you need to make some changes in the mysql file as well. Assuming yours lives in /etc/mysql/my.cnf - edit or add these values

open-files-limit = 2048
table_cache            = 512

The table_cache should be researched (start here: http://dev.mysql.com/doc/refman/5.0/en/table-cache.html) and changes monitored. The default value is 64, so 512 should be a safe bet to begin with.

### 错误分析与解决方案 在SpringBoot项目中创建`student`表时出现的错误`ERROR 1005 (HY000): Can't create table 'springboot.student' (errno: 150)`,通常与外键约束相关。此错误表明MySQL无法创建表,可能的原因包括但不限于以下几种: #### 1. 数据类型不匹配 外键字段的数据类型必须与其引用的主键字段完全一致,包括长度和字符集。如果数据类型不匹配,MySQL将无法创建外键约束[^2]。 #### 2. 父表不存在或未正确定义主键 外键依赖于父表中的主键或唯一键。如果父表尚未创建,或者父表中的主键未正确定义,则会导致该错误。确保父表已创建,并且主键字段已正确定义[^3]。 #### 3. 存储引擎不支持外键 MySQL的外键约束仅支持InnoDB存储引擎。如果表使用的是MyISAM或其他不支持外键的存储引擎,则会触发此错误。可以通过以下方式检查并修改存储引擎: ```sql SHOW TABLE STATUS WHERE Name = 'student'; ALTER TABLE student ENGINE=InnoDB; ``` #### 4. 字符集或排序规则不一致 如果外键字段和其引用的主键字段的字符集或排序规则不同,也会导致此错误。确保两个字段的字符集和排序规则一致[^2]。 #### 5. 循环依赖 如果存在循环依赖(例如A表依赖B表,而B表又依赖A表),则可能导致外键创建失败。检查表之间的关系,避免循环依赖。 --- ### 示例代码:解决外键问题 假设`student`表需要引用`user`表的`snum`字段,以下是正确的建表语句示例: ```sql -- 创建父表 user CREATE TABLE `user` ( `snum` INT NOT NULL, `name` VARCHAR(45) DEFAULT NULL, PRIMARY KEY (`snum`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 创建子表 student CREATE TABLE `student` ( `id` INT NOT NULL AUTO_INCREMENT, `snum` INT NOT NULL, `grade` VARCHAR(45) DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `fk_student_user` FOREIGN KEY (`snum`) REFERENCES `user` (`snum`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` --- ### 注意事项 - **确保父表已创建**:在创建`student`表之前,确保`user`表已成功创建并包含主键字段`snum`。 - **检查存储引擎**:确认`student`表和`user`表均使用InnoDB存储引擎。 - **验证字段一致性**:确保`student`表中的`snum`字段与`user`表中的`snum`字段在数据类型、长度、字符集等方面完全一致[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值