修改MySQL数据库字符集
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.
- 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数据库字符集的更多相关文章
- 使用SQL语句修改Mysql数据库字符集的方法
使用SQL语句修改Mysql数据库字符集的方法 修改库: alter database [$database] character set [$character_set] collate [$c ...
- 修改MYSQL数据库表的字符集
MySQL 乱码的根源是的 MySQL 字符集设置不当的问题,本文汇总了有关查看 MySQL 字符集的命令.包括查看 MySQL 数据库服务器字符集.查看 MySQL 数据库字符集,以及数据表和字段的 ...
- MySQL数据库字符集由utf8修改为utf8mb4一例
对于mysql 5.5 而言,如果不设定字符集,mysql默认的字符集是 latin1 拉丁文字符集: 为了统一管理和应用开发的方便,一般都会统一将操作系统,客户端,数据库各方面的字符集都设置为 ut ...
- 修改mysql默认字符集的方法
+--------------------------+---------------------------------+ | Variable_name | Value | +---------- ...
- 修改mysql默认字符集的方案
mysql默认字符集能否进行修改呢?答案是肯定的,下面就将教您两种修改mysql默认字符集的方法,希望对您学习mysql默认字符集方面能有所启迪. (1) 最简单的修改方法,就是修改mysql的my. ...
- 查看和设置MySQL数据库字符集(转)
查看和设置MySQL数据库字符集作者:scorpio 2008-01-21 10:05:17 标签: 杂谈 Liunx下修改MySQL字符集:1.查找MySQL的cnf文件的位置find / -ina ...
- Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码
由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 &g ...
- centos 下修改mysql 默认字符集
解决办法: CentOS 7下修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 mysql -u root - ...
- Mysql数据库字符集问题
修改mysql数据库的默认编码方式 修改my.ini文件 加上 default-character-set=gb2312 设定数据库字符集 alter database da_name default ...
随机推荐
- React怎么创建.babelrc文件
在windows环境下做react开发其实是一件非常让人头疼的事,强烈建议使用Mac或者是Linux系统,否则真的是自己挖坑自己跳了. 不过,这里还是给大家说说如何在windows环境下新建一个.ba ...
- while循环小例
# 使用while 循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n <= 10: if n == 7: pass else: print(n) n = n + 1 # ...
- 是否应该提供一个dao.insertIgnoreNull ? (像updateIgnoreNull一样)
是否应该提供一个dao.insertIgnoreNull ? (像updateIgnoreNull一样) 发布于 406天前 作者 SayingCode 153 次浏览 复制 上一个帖子 ...
- Uva 12169 不爽的裁判 模运算
题目链接:https://vjudge.net/contest/156903#problem/B 题意: 有一个递推公式 : a,b都不是已知的,给出了 x1,x3,x5.... 求x2,x4,x6. ...
- css3阴影 box-shadow
语法 box-shadow:X轴偏移量 y轴偏移量 [阴影模糊半径] [阴影扩展半径] [阴影颜色] [投影方式] 参数介绍: 注:inset 可以写在参数的第一个或最后一个,其它位置是无效的. 阴影 ...
- Bug分支
软件开发中,bug就像家常便饭一样.有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除. 当你接到一个修复一 ...
- PHP中的生成XML文件的4种方法分享
生成如下XML串 Xml代码 <?xml version="1.0" encoding="utf-8"?> <article> < ...
- 第28章 LTDC—液晶显示中英文—零死角玩转STM32-F429系列
第28章 LTDC—液晶显示中英文 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- AngularJS 外部文件中的控制器其他实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 使用Git操作码云
一.安装并配置 .安装git 下载地址: 官方网站:https://git-for-windows.github.io/ 国内镜像:https://pan.baidu.com/s/1kU5OCOB#l ...