欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

MySQL的如何计算打开文件数(2)_MySQL

程序员文章站 2022-06-07 18:42:02
...
bitsCN.com

  4. flush tables 后再看看

  mysql>flush tables;
  mysql> show global status like 'open_%';
  +------------------------+-------+
  | Variable_name | Value |
  +------------------------+-------+
  | Open_files | 1 |
  | Open_streams | 0 |
  | Open_table_definitions | 0 |
  | Open_tables | 0 |
  | Opened_files | 52 |
  | Opened_tables | 15 |
  +------------------------+-------+
  lsof | grep /home/mysql
  ...
  mysqld 24349 mysql 5u unix 0x000001041e8de040 4244009 /home/mysql/mysql.sock
  mysqld 24349 mysql 22u unix 0x00000102378ff980 4244128 /home/mysql/mysql.sock
  ...
  可以看到,flush 之后,所有的文件描述符都释放了。
  通过测试可以得知,另一个打开的文件描述符是 slow query log所用。
  如果是有大量的 MyISAM 表,那么就需要特别注意打开文件数是否会超出限制了。
  二、原理
  接下来仔细了解下这个最大文件数相关的参数:
  table_cache (新版本改成了 table_open_cache) The number of cached open tables.
  open_files_limit If this is not 0, then mysqld will use this value to reserve file descriptors to use with setrlimit().
  If this value is 0 then mysqld will reserve max_connections*5 or max_connections + table_cache*2 (whichever is larger)
  number of files.
  如果 open_files_limit 不是设置为 0,则以 setrlimit() 函数计算后的结果为准,如果设置为 0,则实际值是 max_connections*5 或 max_connections + table_cache*2 中的最大者。
  因此,想要解决打开文件数超限的问题,还需要综合系统内核限制(ulimit -n),mysqld自身限制(open_files_limit),以及表缓存数(table_open_cache)等多方面因素。
  不过,实际测试中,发现却不是这样的,open_files_limit采用了内核的最大限制,而非上面的计算结果。
  1. 查看内核限制
  ulimit -n
  65535
  2. 修改 my.cnf 限制

  vi /etc/my.cnf
  ...
  open_files_limit = 10000
  ...
  3. 重启 mysqld
  /etc/init.d/mysql restart
  4. 查看结果

  mysql>show global variables like '%open%';
  | open_files_limit | 65535 |
  | table_open_cache | 1000 |
  5. 不设置 open_files_limit 看看

  vi /etc/my.cnf
  ...
  #open_files_limit = 10000
  ...
  重启
  /etc/init.d/mysql restart
  查看
  mysql>show global variables like '%open%';
  | open_files_limit | 65535 |
  | table_open_cache | 1000 |

bitsCN.com