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. java——二叉树面试题

    import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util ...

  2. IOS SQLite函数总结

    SQL语句的种类 ●  数据定义语句(DDL:Data Definition Language) ●  包括create和drop等操作 ●  在数据库中创建新表或删除表(create table或 ...

  3. framework7 可以拉动右侧工具栏和点击当前item就可以出发事件的HTML结构

    <li class="swipeout"> <div class="swipeout-content item-content"> &l ...

  4. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

    cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...

  5. ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id

    远程删除key ssh-keygen -f "~/.ssh/known_hosts" -R 192.168.0.34 如果还是不可以,通过 ssh-keygen 重新生成key

  6. Uva 12657 双向链表

    题目链接:https://uva.onlinejudge.org/external/126/12657.pdf 题意: 给你一个从1~n的数,然后给你操作方案 • 1 X Y : move box X ...

  7. http主要内容流程介绍

    从这张图可以看出,从输入网址,按下回车的那一刻起,就开始了Redirect,那么为什么一开始就redirect呢?因为我们的浏览器可能记录了你的这个地址,以及永久跳转成一个新的地址了,所以一开始浏览器 ...

  8. 轻量级HTTP服务器Nginx(常用配置实例)

    轻量级HTTP服务器Nginx(常用配置实例)   文章来源于南非蚂蚁   Nginx作为一个HTTP服务器,在功能实现方面和性能方面都表现得非常卓越,完全可以与Apache相媲美,几乎可以实现Apa ...

  9. redis 对cmd的操作

    这个是原子递增的知识点: 关于list部分: 利用lpush命令, rpush命令, lrange命令,对列表操作 此前 我已经 在列表(list)中 插入了 部分 元素了 关于集合set 部分 首先 ...

  10. R 语言学习日志 1

      1. CSV文件的的读取与写出 2. 数据集筛选 3. 简单随机抽样 sample函数   正文: 1. CSV文件的的读取与写出 文件读取: df2 <- read.table(" ...