学习小记 - Flask Web (4) MySQL中文编码

2017-01-05 20:06:17来源:CSDN作者:awakeBird人点击

发布中文文章时,数据库报错:

(_mysql_exceptions.OperationalError) (1366, "Incorrect string value:...)

排查发现是MySQL问题:不支持中文字符的储存(即utf-8编码)。
解决方法:
配置MySQL:
1.停止MySQL服务器:

$ service mysql stop

2.打开配置文件 /etc/mysql/my.cnf

$ sudo vim /etc/mysql/my.cnf

3.在配置文件中加入以下语句:

[client]default-character-set = utf8[mysqld]default-storage-engine = INNODBcharacter_set_server=utf8 init_connect='SET NAMES utf8'collation-server = utf8_general_ci

4.重新启动mysql服务器:

$ service mysql start

5.进入mysql并查询配置:

mysql> show variables like '%char%';+--------------------------+------------------------------------+| 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-5.1.65-osx10.6-x86_64/share/charsets/ |+--------------------------+------------------------------------+8 rows in set (0.00 sec)

6.重新导入数据库,成功。

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台