728x90


개요

MySQL 의 default character set 은 MySQL 의 설정 파일에 설정할 수 있다.

배포판
위치
RHEL/CentOS /etc/my.cnf
Ubuntu /etc/mysql/mysql.conf.d/mysqld.cnf


명시적으로 설정하지 않으면 기본 캐릭터 셋은 latin1 이 된다. 현재 characterset 확인은 다음과 같이 mysql 클라이언트로 연결한 후에 status 명령어로 알수 있다.

또는 다음과 같이 mysql 콘솔에서 show variable 을 사용해도 된다.

mysql> show variables like 'char%';


redmine, gitlab 등 거의 모든 app 들은 charset 으로 UTF-8 을 권장하고 있고 실제로도 UTF-8 을 사용하는게 정신건강에 좋다.

MySQL 5.1 과 5.5 이상은 my.cnf 에 캐릭터 셋 설정하는 방식이 약간 다르다.


설정

MySQL 5.5 이상

MySQL 5.5 부터는 default-character-set 옵션이 제거되었고 character-set-server 만 설정해 주면 된다.

skip-character-set-client-handshake 가 빠지면 client 의 시스템 encoding 을 사용하는데 윈도의 경우 euckr 로 설정 될수 있으므로 필요하다.

MySQL 5.5
[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8
skip-character-set-client-handshake


이제 service mysqld restart 로 재구동후에 mysql client 에서 status 명령어로 보면 charset 이 utf-8 로 변경되었을 것이다.


MySQL 5.1

5.1 은 다음과 같이 설정해야 한다.

MySQL 5.1
[mysqld]
character-set-server = utf8
  
[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8

UTF8-MB 

Emoji 는 기존 utf-8 로는 표현을 못하므로 MySQL에서 이모지를 저장하려면 4바이트의 가변 길이를 갖는 utf8mb4 캐릭터 셋을 사용해야 한다. (MySQL 5.5.3 이상 필요)

[mysqld]
collation-server = utf8mb4_unicode_ci
character-set-server = utf8mb4
skip-character-set-client-handshake


이미 기존에 만들어진 database 라면 alter database 명령어로 캐릭터 셋과 collate 를 변경할 수 있다.


character set 변경
ALTER DATABASE homestead CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;


같이 보기

Ref

utf8mb4_unicode_ci 

+ Recent posts