2017년 6월 1일 목요일

MySQL table의 필드 값이 한글일 경우 Java 소스에서 SELECT 실패할 경우





MySQL 필드의 내용이 한글 일 경우 Java 소스코드 상 SELECT문의 비교문에서 인식이 안되는 문제가 있다.

mysql> select * from board;
+------+-----------------+
| bdid | name            |
+------+-----------------+
|    1 | 공지사항        |
|    2 | QandA           |
|    3 | 자유게시판      |
|    4 | menu            |
+------+-----------------+

board라는 테이블의 내용이 위와 같다고 할 경우

select bdid from board where name = '자유게시판';
과 같은 구문에서 bdid의 값 3이 추출되지 않는다.

만일 select bdid from board where name = 'QandA';
와 같이 한글이 아닌 영문일 경우는 정상적으로 bdid의 값 2를 추출할 수 있다.

이 문제는 MySQL의 환경설정 중 character set상에서의 문제이다.

mysql> show variables like '%chara%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

비록 위와 같이 MySQL variagle들이 utf8로 설정되어 있다하더라도 
/etc/my.cnf라는 설정 파일을 아래와 같이 charater set을 모두 설정해 주어야 한글이 정상적으로 select문에서 작동하게 된다.

[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

character-set-server = utf8
collation-server = utf8_general_ci
init_connect=SET collation_connection = utf8_general_ci
init_connect=set NAMES utf8

character-set-client-handshake = FALSE
skip-character-set-client-handshake
wait_timeout=31536000

[mysqldump]
default-character-set = utf8

# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0

# Recommended in standard MySQL setup
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

위와 같이 설정 후 MySQL 데몬을 재가동해 준다.
# service mysqld restart

댓글 없음:

댓글 쓰기