Home > DB > ๐Ÿ’พ[Database] MySQL DB์— ํ•œ๊ธ€ ์‚ฝ์ž….

๐Ÿ’พ[Database] MySQL DB์— ํ•œ๊ธ€ ์‚ฝ์ž….
Database MySQL

๐Ÿ’พ[Database] MySQL DB์— ํ•œ๊ธ€ ์‚ฝ์ž….

  • ํ•œ๊ธ€์„ MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์‚ฝ์ž…ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋Š” ์ฃผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ” ๋˜๋Š” ์—ด์˜ ๋ฌธ์ž ์„ธํŠธ์™€ ๊ด€๋ จ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ…Œ์ด๋ธ”์˜ ๋ฌธ์ž ์„ธํŠธ๋ฅผ UTF-8๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ™‹โ€โ™‚๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ…Œ์ด๋ธ”์˜ ๋ฌธ์ž ์„ธํŠธ๋ฅผ UTF-8๋กœ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•.

1๏ธโƒฃ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ ์‹œ ๋ฌธ์ž ์„ธํŠธ ์„ค์ •.

CREATE DATABASE {๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

2๏ธโƒฃ ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฌธ์ž ์„ธํŠธ ๋ณ€๊ฒฝ.

ALTER DATABASE {๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

3๏ธโƒฃ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์‹œ ๋ฌธ์ž ์„ธํŠธ ์„ค์ •.

CREATE TABLE test (
    id INT AUTO_INCREMENT PRIMARY KEY,
    content TEXT
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

4๏ธโƒฃ ๊ธฐ์กด ํ…Œ์ด๋ธ”์˜ ๋ฌธ์ž ์„ธํŠธ ๋ณ€๊ฒฝ.

ALTER TABLE test CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

5๏ธโƒฃ ๊ฐ ์—ด์˜ ๋ฌธ์ž ์„ธํŠธ ํ™•์ธ ๋ฐ ๋ณ€๊ฒฝ.

ALTER TABLE test MODIFY content TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

6๏ธโƒฃ MySQL ์„œ๋ฒ„์˜ ๊ธฐ๋ณธ ๋ฌธ์ž ์„ธํŠธ๋ฅผ ๋ณ€๊ฒฝ.

  • my.cnf(๋˜๋Š” my.ini) ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ ๊ธฐ๋ณธ ๋ฌธ์ž ์„ธํŠธ๋ฅผ utf8mb4๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ณดํ†ต ์ด ํŒŒ์ผ์€ /etc/my.cnf ๋˜๋Š” /etc/mysql/my.cnf ์— ์œ„์น˜ํ•ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • my.cnf ํŒŒ์ผ์— ๋‹ค์Œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
    ```ini
    [client]
    default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci


### 7๏ธโƒฃ MySQL ์„œ๋ฒ„ ์žฌ์‹œ์ž‘.
```shell
sudo systemctl restart mysqld
  • ์ด์ œ ํ•œ๊ธ€์„ ํฌํ•จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•œ๊ธ€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค๋ฉด:
      INSERT INTO test (content) VALUES ('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ');
      
  • ์ด ๋ฐฉ๋ฒ•์œผ๋กœ UTF-8 ์„ค์ •์„ ์ ์šฉํ•˜๋ฉด ํ•œ๊ธ€ ๋ฐ์ดํ„ฐ๋ฅผ MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฌธ์ œ ์—†์ด ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.