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

标签 mysql

我在我的 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
    

最佳答案

  • 为 MySQL 服务器设置 UTF-8 字符集排序规则:

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 字符集排序规则:

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

创建数据库 some_database 字符集 utf8 整理 utf8_general_ci;

检查你的配置:

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

关于mysql - 无法更改 MySQL 5.7 中的字符集(从拉丁语到 utf8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253039/

相关文章:

mysql - MariaDB:性能监控

php - 使用for循环和mysql更新数据库

php - Mysql 和 PHP 正确的查询语法

mysql - 我的 sql 查询有些奇怪

mysql - SQL 内连接选择字段列表

mysql - SQL数据库结构问题

php - 从中选择 $x(php 变量)

php - 如何计算每页使用的 MySQL 查询总数?

mysql - 让 MySQL 返回本身包含某些值的 JSON 键

Mysql 多重排序依据