Database/MySQL

MySQL 한글 깨짐 인코딩 문제 해결하기

Hannah_ko 2020. 10. 13. 11:40
SMALL

2020/10/06 - [Web/Nginx] - Nginx에 MySQL 설치하기 for Centos7

 

Nginx에 MySQL 설치하기 for Centos7

Centos 환경에 PHP를 설치한 후 MySQL을 연동하는 방법을 알아보자. 1. MySQL 설치하기 $ yum install mysql-server 먼저 MySQL 서버를 설치해준다. 2. mysqld 데몬을 실행 및 자동 재시작 설정하기 $ systemctl s..

eusun0830.tistory.com

Centos 7에 MySQL 설치하기

 


 

Centos 7에 MySQL을 설치한 후,

데이터베이스, 테이블 등을 생성하고

 

서버와의 연동 테스트를 하던 중

한글입력시 글자가 깨져서 출력되는 문제를 발견했다.

 

영어, 숫자 등은 제대로 들어가지만

한글만 깨지는 걸로 보아 인코딩 문제라고 생각되어 검색해 보았다.

 

 

mysql을 설치하면 기본 설정이 UTF-8으로 되어있는 경우도 있지만

보통은 latin1_swedish_ci로 설정되있는 경우가 많다고 한다.

 

 

 

1. 먼저 my.cnf 파일에 아래의 설정들을 추가해준다.

리눅스 기준으로 my.cnf 파일은 /etc/my.cnf 로 etc폴더 안에 위치한다.

[client]

default-charcter-set = utf8

...

[mysqld]

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

...

[mysql]

default-character-set = utf8

 

2. 데몬 재 실행

$ systemctl restart mysqld

 

3. mysql 접속 후 인코딩 설정 확인

$ mysql -u root -p
Enter password: *****


mysql> SHOW VARIABLES LIKE 'c%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| check_proxy_users        | OFF                        |
| collation_connection     | utf8_general_ci            |
| collation_database       | utf8_unicode_ci            |
| collation_server         | utf8_unicode_ci            |
| completion_type          | NO_CHAIN                   |
| concurrent_insert        | AUTO                       |
| connect_timeout          | 10                         |
| core_file                | OFF                        |
+--------------------------+----------------------------+
16 rows in set (0.01 sec)

 

3-1. 인코딩 설정 변경 이전에 생성했던 데이터베이스는 latin 인코딩으로 되어있을 확률이 높다.

따라서 기존 데이터베이스는 직접 인코딩을 수정해주어야 한다.

mysql> ALTER DATABASE [변경할 데이터베이스 이름] CHARACTER SET utf8 COLLATE utf8_general_ci;

 

LIST