修改及查看mysql数据库的字符集
操作系统:Windows Server 2008 R2
Mysql版本:mysql-5.6.29-winx64
事由:MySQL5.6的修改字符集编码为UTF8(解决中文乱码问题)
一、登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
+
--------------------------+----------------------------+
| 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/ |
+
--------------------------+----------------------------+
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
|
+
--------------------------+----------------------------+
| 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:
注意:我们这里设置的sql_mode都是session级别的。另外,可以直接修改my.ini文件,找到sql_mode,然后设置新的模式即可!
例如:
vi /etc/my.cnf
在[mysqld]下面添加如下列:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES