DataBase๐Ÿ’พ

[MySQL] MySQL ํ•œ๊ธ€๊นจ์ง ํ•ด๊ฒฐ๋ฐฉ๋ฒ•!

hae02y 2021. 3. 18. 02:03
๋ฐ˜์‘ํ˜•

์„ค์น˜ ํ™˜๊ฒฝ

Server ์–ธ์–ด: JAVA / JSP (Java v1.7), Spring Framework

WAS1Tomcat v7.0

DBMS: MySQL v5.1.41->v5.6.23, MySQL Workbench v5.2.38

 

MySQL์„ ์„ค์น˜ํ•œ ๊ฒƒ์€ ํ›จ์”ฌ ์ด์ „์ด๊ณ , ํ•œ๊ธ€์กฐ์ฐจ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ž‘์€ ํ”„๋กœ์ ํŠธ๋งŒ ์ง„ํ–‰ํ•˜๋˜ ํƒ“์— (.. ) 

๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ํ™˜๊ฒฝ์—์„œ ์ฝ”๋”ฉ์„ ํ•˜๋Š” ๋„์ค‘, ํ•œ๊ธ€์ด ์ œ๋Œ€๋กœ ํ‘œ์‹œ๋˜์ง€ ์•Š๋Š” ๋‚œ๊ด€์— ๋ถ€๋”ซํ˜”์Šต๋‹ˆ๋‹ค.

 

์œ„์—์„œ ์ œ์‹œํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ํ™˜๊ฒฝ์—์„œ ํ•œ๊ธ€์˜ I/O๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด, ํฌ๊ฒŒ ๋„ค ๊ฐ€์ง€ ์ธก๋ฉด์— ๋Œ€ํ•œ ์„ค์ •์„ ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  ๊ทธ ๋„ค ๊ฐ€์ง€๋Š” ์›น ์„œ๋น„์Šค์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋˜๊ธฐ๋„ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์„œ๋ฒ„ ์–ธ์–ด, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„

ํด๋ผ์ด์–ธํŠธ ์–ธ์–ด์ด๋ฉฐ, ์ด๊ฒƒ๋“ค์— ๋Œ€ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” encoding ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •์„ ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

 

๋จผ์ € MySQL ์ฝ˜์†”์—์„œ show variables like 'c%'; ๋ฅผ ์‹คํ–‰์‹œ์ผœ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ( default ์ผ ๊ฒฝ์šฐ )

ํ˜น์€, status ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ˜„์žฌ์˜ ๋ฌธ์ž ์…‹์„ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ด ๋ช…๋ น์–ด๋Š” ํ˜„์žฌ DB์˜ ๋ฌธ์ž ์…‹2๊ณผ ์ฝœ๋ ˆ์ด์…˜3์˜ ์„ค์ • ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š”๋ฐ, 

MySQL์˜ ๊ธฐ๋ณธ ์„ค์ •์€ latin1, latin1_swedish_ci ์ƒํƒœ์ž…๋‹ˆ๋‹ค.

 

ํ•œ๊ธ€์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ, ๋ฌธ์ž ์…‹๊ณผ ์ฝœ๋ ˆ์ด์…˜์˜ ์„ค์ •์„ ๋ฐ”๊พธ์–ด ์ค๋‹ˆ๋‹ค.

 

๋จผ์ €, MySQL์ด ์„ค์น˜๋œ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” my-medium.ini ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜์—ฌ my.ini ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ํ›„, my.ini ํŒŒ์ผ์— ์•„๋ž˜ ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ ๋˜๋Š” ์ˆ˜์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

 

[mysql]   
default-character-set=utf8
 
[mysqld]   
character-set-client-handshake = FALSE  
init_connect = "SET collation_connection = utf8_general_ci"  
init_connect = "SET NAMES utf8"  
default-character-set = utf84
character-set-server = utf8  
collation-server = utf8_general_ci  
 
[client]    
default-character-set = utf8  
 
[mysqldump]    
default-character-set = utf8

 

 

์œ„์™€ ๊ฐ™์ด ํŒŒ์ผ์„ ์ˆ˜์ •ํ–ˆ๋‹ค๋ฉด, ์ œ์–ดํŒ - ๊ด€๋ฆฌ๋„๊ตฌ - ์„œ๋น„์Šค - MySQL์—์„œ DB๋ฅผ ์žฌ๊ธฐ๋™ ์‹œํ‚ต๋‹ˆ๋‹ค.

 

๊ทธ ํ›„ MySQL์ฝ˜์†”๋กœ ๋“ค์–ด๊ฐ€์„œ show variables like 'c%'; ๋ฅผ ๋‹ค์‹œ ์‹คํ–‰์‹œ์ผœ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. 

๋ชจ๋“  ๋ฌธ์ž์…‹๊ณผ ์ฝœ๋ ˆ์ด์…˜ ์„ค์ •์—  utf-8์ด ์ ์šฉ๋œ ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

ํ•˜์ง€๋งŒ windows์—์„œ MySQL ๋ฌธ์ž ์…‹๊ณผ ์ฝœ๋ ˆ์ด์…˜์„ utf-8๋กœ ์„ค์ •ํ•˜๋”๋ผ๋„ ์ฝ˜์†” ์ฐฝ์œผ๋กœ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๋ฉด ๊ทธ๋ž˜๋„ ํ•œ๊ธ€์ด ๊นจ์ ธ 

๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” MySQL ์ฝ˜์†”์˜ ๋ฌธ์ž ์…‹ ์„ค์ •์ด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด MS949๋กœ ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

 

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”

1. cmd์‹คํ–‰ ํ›„ ๊ธ€๊ผด์„->๋ž˜์Šคํ„ฐ ๊ธ€๊ผด ์ด์™ธ์— ๋‹ค๋ฅธ ๊ฑธ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

2. cmd์ฐฝ์—์„œ chcp5 65001 ์„ ์ž…๋ ฅํ•˜๋ฉด ์ฐฝ์˜ ๋ฌธ์ž ์…‹์ด utf-8๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค.

์ €๋Š” MySQL Workbench๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ, ๋”ฐ๋กœ ์„ค์ •์„ ํ•ด์ฃผ์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ† ๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

MySQL Workbench์—์„œ๋Š” ๋ณ„๋‹ค๋„๋กœ ๋ฌธ์ž ์…‹์„ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ํ•œ๊ธ€์˜ ์ž…๋ ฅ ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

 

2. ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„

 

Connector ์ •๋ณด์— URIEncoding="UTF-8"๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

<Connector port="8080" protocol="HTTP/1.1" redirectPort="8443" connectionTimeout="20000" URIEncoding="UTF-8" />

 

์ด ์„ธํŒ…์€ ์›นํŽ˜์ด์ง€์˜ URL์—์„œ ๋ฌธ์ž์—ด์„ ์ฝ์–ด์˜ฌ ๋•Œ ์„œ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋Š” URL์˜ ์ธ์ฝ”๋”ฉ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Internet Explorer์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ "URL์„ ํ•ญ์ƒ UTF-8๋กœ ๋ณด๋ƒ„"์ด ์ผœ์ ธ ์žˆ์œผ๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„์— UTF-8๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฅผ ์„œ๋ฒ„์—์„œ ์ฝ์–ด๋“ค์ผ๋•Œ UTF-8์ด ์„ธํŒ…๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

3. ์„œ๋ฒ„ ์–ธ์–ด

 

Eclipse ๊ณ„์—ด์˜ ์—๋””ํ„ฐ์—์„œ Encoding ์„ธํŒ…ํ•ด ์ค๋‹ˆ๋‹ค

 

 

 

4. ํด๋ผ์ด์–ธํŠธ ์–ธ์–ด

 

1) ํŽ˜์ด์ง€ ์„ค์ •

 

*.html, *.htm, *.jsp ํŒŒ์ผ์€ UTF-8 ํ˜•์‹์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

๋ชจ๋“  HTML, JSP์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ METAํƒœ๊ทธ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

JSP์—์„œ๋Š” ํŽ˜์ด์ง€ ์ƒ๋‹จ์— page ์ง€์‹œ์ž๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋„ฃ์Šต๋‹ˆ๋‹ค.

 

<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

 

 

2) ๋ฐ์ดํ„ฐ ์ „์†ก ์‹œ ์„ค์ •

 

POST๋ฐฉ์‹์œผ๋กœ ์ „์†ก๋œ ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ผ์ธ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํ•ด๊ฒฐ์ด ๋ฉ๋‹ˆ๋‹ค.

 

request.setCharacterEncoding("UTF-8");

 

GET๋ฐฉ์‹์œผ๋กœ ์ „์†ก๋œ ๋ฐ์ดํ„ฐ๋Š” URL์„ ํ†ตํ•ด ์ „์†ก๋˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ๋ฐฉ์‹์œผ๋ก  ์“ธ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

URL์—์„œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ Latin-1(8859_1) ์ธ์ฝ”๋”ฉ์œผ๋กœ ์„œ๋ฒ„์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ฐ์บฃ์˜ ๋ฒ„๊ทธ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ถˆ๋Ÿฌ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

request.setCharacterEncoding("UTF-8");

String data = new String(request.getParameter("data").getBytes("8859_1"),"UTF-8");

 

ํ•œ๊ธ€ ์ด๋ฆ„์˜ ํŒŒ์ผ์„ ์ „์†กํ•  ๋•Œ๋Š” Latin-1(8859_1)๋กœ ์„ธํŒ…๋œ ํ†ฐ์บฃ ์„œ๋ฒ„์—์„œ๋Š” URL์„ ๋ฌด์กฐ๊ฑด ์˜์–ด๋กœ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ 

ํ•œ๊ธ€ ํŒŒ์ผ์€ ์ ˆ๋Œ€๋กœ ์ฝ์–ด๋“ค์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์˜์–ด๋กœ ๋ฐ”๊พธ์–ด ์ €์žฅํ•˜๊ฑฐ๋‚˜, ํ•œ๊ธ€ ํŒŒ์ผ์„ ์˜์–ด๋กœ ๋ฐ”๊พธ๋Š” ์ฒ˜๋ฆฌ๋ฅผ 

ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

3) DB์— ๋Œ€ํ•œ ์ฃผ์†Œ

 

jdbc:mysql://localhost:3306/DB๋ช…?useUnicode=true&characterEncoding=utf8 [ Java ๋˜๋Š” JSP ]

jdbc:mysql://localhost:3306/DB๋ช…?useUnicode=true&amp;characterEncoding=utf8 [ xml ]

 

 

 

์œ„์˜ ์‚ฌํ•ญ๋“ค์— ๋Œ€ํ•ด ํ™•์ธ์„ ํ•ด ๋ณด์•˜๋‹ค๋ฉด, ์„œ๋ฒ„๋Š” ๋ฌธ์ œ์—†์ด ์ž˜ ๋™์ž‘ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ์ด์ƒ์œผ๋กœ, ๊ธ€์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•