โญ๏ธ Amazon Web Service/Amazon RDS

[mysql ํ•œ๊ธ€์ž…๋ ฅ ์˜ค๋ฅ˜] SQL Error [1366] [HY000]: Incorrect string value

mini_world 2021. 9. 10. 18:26
๋ชฉ์ฐจ ์ ‘๊ธฐ

 

๐Ÿœธ ์˜ค๋ฅ˜ ๋‚ด์šฉ

ํ•œ๊ธ€ ์‚ฌ์šฉ ์‹œ "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

 

 


 

728x90