详解MySQL数据库资源不足的异常错误

数据库 MySQL
MySQL是一个线程管理型的系统, 通过Linux系统的ps -ef无法正常发现其占用的进程数,在无法登录mysql用户或者登录进入mysql的情况下, 可以通过查看proc文件系统检查系统的情况。

前几天,在管理系统的时候遇到一个奇怪的问题, 今天才有机会安装好MySQL环境来重现此问题,由于不是最原始的环境, 所以未必能够完全重现, 我只能努力重现关键问题了.. 我觉得此问题有点特别, 故在此大概的回想下当时的情景..

工作时, 执行了一个su – mysql 的命令, 遇到了下面这样一个错误..

  1. [root@dbmain ~]# su - mysql   
  2. su: cannot set user id: Resource temporarily unavailable  

这是一个Shell中由于资源不足引起的问题, 当时下意识的先运行ulimit,看看ulimit的基本限制.

  1. [root@dbmain ~]# ulimit -a   
  2. core file size          (blocks, -c) 0   
  3. data seg size           (kbytes, -d) unlimited   
  4. scheduling priority             (-e) 0   
  5. file size               (blocks, -f) unlimited   
  6. pending signals                 (-i) 25600   
  7. max locked memory       (kbytes, -l) 32   
  8. max memory size         (kbytes, -m) unlimited   
  9. open files                      (-n) 1024   
  10. pipe size            (512 bytes, -p) 8   
  11. POSIX message queues     (bytes, -q) 819200   
  12. real-time priority              (-r) 0   
  13. stack size              (kbytes, -s) 10240   
  14. cpu time               (seconds, -t) unlimited   
  15. max user processes              (-u) 25600   
  16. virtual memory          (kbytes, -v) unlimited   
  17. file locks                      (-x) unlimited 

又看了看,/etc/security/limits.conf

  1. oracle              soft    nproc   2047   
  2. oracle              hard    nproc   16384   
  3. oracle              soft    nofile  1024   
  4. oracle              hard    nofile  65536   
  5. oracle              soft    memlock        12582912   
  6. oracle              hard   memlock        12582912   
  7. grid              soft    nproc   2047   
  8. grid              hard    nproc   16384   
  9. grid              soft    nofile  1024   
  10. grid              hard    nofile  65536   
  11. grid              soft    memlock        12582912   
  12. grid              hard   memlock        12582912   
  13. mysql             soft    nproc  500   
  14. mysql             hard    nproc  500   
  15. mysql             soft    nofile  1024   
  16. mysql             hard    nofile  65536   
  17. mysql             soft    memlock  12582912   
  18. mysql             hard    memlock  12582912 

经过分析,怀疑也只有process/file这两个出现资源紧张的概率比较大.. 因此就先ps -ef 看系统中该用户的进程数量..

  1. [root@dbmain ~]# ps -ef | grep mysql   
  2. root      4733     1  0 10:30 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/dbmain.pid   
  3. mysql     4788  4733  0 10:30 ?        00:00:04 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql --user=mysql --log-error=/var/lib/mysql/dbmain.err --pid-file=/var/lib/mysql/dbmain.pid   
  4. root     15171 17507  0 13:26 pts/2    00:00:00 mysql -uroot -p   
  5. root     20792 17163  0 15:30 pts/1    00:00:00 grep mysql 

从这个输出,,我们暂时排除nproc超标的可能性.

由此, 就根据此进程的pid进入其proc目录查看当前打开的文件数量..

发现有大量socket的文件连接.. 但是其数量远远未达到文件数的限制, 由此怀疑可能是MySQL的线程也会消耗掉Linux系统的nproc基数, 因此尝试调整/etc/security/limits.conf文件的nproc参数的值.

发现调整过后, su – mysql 确实可以成功执行了,,后面又将此参数改回, 重新执行su – mysql,,此问题又再次重现..由此确认,,使用MySQL的系统, 在设置MySQL的参数max_connections之外, 还需要考虑设置/etc/security/limits.conf文件的大小, MySQL是线程模式执行的, 其线程数也会被统计在nproc中, 这可能掩盖或造成对此问题的误判..

【编辑推荐】 

  1. MySQL数据库集群进行正确配置步骤
  2. MySQL 集群在Server1与Server2上如何安装MySQL
  3. MySQL集群配置
  4. MySQL集群自动安装脚本
  5. MySQL触发器如何正确使用
责任编辑:彭凡 来源: DBhome
相关推荐

2011-04-07 15:47:28

MySQL数据库

2011-07-12 16:41:14

mysql处理异常

2022-03-29 10:52:08

MySQL数据库

2010-03-29 10:19:24

2011-08-05 14:02:17

MySQL数据库异常处理

2010-06-02 11:24:57

MySQL数据库主键

2018-07-30 15:00:05

数据库MySQLJOIN

2011-05-26 14:43:49

ORACLE数据库异常处理

2010-05-19 13:37:39

MySQL数据库密码

2011-08-30 12:51:19

MySQL线程缓冲池

2010-06-11 14:46:13

MySQL数据库

2011-08-17 17:29:32

Windows编译MySQL

2011-08-30 13:40:28

MySQL线程

2010-06-10 17:19:05

MySQL数据库

2010-01-05 09:24:42

MySQL外键约束

2011-09-01 10:56:34

2011-08-25 17:49:14

MySQLmysqlcheck

2011-08-22 18:12:20

OracleORA-01591

2011-07-05 10:03:00

Qt MYSQL 数据库

2009-12-31 11:10:01

点赞
收藏

51CTO技术栈公众号