지구정복

[MySQL] CentOS 7 리눅스에서 MySQL 한글 깨짐 현상 해결 본문

데이터 엔지니어링 정복/SQL

[MySQL] CentOS 7 리눅스에서 MySQL 한글 깨짐 현상 해결

eeaarrtthh 2021. 6. 6. 15:51
728x90
반응형
SMALL

내 환경

mysql 5.7.28

 

 

먼저 mysql 에 접속한 뒤 아래 명령어로 인코딩타입을 확인한다.

show variables like 'c%';

아래와 같이 latin이 있는 것을 확인할 수 있다.

 

다시 msyql접속을 끊고 설정파일을 아래와 같이 수정한다.

오타가 나지 않도록 조심한다!!

su - root
비번

vi /etc/my.cnf

[client-server]
!includedir /etc/my.cnf.d

[client]
default-character-set = utf8

[mysqld]
port=3307
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

init_connect = "SET collation_connection = utf8_general_ci"
init_connect = "SET NAMES utf8"
character-set-server = utf8
collation_server = utf8_general_ci
lower_case_table_names = 1

[mysqldump]
default-character-set = utf8

[mysql]
default-character-set = utf8

 

그리고 mysqld를 재실행한다.

systemctl restart mysqld

 

그리고 다시 상태를 확인한다.

systemctl status mysqld

아래와 같이 초록불이면 정상이다.

 

만약에 빨간불이고 conflict라고 뜨면 my.cnf에서 character-set-server 와 collation_server는 제거한다.

 

 

이제 다시 mysql 에 접속해서 인코딩 타입을 확인한다.

show variables like 'c%';



혹시라도 character_set_database와 Collation_database가 안바뀌었다면 아래와 같이 직접 바꿔줘야 한다.

 

alter database mysql character set = utf8 collate = utf8_general_ci;

 

마지막으로 한글을 사용할 데이터베이스는 새롭게 만들어줘야한다.

기존 데이터베이스의 테이블에 한글을 사용하면 똑같이 깨진다.

 

또한 테이블에 대해서도 따로 설정해줘야한다.

use 사용할데이터베이스;
alter table 테이블명 convert to character set utf8;

 

실제로 한글을 insert하고 확인해본다.

create database test;

use test;

create table temp2(
	no int,
	content varchar(20),
	mor varchar(10),
	cnt int
);

insert into temp2 values ( 1, '가가가', '나나나', 3);

select * from temp2;
+------+-----------+-----------+------+
| no   | content   | mor       | cnt  |
+------+-----------+-----------+------+
|    1 | 가가가    | 나나나    |    3 |
+------+-----------+-----------+------+
1 row in set (0.00 sec)

 

 

성공!

 

 

 

참고: https://im-codding.tistory.com/3

 

 

728x90
반응형
LIST
Comments