问题描述
环境: CentOS6.5 日前接到需求要把微信数据插入到mysql,遇到表情符插入遇错问题 需要处理流程: 1.修改mysql server 2.确认mysql驱动、连接串 具体错误提示如下: 1366 - Incorrect string value: 'xF0x9Fx98x81' for column 'page' at row 23
1.修改mysql server
step1:/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_unicode_ci init_connect='SET NAMES utf8mb4' PS:mysql 版本必须是5.5.3+ 查看版本:SHOW VARIABLES WHERE Variable_name LIKE ‘version%’;
step2:执行修改数据库和表、字段的字符集sql
ALTER DATABASE dbname CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; alter table tablename modify `NICKNAME` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '昵称';
step3:重启mysql服务
service mysqld restart
2.确认mysql驱动、连接串
我这里用的是python插入数据,经检查Mysql-python模块不支持utf8mb4字符,需要卸载后安装mysqlclient模块
安装方式 pip install mysqlclient-1.3.7-cp27-none-win_amd64.whl 调用方式 import MySQLdb 创建连接 def _connect_mysql(dbjson): try: conn = MySQLdb.connect(host=dbjson['host'], user=dbjson['user'], passwd=dbjson['passwd'], db=dbjson['db'],port=int(dbjson['port']), charset='utf8mb4') conn.ping(True) cursor = conn.cursor() except Exception as e: logging.info('Error:database connection refused..') logging.exception(traceback.print_exc()) exit() return conn, cursor