ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 리눅스와 mysql의 max_open_file 사이즈 변경하기
    프로그래밍/DB 2017. 5. 21. 16:15
    반응형







    환경: 리눅스 CentOS 


    새 서버를 받아 max open file 관련하여 설정을 하여 기록을 남긴다. 

    mysql에서는 보통 max open file 기본 값으로 1024이 설정되어 있는데 

    값이 낮아 mysql 작업 속도를 느리게 하는 원인이 되기도 하니 적절하게 값을 조정해보도록 하자. 



    1.mysql의 max open file 설정 확인


    일단 mysql 의 현재 max open file설정부터 확인해보자.

    ps afx | grep mysql 로 해당 프로세스의 id를 알아내고 프로세스의 설정값을 확인해본다. 

    1024(soft), 4096(hard) 값이 설정되어 있다.  



    shell> ps afx | grep mysql

    48320 pts/3    S+     0:00                      \_ grep mysql

    21919 pts/0    S      0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql

    22021 pts/0    Sl     0:03  \_ /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock


    shell> cat /proc/22021/limits

    Limit                     Soft Limit           Hard Limit           Units

    Max cpu time              unlimited            unlimited            seconds

    Max file size             unlimited            unlimited            bytes

    Max data size             unlimited            unlimited            bytes

    Max stack size            10485760             unlimited            bytes

    Max core file size        0                    unlimited            bytes

    Max resident set          unlimited            unlimited            bytes

    Max processes             30122                30122                processes

    Max open files            1024                 4096                 files

    Max locked memory         65536                65536                bytes

    Max address space         unlimited            unlimited            bytes

    Max file locks            unlimited            unlimited            locks

    Max pending signals       30122                30122                signals

    Max msgqueue size         819200               819200               bytes

    Max nice priority         0                    0

    Max realtime priority     0                    0

    Max realtime timeout      unlimited            unlimited            us

    [root@quick141-176 ~]#


    mysql>  show variables like 'max_connections';

    +-----------------+-------+

    | Variable_name   | Value |

    +-----------------+-------+

    | max_connections | 151   |

    +-----------------+-------+

    1 row in set (0.00 sec)




    또는 mysql 에서 직접 open_files_limit 변수값을 확인할 수도 있다. 



    mysql> show global variables like 'open%';
    +------------------+-------+
    | Variable_name    | Value |
    +------------------+-------+
    | open_files_limit | 1024  |
    +------------------+-------+
    1 row in set (0.00 sec)




    현재 기본 값은 너무 작으니 값을 증가해야 겠다. 
    그리고 mysql 의 max open file값은 시스템이나 사용자에게 기존에 할당된 값 이상으로 변경할 수 없으니 

    mysql max open file 값을 변경하기 이전에 관련 설정들을 확인 및 변경해보자. 


     

    2. OS의 max open file 설정 확인 및 변경 


    일단 서버의 기본 max open file 설정부터 확인해보자. 

    file-max 값은 리눅스에서 한 번에 운용할 수 있는 파일 수를 의미하며, 

    보통 4MB 메모리당 256개의 파일을 운용할 수 있다고 한다.

    대략 1G -> 65536개,  2G -> 131072 개 이다. 

     



    shell> cat /proc/sys/fs/file-max

    382386

     



    이미 충분한 값이 설정되어 있는 것 같다. 

    만약 위에 나온 값을 변경하고 싶으면 터미널에서 아래와 같이 입력하면 된다.  

    그러나 재부팅시 이 값은 유지가 되지 않으니 유의하자. 


    shell> echo 382386 > /proc/sys/fs/file-max



    재부팅시에도 이 값을 계속 유지하려면 

    /etc/sysctl.conf 파일에서 아래와 같이 file-max값 설정을 추가한다. 



    # increase max open file

    fs.file-max = 382386



    그리고 sysctl -p 를 입력하여 설정값이 즉각 변경하도록 한다. 

    이 값은 서버가 재시작을 해도 계속 유지된다.




    shell> sysctl -p
    net.ipv4.ip_forward = 0
    net.ipv4.conf.default.rp_filter = 1
    net.ipv4.conf.default.accept_source_route = 0
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    net.ipv4.tcp_syncookies = 1
    error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
    error: "net.bridge.bridge-nf-call-iptables" is an unknown key
    error: "net.bridge.bridge-nf-call-arptables" is an unknown key
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
    fs.file-max = 382386







    3. 사용자별 max open file 설정 확인 및 변경 



    사용자별 프로세스(task)의 max open file 설정을 확인하려면 터미널에서 ulimit -a를 입력한다.




    shell> ulimit -a
    core file size          (blocks, -c) 0
    data seg size           (kbytes, -d) unlimited
    scheduling priority             (-e) 0
    file size               (blocks, -f) unlimited
    pending signals                 (-i) 30122
    max locked memory       (kbytes, -l) 64
    max memory size         (kbytes, -m) unlimited
    open files                      (-n) 1024
    pipe size            (512 bytes, -p) 8
    POSIX message queues     (bytes, -q) 819200
    real-time priority              (-r) 0
    stack size              (kbytes, -s) 10240
    cpu time               (seconds, -t) unlimited
    max user processes              (-u) 30122
    virtual memory          (kbytes, -v) unlimited
    file locks                      (-x) unlimited



    open file 값이 기본값인 1024으로 설정되어 있어 64000으로 변경하려고 한다. 

    ulimit의 값을 변경하는 방법은 두가지가 있다. 


    터미널에서 아래와 같이 ulimit -n 옵션으로 변경할 수 있다. 

    현재 로그인되어 있는 사용자에 한해 변경이 되며 로그아웃시에는 설정한 값이 유지가 되지 않으니 유의한다.  



    shell> ulimit -n64000



    다시 ulimit -a를 입력하여 해당 값이 제대로 변경된 것을 확인하였다. 




    shell>  ulimit -a

    core file size          (blocks, -c) 0

    data seg size           (kbytes, -d) unlimited

    scheduling priority             (-e) 0

    file size               (blocks, -f) unlimited

    pending signals                 (-i) 30122

    max locked memory       (kbytes, -l) 64

    max memory size         (kbytes, -m) unlimited

    open files                      (-n) 64000

    pipe size            (512 bytes, -p) 8

    POSIX message queues     (bytes, -q) 819200

    real-time priority              (-r) 0

    stack size              (kbytes, -s) 10240

    cpu time               (seconds, -t) unlimited

    max user processes              (-u) 30122

    virtual memory          (kbytes, -v) unlimited

    file locks                      (-x) unlimited



    ulimit -n의 변경값은 일시적이기 때문에 

    이 값을 계속 유지하고 싶으면 /etc/security/limits.conf 파일의 내용을 수정해야 한다. 

    nofile은 해당 사용자(그룹)이 오픈할 수 있는 최대 파일 개수를 의미한다.

    /etc/security/limits.conf 값은 새로운 session 부터 영향을 끼치기 때문에

    파일 설정을 변경하고 재로그인을하거나 새로운 세션으로 다시 접속하면

     해당 설정이 변경된 것을 확인할 수 있다.


    참고로 root user 는 * wildcard 에 속하지 않으니 

    root user   는 따로 설정해줘야한다.


    # 전체 user에게 같은 설정을 하려면 * 을 사용한다


    * hard nofile 64000

    * soft nofile 64000

    root hard nofile 64000

    root soft nofile 64000



    # 특정 user (예: mysql)에기만 설정을 하려면


    mysql hard nofile 64000

    mysql soft nofile 64000




    만약 설정을 변경하였는데 ulimit -a    해도 동일한 결과가 나온다면

    /etc/pam.d/su 의 아래 라인을 코멘트 아웃해야한다. 

    limits.conf   는  pam_limits.so가 사용하는 설정값이라 

    /etc/pam.d/su에서 사용되도록 설정되었는지 확인해야한다.



    # Sets up user limits, please uncomment and read /etc/security/limits.conf

    # to enable this functionality.

    # (Replaces the use of /etc/limits in old login)

    session    required   pam_limits.so








    4. mysql max open file 설정 변경 

     

    자 드디어 mysql max open file 설정을 변경할 차례가 왔다. 

    /etc/my.cnf 파일에 아래와 같이 open-file-limit 옵션을 수정하자. 



    [mysqld_safe]

    open-files-limit=64000




    그리고 mysqld 서비스를 재시작한다. 



    shell> service mysqld restart
    mysqld 를 정지 중:                                         [  OK  ]
    mysqld (을)를 시작 중:                                     [  OK  ]




    마지막으로 수정된 값을 확인하고 설정을 마무리한다. 



    mysql> show global variables like 'open%';
    +------------------+-------+
    | Variable_name    | Value |
    +------------------+-------+
    | open_files_limit | 64000 |
    +------------------+-------+
    1 row in set (0.00 sec)






    참고사이트:

    http://duntuk.com/how-raise-ulimit-open-files-and-mysql-openfileslimit

    http://blog.endpoint.com/2013/12/increasing-mysql-55-maxconnections-on.html

    https://dev.mysql.com/doc/refman/5.0/en/linux-postinstallation.html

    http://jikime.tistory.com/44






    728x90
    반응형
Designed by Tistory.