Preface
 
    I've demonstrated how to change character set in Oracle database in my previous blog.Now,I'm gonna do the similar operation in MySQL database,Let's see the difference of details.
 
Example
 
Create a test table.
 root@localhost:mysql3306.sock [zlm]>create table charset(
-> id int,
-> name varchar()
-> ) engine=innodb charset=utf8;
Query OK, rows affected (0.01 sec)

Check the character set.

 root@localhost:mysql3306.sock [zlm]>\s
--------------
mysql Ver 14.14 Distrib 5.7., for linux-glibc2. (x86_64) using EditLine wrapper Connection id:
Current database: zlm
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.-log MySQL Community Server (GPL)
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql3306.sock
Uptime: 2 min sec

Insert a record contains Chinese characters into test table.

 root@localhost:mysql3306.sock [zlm]>insert into charset values(,'黎明');
Query OK, row affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec)

Change the character from utf8 to to gbk.

 root@localhost:mysql3306.sock [zlm]>set @@global.character_set_database=gbk;
Query OK, rows affected, warning (0.00 sec) root@localhost:mysql3306.sock [zlm]>set @@global.character_set_server=gbk;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>show global variables like 'character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>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/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec)

Start a new mysql client and check the data in test table.

 [root@zlm1 :: ~]
#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. root@localhost:mysql3306.sock [(none)]>show variables like 'character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [(none)]>select * from charset;
ERROR (3D000): No database selected
root@localhost:mysql3306.sock [(none)]>use zlm //After execute "use database",the character set of database will turn into utf8 again.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>select length('黎明') from dual;
+------------------+
| length('黎明') |
+------------------+
| | //The length of one Chinese character occupys three bytes.It depends on the character set of table.
+------------------+
row in set (0.00 sec) //The data still shows correct result after change the database and server character set to gbk. root@localhost:mysql3306.sock [zlm]>\s
--------------
mysql Ver 14.14 Distrib 5.7., for linux-glibc2. (x86_64) using EditLine wrapper Connection id:
Current database: zlm
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.-log MySQL Community Server (GPL)
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: gbk
Db characterset: utf8 //The character set of database turns back to utf8.Therefore,no messy code appears.
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql3306.sock
Uptime: min sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.052
-------------- root@localhost:mysql3306.sock [zlm]>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 | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.01 sec)

Set the character set again in curren session to gbk.

 root@localhost:mysql3306.sock [zlm]>set character_set_database=gbk;
Query OK, rows affected, warning (0.00 sec) root@localhost:mysql3306.sock [zlm]>show variables like 'character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | gbk |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.7.-linux-glibc2.-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+
rows in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec) //Change the character set of client tool(mine is Xshell) to gbk. root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 榛庢槑 | //After changing the character set of client tool,the messy code occurs.
+------+--------+
row in set (0.00 sec)

Change the character set of client tool back to utf8 and insert another record into test table.

 root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
+------+--------+
row in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>insert into charset values(,'上海');
Query OK, row affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+--------+
| id | name |
+------+--------+
| | 黎明 |
| | 上海 |
+------+--------+
rows in set (0.00 sec) //The changing of character set from utf8 to gbk does not influence the result of Chinese characters.

Change the character set of database & server to utf8 again.Then,change the character set of client & connection to gbk.

 root@localhost:mysql3306.sock [zlm]>set character_set_database=utf8;
Query OK, rows affected, warning (0.01 sec) root@localhost:mysql3306.sock [zlm]>set character_set_server=utf8;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>set names gbk;
Query OK, rows affected (0.00 sec) root@localhost:mysql3306.sock [zlm]>\s
--------------
mysql Ver 14.14 Distrib 5.7., for linux-glibc2. (x86_64) using EditLine wrapper Connection id:
Current database: zlm
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.-log MySQL Community Server (GPL)
Protocol version:
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: gbk
Conn. characterset: gbk
UNIX socket: /tmp/mysql3306.sock
Uptime: hour min sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.038
-------------- root@localhost:mysql3306.sock [zlm]>root@localhost:mysql3306.sock [zlm]>select * from charset;
+------+------+
| id | name |
+------+------+
| | hķ |
| | ʏº£ |
+------+------+
rows in set (0.00 sec) //The messy code occured after I've changed the character of my client tool to utf8.

Insert the third record with Chinese characters.

 root@localhost:mysql3306.sock [zlm]>insert into charset values(,'中国');
ERROR (HY000): Incorrect string value: '\xAD\xE5\x9B\xBD' for column 'name' at row //It doesn't permit your insertion operation now 'cause they'll be messy code again.
Summary
  • Cheracter set in MySQL does not make a large influence even though it has so many variables which may confuse us.
  • We can specify character set in a single table or even a column of the table which oracle cannot support.
  • In order to avoid messy code,make sure to keep character set of connection is bigger or equal with the one of our client tool.
  • It's reccomended to use utf8 even utf8mb4 as the character set of MySQL database because it can support almost all the languages
  • Notice that the character set of database may change after you execute "use xxx" to choose a target database.

修改MySQL数据库字符集的更多相关文章

  1. 使用SQL语句修改Mysql数据库字符集的方法

    使用SQL语句修改Mysql数据库字符集的方法   修改库: alter database [$database] character set [$character_set] collate [$c ...

  2. 修改MYSQL数据库表的字符集

    MySQL 乱码的根源是的 MySQL 字符集设置不当的问题,本文汇总了有关查看 MySQL 字符集的命令.包括查看 MySQL 数据库服务器字符集.查看 MySQL 数据库字符集,以及数据表和字段的 ...

  3. MySQL数据库字符集由utf8修改为utf8mb4一例

    对于mysql 5.5 而言,如果不设定字符集,mysql默认的字符集是 latin1 拉丁文字符集: 为了统一管理和应用开发的方便,一般都会统一将操作系统,客户端,数据库各方面的字符集都设置为 ut ...

  4. 修改mysql默认字符集的方法

    +--------------------------+---------------------------------+ | Variable_name | Value | +---------- ...

  5. 修改mysql默认字符集的方案

    mysql默认字符集能否进行修改呢?答案是肯定的,下面就将教您两种修改mysql默认字符集的方法,希望对您学习mysql默认字符集方面能有所启迪. (1) 最简单的修改方法,就是修改mysql的my. ...

  6. 查看和设置MySQL数据库字符集(转)

    查看和设置MySQL数据库字符集作者:scorpio 2008-01-21 10:05:17 标签: 杂谈 Liunx下修改MySQL字符集:1.查找MySQL的cnf文件的位置find / -ina ...

  7. Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码

    由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 &g ...

  8. centos 下修改mysql 默认字符集

    解决办法: CentOS 7下修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 mysql  -u root - ...

  9. Mysql数据库字符集问题

    修改mysql数据库的默认编码方式 修改my.ini文件 加上 default-character-set=gb2312 设定数据库字符集 alter database da_name default ...

随机推荐

  1. SQL:获取语句执行时间2

    获取sql执行时间方法2 --清除缓存 CHECKPOINT; DBCC DROPCLEANBUFFERS; DBCC FREEPROCCACHE; DBCC FREESYSTEMCACHE ('AL ...

  2. nbu8.1配置群集SQL Server实例的备份

    1.About SQL Server high availability (HA) environments SQL Server Intelligent policies support the f ...

  3. telegram汉化和代理

    telegram在Ubuntu18.04的应用商店中可以一键下载. 1.注册:用国内手机号即可,就是验证码可能很慢. 2.汉化:关注zh-CN 频道,在点击其中的安装链接即可. 3.代理: 如果你使用 ...

  4. 使用node.js + socket.io + redis实现基本的聊天室场景

    在这篇文章Redis数据库及其基本操作中介绍了Redis及redis-cli的基本操作. 其中的publish-subscribe机制应用比较广泛, 那么接下来使用nodejs来实现该机制. 本文是对 ...

  5. ios各层

    数据持久层.业务逻辑层.表示层 数据持久层: 持久化(Persistence)意思就是当你退出app的时候它还会存在. dao层:DAO (Data Access Object) 数据访问对象是一个面 ...

  6. Spring任务执行器(TaskExecutor)

    Spring任务执行器(TaskExecutor)    Spring通州任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor可实现一个基于线 ...

  7. B4260 Codechef REBXOR

    真是6块钱cpu(笑 爆炸入口 踹树练习(汗 对于二进制异或和弹性,我们可以贪心的来做. 瓶颈在于快速贪心. 我们可以维护一个trie树,储存异或前缀和.每次在trie树上贪心的跑. 正向and反向跑 ...

  8. OpenFire通过User Service管理用户

    安装OpenFire服务器略去 1.安装User Service插件: 在管理控制平台找到选项卡“插件”,里边有我们需要安装的一个User Service插件,如果安装过了会显示已经安装的哪些插件,没 ...

  9. C#中类的声明

    一.C#中类的声明 在C#中必须先声明类,然后才能在程序中使用. 类的声明格式如下: [类的属性] [访问修饰符] class 类名称 [: 父类名]{    [成员修饰符] 类的成员变量或者成员函数 ...

  10. 【ACM之行】◇第一站◇ 2018HDU多校赛总结

    ◇第一站◇ 2018HDU多校赛 十场多校赛下来,也算是给一个初中生开了眼界……看着清华一次次AK(默默立下flag),看着自己被同校的高中生完虐,一个蒟蒻只能给dalao们垫脚