错误:python:mysql error, "Incorrect string value: → Ubuntu中MySQL修改编码utf8

摘要:使用apt-get 命令安装的mysql默认不是utf8,有些时候在插入数据的时候会出现编码错误,可能就是MySQL的默认编码不是utf8,所以需要到配置文件中修改字符编码格式。

例如PyCharm报错:python:mysql error, "Incorrect string value:

一、查看MySQL信息

查看MySQL版本

$ mysql -V
mysql  Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using  EditLine wrapper

查看status

#登录
$ mysql -uroot -p

#进入MySQL输入status查看MySQL状态
$ mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using  EditLine wrapper

Connection id:      37
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.20-0ubuntu0.16.04.1 (Ubuntu)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1 #报错原因就是这里
Db     characterset:    latin1 #报错原因就是这里
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /var/run/mysqld/mysqld.sock
Uptime:         14 hours 52 min 39 sec

Threads: 1  Questions: 1452  Slow queries: 0  Opens: 2701  Flush tables: 1  Open tables: 258  Queries per second avg: 0.027
--------------

下面就是修改MySQL的编码格式

二、修改配置文件

Ubuntu下MySQL的配置文件路径 /etc/mysql/my.cnf
a)打开配置文件

$ vim /etc/mysql/my.cnf

b)添加配置信息
在[client]下追加:

[client]
default-character-set=utf8

在[mysqld]下追加:

[mysqld]
character-set-server=utf8

在[mysql]下追加:

[mysql]
default-character-set=utf8

最终如下图


错误:python:mysql error,
image.png

三、重启MySQL服务

$ service mysql restart

再次登录查看status

--------------
mysql  Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using  EditLine wrapper

Connection id:      37
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.20-0ubuntu0.16.04.1 (Ubuntu)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /var/run/mysqld/mysqld.sock
Uptime:         14 hours 52 min 39 sec

Threads: 1  Questions: 1452  Slow queries: 0  Opens: 2701  Flush tables: 1  Open tables: 258  Queries per second avg: 0.027
--------------

全部为utf8则修改成功!

你可能感兴趣的:(错误:python:mysql error, "Incorrect string value: → Ubuntu中MySQL修改编码utf8)