mysql_options(mysql, MYSQL_SET_CHARSET_NAME, cs_name) ã ãã㦠mysql_real_connect(mysql, ...) ããå¾SHOW VARIABLESãã¦ã¿ããæ¥ç¶ã®charsetãè¨å®æ¸ã¿ã®æåããããããã©mysql_real_connectããã®ä¸é£ã®ã³ã¼ãèªãã§ãã©ãã§ãããèµ·ããã®ããããã誰ããããã¦ð¥²https://t.co/ZScoD3tIQ8
— Ryuta Kamizono (@kamipo) February 20, 2021
MySQLã®ãã³ãã·ã§ã¤ã¯ãã±ããã«collation_idã1ãã¤ãã ãå ¥ããããã¨ããããã£ã¦ãcharset name ã®ããã©ã«ãã® collation_id ãéã£ã¦ãã¾ãã
— Inada Naoki (@methane) February 20, 2021
ã¯ã©ã¤ã¢ã³ãã¨ãµã¼ãã¼ã®ãã¼ã¸ã§ã³ãéãã¨ããã©ã«ãã®collation_idãéããã¨ããã£ã¦ç½ ã«ãªãã¾ãã
ãã®å ´åããã©ã«ãã®collationã¯ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãª (libmysqlclient) ã«å®ç¾©ããã¦ãã¦ãMySQL 8.0.ã©ãã 㧠utf8mb4 ã®ããã©ã«ãã®collationãå¤ãã£ãã®ã§ããã¼ã¸ã§ã³ã¢ãããããåä½ãå¤ãã£ãï¼ã£ã¦ãªãå¾ã¾ãã
— Inada Naoki (@methane) February 20, 2021
methaneããã«MySQLã®ãã³ãã·ã§ã¤ã¯ãã±ããã«ã¯collation_idãå ¥ã£ã¦ããã¨ãæãã¦ããã£ãã®ã§ãæ¬å½ã«Handshake Response Packetããcharsetãè¨å®ãã¦ããã®ã調ã¹ã¦ã¿ãã
MySQL :: MySQL Internals Manual :: 14.2 Connection Phase
Handshake Response Packetã®ãã¤ãã¼ãã®æ§é ãè¦ãã¨å é ãã8ãã¤ãç®ã«ãããã«character_setã®idã1ãã¤ãå ¥ããããã£ã½ãã
MySQL :: MySQL Internals Manual :: 14.2.5 Connection Phase Packets
MySQL 8.0ã®default collationã®idä¸è¦§ã¯ããã
MySQL :: MySQL Internals Manual :: 14.1.4 Character Set
ãã®ãã±ããã¯ããµã¼ãã¼ããã®Initial Handshake Packetããã¼ã¹ãããã¨ãæåã«ã¬ã¹ãã³ã¹ããã¨ãã«mysql_fill_packet_header
ã§ä½ããã¦ãµã¼ãã¼ã«éãããã
https://github.com/mysql/mysql-server/blob/mysql-8.0.23/sql-common/client.cc#L4060
ãµã¼ãã¼ã¯parse_client_handshake_packet
ã§ã¯ã©ã¤ã¢ã³ãããã®ã¬ã¹ãã³ã¹ã®å
é ãã8ãã¤ãç®ãcharset_code
ã¨ãã¦åãåºãã¦ããã
https://github.com/mysql/mysql-server/blob/mysql-8.0.23/sql/auth/sql_authentication.cc#L2476
https://github.com/mysql/mysql-server/blob/mysql-8.0.23/sql/auth/sql_authentication.cc#L2581
æçµçã«thd_init_client_charset
ã§åãåºããcs_number
ããç¾å¨ã®ã¹ã¬ãããã³ãã«ã®charsetãè¨å®ãã¦ããã
https://github.com/mysql/mysql-server/blob/mysql-8.0.23/sql/sql_connect.cc#L422-L423
ããã«ãããmysql_options(mysql, MYSQL_SET_CHARSET_NAME, cs_name)
ãã¦mysql_real_connect(mysql, ...)
ããã¨cs_name
ã®default collationãã³ãã¯ã·ã§ã³ã®charsetã¨ãã¦è¨å®ãããããã§ããã
ããã§è¡¨é¡ã® "MySQL 8.0ã®ã¯ã©ã¤ã¢ã³ãã§MySQL 5.7ã®ãµã¼ãã¼ã«æ¥ç¶ããã¨charsetãè¨å®ãããªããããããªã" ã«ã¤ãã¦ãªãã§ãããMySQL 8.0.1ããutf8mb4ã®default collationãutf8mb4_general_ci (id: 45)ããutf8mb4_0900_ai_ci (id: 255)ã«å¤æ´ããããããMySQL 8.0ã®ã¯ã©ã¤ã¢ã³ããuff8mb4ã§ãµã¼ãã¼ã«æ¥ç¶ããã¨id: 255ã®cs_numberãéããã©MySQL 5.7ã¯id: 255ã®cs_numberãç¥ããªãã®ã§ãµã¼ãã¼å´ã®ããã©ã«ãã®è¨å®ãæ¡ç¨ãããã¨ããä»çµã¿ã
çæ³çãªã±ã¼ã¹ã§ã¯ããµã¼ãã¼ã«æ¥ç¶ãããcharsetã¯é©åã«è¨å®ããããã©ãææªã®ã±ã¼ã¹ããµã¼ãã¼ã¯MySQL 5.7ã§ãµã¼ãã¼å´ã®charsetã¯utf8mb4ã«è¨å®ããã¦ãããMySQL 8.0ã®ã¯ã©ã¤ã¢ã³ãããutf8mb4ã§æ¥ç¶ããã±ã¼ã¹ã§ã¯ã³ãã¯ã·ã§ã³ã®charsetã¯utf8mb4ã«è¨å®ãããªãã
ä¸å¿ãæ¥ç¶å¾ã«SET NAMES utf8mb4
ããã°ãµã¼ãã¼å´ã®utf8mb4ã®default collationãè¨å®ãããããææªã®ã±ã¼ã¹ãã«ãã¼ããããã«é©åã«è¨å®ãã¦ãã²ã¨ã«ã¯å¿
è¦ãªãå¦çãå¢ãã¦æããããã¨ã«ãªãã®ã§ãªãã¨ãåé¿ãããæ°æã¡ããããã©ãç¾ç¶ã¯ããããæãã