无法更改 MySQL 5.7 中的字符集(从拉丁语到 utf8)

Posted

技术标签:

【中文标题】无法更改 MySQL 5.7 中的字符集(从拉丁语到 utf8)【英文标题】:Can't change charset in MySQL 5.7 (from latin to utf8) 【发布时间】:2016-03-19 02:35:07 【问题描述】:

我在我的 Ubuntu 14.04 上安装了 mysql 5.7.8。

但我无法修改 MySQL 默认字符集。我想将字符集更改为 utf8。

我在互联网上搜索并查看了很多文章,但我尝试的每种方法都不适用于我。

我已经在 /etc/mysql/my.cnf 中添加了 'character-set-server=utf8' 和 'collat​​ion-server=utf8_general_ci'。但它不起作用......

如何更改我的 MySQL 字符集?请回答我!

MySQL 中的状态

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:                 1 min 24 sec

显示像'c%'这样的变量

| 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/ |
| check_proxy_users        | OFF                        |
| collation_connection     | utf8_general_ci            |
| collation_database       | latin1_swedish_ci          |
| collation_server         | latin1_swedish_ci          |
| completion_type          | NO_CHAIN                   |
| concurrent_insert        | AUTO                       |
| connect_timeout          | 10                         |
| core_file                | OFF                        |

my.cnf(在 /etc/mysql 中)

[mysqld]
#
# * Basic Settings
#
character-set-server=utf8
collation-server=utf8_general_ci
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
character-set-server=utf8
collation-server=utf8_general_ci
init_connect=set collation_connection=utf8_general_ci
init_connect=set names utf8
character-set-client-handshake=FALSE

【问题讨论】:

【参考方案1】: 为 MySQL 服务器设置 UTF-8 character setcollation

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

然后按 i 并添加以下内容:

...
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
...

Esc 然后按 :w 然后按 :q

然后重启服务器:

sudo /etc/init.d/mysql restart

检查您的配置:

mysql> show variables like "%character%"; 


+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

mysql> show variables like "%collation%";


+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
为数据库和表设置 UTF-8 character setcollation

在创建数据库时明确指定:

CREATE DATABASE some_database CHARACTER SET utf8 COLLATE utf8_general_ci;

检查你的配置:

mysql> SHOW CREATE DATABASE ... 
mysql> SHOW CREATE TABLE ... 

【讨论】:

以上是关于无法更改 MySQL 5.7 中的字符集(从拉丁语到 utf8)的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 从 5.6 升级到 5.7 的 Grails 更改

MySQL 5.7 更改数据目录

MySQL 5.7 JSON_EXTRACT 不适用于对象中的带引号的字符串:[错误]“字符串中缺少右引号”

在 MySQL 中使用拉丁字符集存储电子邮件和域名

MySQL 5.7与MySQL 8中的UTF-8 Unicode字符

Percona Server 5.7 源码安装