Solve the problem of error reporting when Mysql stores WeChat expressions

A few days ago, when the user's nickname was obtained through WeChat authorization and stored in the mysql database, a database exception occurred. After multiple investigations, it was finally found that the root cause was that the WeChat emoji was 4-byte UTF-8 encoding. Therefore, the solution is to modify the mysql database encoding to utf8mb4. It should be noted that versions below mysql 5.5 do not support it.

 

The specific method of modifying the database code is as follows:

 

  window my.ini

  [mysql]

  default-character-set=utf8mb4

  [mysqld]

  character-set-server=utf8mb4

  collation-server=utf8mb4_bin

 

  Under linux (my.cnf)

  [client]

  default-character-set = utf8mb4

  [mysql]

  default-character-set = utf8mb4

  [mysqld]

  character-set-server = utf8mb4

  collation-server = utf8mb4_unicode_ci

 

Highlights:

It is not easy to just modify the database encoding. Next, you need to change the encoding of the specific table storage field type to utf8mb4 . The command is as follows:

alter table test_user modify column username varchar(255) character set utf8mb4 collate utf8mb4_unicode_ci comment '用户名'; 

 

Finally, if the database connection information is configured in the java web project or the database connection pool information is configured in tomcat, you need to remove the characterEncoding=UTF-8 configuration , and then re-authorize and save the emoticon WeChat nickname is no problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326513248&siteId=291194637