MySql修改默认字符编码

database

windows下

在命令行下,查看当前数据库编码

show variables like "%char%";

mysql> show variables like "%char%";

+--------------------------+---------------------------------------------------------+

| Variable_name | Value |

+--------------------------+---------------------------------------------------------+

| character_set_client | utf8mb4 |

| character_set_connection | utf8mb4 |

| character_set_database | utf8 |

| character_set_filesystem | binary |

| character_set_results | utf8mb4 |

| character_set_server | latin1 |

| character_set_system | utf8 |

| character_sets_dir | D:Program FilesMySQLMySQL Server 5.6sharecharsets |

+--------------------------+---------------------------------------------------------+

可以看到,当前数据库客户端默认是utf8编码,服务端是latin1编码

可以通过 set character_set_server=utf8改变服务端编码

mysql> set character_set_server=utf8;

Query OK, 0 rows affected

再次使用show variables like "%char%";查看字符编码

mysql> show variables like "%char%";

+--------------------------+---------------------------------------------------------+

| Variable_name | Value |

+--------------------------+---------------------------------------------------------+

| character_set_client | utf8mb4 |

| character_set_connection | utf8mb4 |

| character_set_database | utf8 |

| character_set_filesystem | binary |

| character_set_results | utf8mb4 |

| character_set_server | utf8 |

| character_set_system | utf8 |

| character_sets_dir | D:Program FilesMySQLMySQL Server 5.6sharecharsets |

+--------------------------+---------------------------------------------------------+

8 rows in set

已经修改过来了,

但是这样的修改,在数据库重新启动之后,数据库编码依然会还原,

此时可以用这个命令

mysql> set global character_set_server=utf8;

Query OK, 0 rows affected

直接修改mysql.ini配置文件来的最稳妥,

找到ProgramDataMySQLmy.ini,这个就是mysql的配置文件

在[client]节点内

添加如下编码信息

[client]

# pipe=

# socket=MYSQL

port=3306

default-character-set=utf8 #utf8mb4

default-collation=utf8_general_ci #utf8mb4

在数据库连接配置的url后边加上characterEnconding=utf8

jdbc:mysql://localhost:3306/xxxx?characterEncoding=utf8&serverTimezone=Asia/Shanghai

即可完美解决。


linux下,同理

使用whereis 或者which 查找mysqld或者mysql的具体安装位置

一般配置文件都在/etc目录下。

然后在port下边添加

default-character-set=utf8 #utf8mb4

default-collation=utf8_general_ci #utf8mb4

即可。

然后

serviece mysqld restart

重启,,mysql服务

以上是 MySql修改默认字符编码 的全部内容, 来源链接: utcz.com/z/534002.html

回到顶部