Ubuntu18.04下mysql无法插入中文的问题

Posted mudaol

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu18.04下mysql无法插入中文的问题相关的知识,希望对你有一定的参考价值。

问题:

   Incorrect string value: ‘xE6xB2x99xE6xB2xB3‘ for column ‘name‘ at row 1

解决:

  在mysql中输入:

    show variables like "char%";

  查看mysql的编码方式:

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

    | 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 |
    | character_sets_dir | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+

  标红的那两行是问题的关键。

  修改配置文件:

    sudo nano /etc/mysql/conf.d/mysql.cnf

    [mysql]

    default-character-set=utf8
    [mysqld]
    character-set-server=utf8

  按Ctrl+o保存,在按ctrl+x退出

  重启mysql服务:

    sudo systemctl restart 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/share/mysql/charsets/ |
    +--------------------------+----------------------------+

  这样就OK了,但是原来创建的数据库和表还是不支持的,需要重新创建。做好数据的备份。

 

以上是关于Ubuntu18.04下mysql无法插入中文的问题的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu18.04安装、卸载MySQL

Ubuntu18.04安装MySQL(未设置密码或忘记密码)

Ubuntu18.04下安装MySQL教程

Ubuntu18.04安装MySQL

新安装的ubuntu18.04系统无法用xshell远程连接问题

在 Ubuntu 18.04 WSL 上安装无人值守的 MySQL 8 社区