๐ธ ์ค๋ฅ ๋ด์ฉ
ํ๊ธ ์ฌ์ฉ ์ "SQL Error [1366] [HY000]: Incorrect string value" ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
Mysql Character Encoding ์ ๋ณ๊ฒฝํ๋ฉด ํ๊ธ์ ์ ์์ ์ผ๋ก ์ธ ์ ์์ต๋๋ค. :)
โป ์ฃผ์์ฌํญ
1) ํ
์ด๋ธ ์์ฑ ์ ์ CHARACTER_SET ๊ฐ์ ๋ณ๊ฒฝํ๋ค๋ฉด ๊ด์ฐฎ์ง๋ง, ํ
์ด๋ธ์ ์ด๋ฏธ ์์ฑํ ๊ฒฝ์ฐ๋ผ๋ฉด ํ
์ด๋ธ, ์ปฌ๋ผ ๋ชจ๋ ์ธ์ฝ๋ฉ์ ๋ณ๊ฒฝํด์ฃผ์ด์ผ ํฉ๋๋ค.
2) ํ๊ธ์ utf8, ์ด๋ชจ์ง๋ utf8mb4๋ฅผ ์ฌ์ฉํฉ๋๋ค. ํ๊ธ๊ณผ ์ด๋ชจ์ง ๋ชจ๋ ์ฌ์ฉํ๋ ค๋ฉด utf8mb4 ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
3) RDS Dynamic Parameter๋ ์ฌ๋ถํ
ํ ํ์ ์์ด ์ด์์ค์ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ๋ก ๋ฐ์๋ฉ๋๋ค. ๋จ, ๊ธฐ์กด ์ฐ๊ฒฐ๋ ์ปค๋ฅ์
์์ ์ ๋ณด๋ฅผ ๋ฌผ๊ณ ์์ ์ ์์ผ๋, ํ๋ผ๋ฉํฐ ๋ณ๊ฒฝํ์๋ ์ ์ฒด ์ปค๋ฅ์
์ ๋ฆฌ๋ก๋ ํด์ฃผ๋ ๊ณผ์ ์ด ํ์ํฉ๋๋ค.
๐ธ ํ์ธ ๋ฐฉ๋ฒ
์๋ฌด ์ค์ ๋ ํ์ง ์๊ณ ๋ฐ๋ก ์ฌ์ฉํ ๊ฒฝ์ฐ, ๊ธฐ๋ณธ ๋ฌธ์ ์
์ด latin1 ์
๋๋ค. ์ด๊ฒ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋์์.!
ํ๊ธ์ insertํ๋ ๊ฒฝ์ฐ '???' ๊ฐ์ผ๋ก ์ ์ฅ๋ฉ๋๋ค.๐
1) ๊ธฐ๋ณธ ๋ณ์ ํ์ธ
mysql> show variables like 'c%';
+--------------------------+-----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /rdsdbbin/oscar-5.7.mysql_aurora.2.07.1.0.425.0/share/charsets/ |
| check_proxy_users | OFF |
| collation_connection | utf8mb4_0900_ai_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
| completion_type | NO_CHAIN |
| concurrent_insert | AUTO |
| connect_timeout | 10 |
| core_file | ON |
+--------------------------+-----------------------------------------------------------------+
16 rows in set (0.01 sec)
2) ํ ์ด๋ธ ์ธ์ฝ๋ฉ ๊ฐ ํ์ธ
mysql> select * from SCHEMATA;
+--------------+--------------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+--------------------+----------------------------+------------------------+----------+
| def | information_schema | utf8 | utf8_general_ci | NULL |
| def | employees | latin1 | latin1_swedish_ci | NULL |
| def | mysql | latin1 | latin1_swedish_ci | NULL |
| def | performance_schema | utf8 | utf8_general_ci | NULL |
| def | sys | utf8 | utf8_general_ci | NULL |
+--------------+--------------------+----------------------------+------------------------+----------+
5 rows in set (0.01 sec)
3) ์ปฌ๋ผ ์ธ์ฝ๋ฉ ๊ฐ ํ์ธ
mysql> SHOW FULL COLUMNS FROM employees.employees;
+------------+---------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+------------+---------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
| emp_no | int(11) | NULL | NO | PRI | NULL | | select,insert,update,references | |
| birth_date | date | NULL | NO | | NULL | | select,insert,update,references | |
| first_name | varchar(14) | latin1_swedish_ci | NO | | NULL | | select,insert,update,references | |
| last_name | varchar(16) | latin1_swedish_ci | NO | | NULL | | select,insert,update,references | |
| gender | enum('M','F') | latin1_swedish_ci | NO | | NULL | | select,insert,update,references | |
| hire_date | date | NULL | NO | | NULL | | select,insert,update,references | |
+------------+---------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
6 rows in set (0.01 sec)
๐ธ ํด๊ฒฐ ๋ฐฉ๋ฒ
1) Aurora ํ๋ผ๋ฉํฐ ๊ทธ๋ฃน ๋ณ๊ฒฝ (๊ธฐ๋ณธ ๋ณ์ ๋ณ๊ฒฝ)
RDS์ ํ๋ผ๋ฉํฐ ๊ทธ๋ฃน ์ค์ ์ ๋ณ๊ฒฝํด์ค๋๋ค. (Aurora์ ๊ฒฝ์ฐ ํด๋ฌ์คํฐ ํ๋ผ๋ฉํฐ๋ฅผ ๋ณ๊ฒฝํ์ธ์ฉ)
Dynamic ๊ฐ์ ์๋ฒ ์ฌ๋ถํ
์์ด ์ ์ฉ ๋ฉ๋๋ค.
ํ๋ผ๋ฉํฐ ๋ณ๊ฒฝ ํ ์ ์ฉ๋ ํ๋ผ๋ฉํฐ ํ์ธ (๋ฐ์๋๋๋ฐ ์๊ฐ์ด ์กฐ๊ธ ๊ฑธ๋ฆด ์ ์์ต๋๋ค.)
mysql> show variables like 'c%';
+--------------------------+-----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8 |
| character_set_filesystem | utf8 |
| character_set_results | utf8mb4 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /rdsdbbin/oscar-5.7.mysql_aurora.2.07.1.0.425.0/share/charsets/ |
| check_proxy_users | OFF |
| collation_connection | utf8mb4_0900_ai_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
| completion_type | NO_CHAIN |
| concurrent_insert | AUTO |
| connect_timeout | 10 |
| core_file | ON |
+--------------------------+-----------------------------------------------------------------+
16 rows in set (0.00 sec)
2) ํ ์ด๋ธ / ์ปฌ๋ผ
ํ ์ด๋ธ ๊ธฐ๋ณธ ์ธ์ฝ๋ฉ ๊ฐ์ utf8๋ก ๋ณ๊ฒฝํฉ๋๋ค.
mysql> alter table employees.employees default charset = utf8;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
ํ
์ด๋ธ์ ๊ธฐ๋ณธ ์ธ์ฝ๋ฉ ๊ฐ์ utf8๋ก ๋ณ๊ฒฝํ๋ค๊ณ ํด๋, ๊ธฐ์กด ์ปฌ๋ผ๋ค์ ๋ฐ์๋์ง ์์ต๋๋ค.
๊ธฐ์กด ์ปฌ๋ผ๊ฐ๋ค์ด ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋๋ก CONVERT ํฉ๋๋ค.
mysql> ALTER TABLE employees.employees CONVERT TO character SET utf8;
Query OK, 300024 rows affected (3.36 sec)
Records: 300024 Duplicates: 0 Warnings: 0