Mysql max_connections 변경하기
💡 요약 정리
- max_connections 값은 동시 접속 가능한 클라이언트 수를 제한하는 MySQL의 핵심 설정입니다.
- 설정 방법은 총 3가지이며, 각각 적용 시점과 지속성이 다릅니다.
- 설정 파일 수정은 재시작이 필요하지만 영구적으로 적용되며, 전역 변수 수정은 재시작 없이 즉시 적용됩니다.
- 시작 시 옵션 지정은 특정 실행 명령에서만 일시적으로 적용됩니다.
1. 설치환경
- CentOS 5.x (64bit)
- MySQL 5.1.59
2. my.cnf 파일을 수정하여 변경하는 방법 (MySQL 재시작 필요)
/etc/my.cnf설정 파일을 수정하여 MySQL 서버 구동 시점에 설정을 반영할 수 있습니다.- 다만, 설정 이후 서버를 재시작해야 적용됩니다.
[root@cafe24]# vi /etc/my.cnf0.0-1.x86_64.rpm
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
max_connections = 2048 <--- 추가설정
#default-character-set=euckr
#character-set-client-handshake = FALSE
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
- 저장 후 MySQL 서버를 재시작합니다.
[root@cafe24 ~]# /etc/rc.d/init.d/mysql restart
Shutting down MySQL... [ OK ]
Starting MySQL. [ OK ]
- 변경된 max_connections 값을 확인합니다.
[root@cafe24 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.59-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like "%max_connection%";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 2048 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql>
3. 전역 변수(Global Variable) 수정 방법 (재시작 불필요)
set global max_connections=값;명령을 사용해 max_connections 값을 즉시 수정할 수 있습니다.- 재시작 없이 빠르게 변경할 수 있지만, MySQL 재시작 시 값이 초기화되므로 주의가 필요합니다.
[root@cafe24 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.59-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like "%max_connection%";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 2048 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> set global max_connections=4096;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like "%max_connection%";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 4096 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql>
4. 서버 시작 시 옵션값을 지정하는 방법
/etc/my.cnf파일에 설정하지 않고, 시작 명령 시-O옵션으로 적용할 수 있습니다.- 일회성 설정이므로 재시작 시 적용되지 않습니다.
[root@cafe24 ~]# /home/APM/mysql/bin/mysqld_safe -O max_connections=1000 -O table_cache=256 -O wait_timeout=300 &
[1] 18912
[root@cafe24 ~]# 130131 09:49:41 mysqld_safe Logging to '/home/APM/mysql/var/cafe24.err'.
130131 09:49:41 mysqld_safe Starting mysqld daemon with databases from /home/APM/mysql/var
- 값이 정상 반영되었는지 확인합니다.
[root@cafe24 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.59-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like "%max_connection%";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 1000 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql>