修改及查看mysql数据库的字符集
操作系统:Windows Server 2008 R2
Mysql版本:mysql-5.6.29-winx64
事由:MySQL5.6的修改字符集编码为UTF8(解决中文乱码问题)
一、登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下:
?
123456789101112
|
+ | Variable_name | Value | + | 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/ | + |
character_set_database和character_set_server的默认字符集还是latin1。 二、最简单的完美修改方法,修改mysql的my.cnf文件中的字符集键值(注意配置的字段细节): ?
12345678910111213141516171819
|
1、在[client]字段里加入 default - character - set =utf8,如下: [client] port = 3306 socket = /var/lib/mysql/mysql.sock default - character - set =utf8 2、在[mysqld]字段里加入 character - set -server=utf8,如下: [mysqld] port = 3306 socket = /var/lib/mysql/mysql.sock character - set -server=utf8 3、在[mysql]字段里加入 default - character - set =utf8,如下: [mysql] no -auto-rehash default - character - set =utf8 |
修改完成后,service mysql restart重启mysql服务就生效。注意:[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。
使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8。 ?
123456789101112
|
+ | 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/ | + |
附My.ini 配置文件内容:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[WinMySQLAdmin]
#指定mysql服务启动的启动文件
Server=D:\MySQL\Mysql-Windows\mysql-5.6.29-winx64\bin\mysql.exe
[mysql]
no-auto-rehash
default-character-set=utf8
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = D:\MySQL\Mysql-Windows\mysql-5.6.29-winx64
datadir = D:\MySQL\Mysql-Windows\mysql-5.6.29-winx64\data
port = 3306
server_id = 1
# default-character-set=gbk
#default-character-set=utf8
character-set-server=utf8
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
[client]
#设置mysql客户端的字符集
#default-character-set=gbk
default-character-set=utf8
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
//-----------------------------------//
mysql的sql_mode 模式
https://dev.mysql.com/doc/refman/5.6/en/sql-mode.html
1. sql_mode模式
mysql数据库的中有一个环境变量sql_mode,定义了mysql应该支持的sql语法,数据校验等!我们可以通过以下方式查看当前数据库使用的sql_mode:
[java] view plain copy
- mysql> select @@sql_mode;
- +----------------------------------------------------------------+
- | @@sql_mode |
- +----------------------------------------------------------------+
- | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
- +----------------------------------------------------------------+
或者通过查看系统变量方式:
[java] view plain copy
- mysql> show variables like 'sql_mode%'\G;
- *************************** 1. row ***************************
- Variable_name: sql_mode
- Value: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
mysql5.0以上版本支持三种sql_mode模式:ANSI、TRADITIONAL和STRICT_TRANS_TABLES。
ANSI模式:宽松模式,对插入数据进行校验,如果不符合定义类型或长度,对数据类型调整或截断保存,报warning警告。
TRADITIONAL模式:严格模式,当向mysql数据库插入数据时,进行数据的严格校验,保证错误数据不能插入,报error错误。用于事物时,会进行事物的回滚。
STRICT_TRANS_TABLES模式:严格模式,进行数据的严格校验,错误数据不能插入,报error错误。
1.2 ANSI模式
[java] view plain copy
- mysql> set @@sql_mode=ANSI;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table test(name varchar(4), pass varchar(4));
- Query OK, 0 rows affected (0.03 sec)
-
- mysql> insert into test values('aaaaa','aaaaa'),('bbbb','bbbb');
- Query OK, 2 rows affected, 2 warnings (0.02 sec)
- Records: 2 Duplicates: 0 Warnings: 2
-
- mysql> show warnings;
- +---------+------+-------------------------------------------+
- | Level | Code | Message |
- +---------+------+-------------------------------------------+
- | Warning | 1265 | Data truncated for column 'name' at row 1 |
- | Warning | 1265 | Data truncated for column 'pass' at row 1 |
- +---------+------+-------------------------------------------+
- 2 rows in set (0.00 sec)
-
- mysql> select * from test;
- +------+------+
- | name | pass |
- +------+------+
- | aaaa | aaaa |
- | bbbb | bbbb |
- +------+------+
- 2 rows in set (0.00 sec)
我们可以看到,在ANSI模式下,当我们插入数据时,未满足列长度要求时,数据同样会插入成功,但是对超出列长度的字段进行截断,同时报告warning警告。
1.3 STRICT_TRANS_TABLES模式
[java] view plain copy
- mysql> set @@sql_mode=STRICT_TRANS_TABLES;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table test(name varchar(4), pass varchar(4));
- Query OK, 0 rows affected (0.02 sec)
-
- mysql> insert into test values('aaaaa','aaaaa'),('bbbb','bbbb');
- ERROR 1406 (22001): Data too long for column 'name' at row 1
-
- mysql> show errors;
- +-------+------+------------------------------------------+
- | Level | Code | Message |
- +-------+------+------------------------------------------+
- | Error | 1406 | Data too long for column 'name' at row 1 |
- +-------+------+------------------------------------------+
- 1 row in set (0.00 sec)
-
- mysql> select * from test;
- Empty set (0.00 sec)
我们可以看到,在STRICT_TRANS_TABLES模式下,当我们插入数据时,mysql会严格的进行数据的校验,当发现插入列值未满足要求,直接报告error错误,保证了错误数据无法插入到数据库中。
1.3 TRADITIONAL模式
[java] view plain copy
- mysql> set @@sql_mode=TRADITIONAL;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table test(name varchar(4), pass varchar(4));
- Query OK, 0 rows affected (0.02 sec)
-
- mysql> insert into test values('aaaaa','aaaaa'),('bbbb','bbbb');
- ERROR 1406 (22001): Data too long for column 'name' at row 1
-
- mysql> show errors;
- +-------+------+------------------------------------------+
- | Level | Code | Message |
- +-------+------+------------------------------------------+
- | Error | 1406 | Data too long for column 'name' at row 1 |
- +-------+------+------------------------------------------+
- 1 row in set (0.00 sec)
-
- mysql> select * from test;
- Empty set (0.00 sec)
TRADITIONAL模式与STRICT_TRANS_TABLES模式执行的结果,在这种情况下一致。
[java] view plain copy
- mysql> select @@sql_mode\G;
- *************************** 1. row ***************************
- @@sql_mode: STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,E
- RROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER
- 1 row in set (0.00 sec)
看一下TRADITIONAL模式,我们发现在TRADITIONAL模式下,对所有的事务存储引擎,非事务存储引擎检查,日期类型中的月和日部分不能包含0,不能有0这样的日期(0000-00-00),数据不能除0,禁止grant自动创建新用户等一些校验。
注意:我们这里设置的sql_mode都是session级别的。另外,可以直接修改my.ini文件,找到sql_mode,然后设置新的模式即可!
例如:
vi /etc/my.cnf
在[mysqld]下面添加如下列:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES