SHOW VARIABLES LIKE 'character_set%'; .../>

utf8升级utf8mb4

mysql 2018-05-10 浏览(2666 评论(0

一、升级前

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8|
| character_set_connection | utf8                             |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8_general_ci    |
| collation_database   | utf8_general_ci    |
| collation_server     | utf8_general_ci    |
+----------------------+--------------------+

二、修改配置文件

打开MySQL的配置文件(vim /etc/my.cnf),将字符集的配置修改成如下:

[client]  
default-character-set = utf8mb4

[mysql]  
default-character-set = utf8mb4

[mysqld]  
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'

三、升级后

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8mb4                          |
| character_set_connection | utf8mb4                          |
| character_set_database   | utf8mb4                          |
| character_set_filesystem | binary                           |
| character_set_results    | utf8mb4                          |
| character_set_server     | utf8mb4                          |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)

或者用这条命令:SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8mb4                          |
| character_set_connection | utf8mb4                          |
| character_set_database   | utf8mb4                          |
| character_set_filesystem | binary                           |
| character_set_results    | utf8mb4                          |
| character_set_server     | utf8mb4                          |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
| collation_connection     | utf8mb4_general_ci               |
| collation_database       | utf8mb4_general_ci               |
| collation_server         | utf8mb4_general_ci               |
+--------------------------+----------------------------------+
系统变量 描述
character_set_client (客户端来源数据使用的字符集)
character_set_connection (连接层字符集)
character_set_database (当前选中数据库的默认字符集)
character_set_results (查询结果字符集)
character_set_server (默认的内部操作字符集)

这几个变量必须是utf8mb4。

四、 数据库连接的配置

数据库连接参数中: 
character=utf8会被自动识别为utf8mb4,也可以不加这个参数,会自动检测。 
而autoReconnect=true是必须加上的。  

五、 将数据库和已经建好的表也转换成utf8mb4

1、更改数据库编码:ALTER DATABASE backend CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

2、更改表编码:ALTER TABLE login_user CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; 

3、更改mysql字段的编码类型为utf8mb4:TABLE login_user MODIFY COLUMN auth_key VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^

黄信强博客