如何修改mysql字符编码

说明:mysql字符编码修改有几种方法,直接修改配置文件,也可以通过命令的方式,此处只介绍修改配置文件的方法。

一. 检查mysql编码

入如下sql行,得到

mysql> show variables like 'character\_set\_%';

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

| Variable_name | Value |

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

| character_set_client | utf8 |

| character_set_connection | utf8 |

| character_set_database | latin1 |

| character_set_filesystem | binary |

| character_set_results | utf8 |

| character_set_server | latin1 |

| character_set_system | utf8 |

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

7 rows in set (0.01 sec)

mysql> show variables like 'collation_%';

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

| Variable_name | Value |

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

| collation_connection | utf8_general_ci |

| collation_database | latin1_swedish_ci |

| collation_server | latin1_swedish_ci |

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

3 rows in set (0.00 sec)

二.  修改mysql库编码

如果2.8编码并utf8需修改库编码按照以下步骤进

行以下命令:

service mysqld stop

cp /usr/share/mysql/my-huge.cnf /etc/my.cnf

vi /etc/my.cnf

以上命令,先停止了mysql,而后制一示例配置文件作配置。

my.cnf文件中找到“[client],在其下添加如下

default-character-set=utf8

my.cnf文件中找到“[mysqld],在其下添加如下

character-set-server=utf8

collation-server=utf8_general_ci

init-connect='SET NAMES utf8'

保存并关闭my.cnf

mysql

service mysqld start

修改完成后,再次第一步中查询,确修改已成功。