1、以root管理员身份查看数据编码格式。
登陆命令:>mysql -u root –p 输入密码后如下命令查看数据编码格式:
查询结果如下:
show variables like ‘character%‘; |
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
通过查询结果显示可以看出
数据库编码为拉丁语,需要修改为gbk或utf8.
character_set_client为客户端编码方式;
character_set_connection为建立连接使用的编码;
character_set_database数据库的编码;
character_set_results结果集的编码;
character_set_server数据库服务器的编码;
只要保证以上五个采用的编码方式一样,就不会出现乱码问题。
2、修改数据库编码的步骤:
1)停止mysql服务。
2)修改mysql配置文件。
windows:安装路径下的:my.ini
linux:路径一般为:/etc/mysql/my.cnf
3)找到客户端配置[client] 在下面添加
default-character-set=utf8 默认字符集为utf8
4)在找到[mysqld] 添加
default-character-set=utf8 默认字符集为utf8
init_connect=‘SET NAMES utf8‘ (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)
5)修改好后,重新启动mysql查看当前数据编码格式。
>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/share/mysql/charsets/ |
+--------------------------+----------------------------+
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
3、MySQL指定编码的方式是非常灵活并多样化的,可以指定表级别的编码,行级别编码,甚至可以指定字段级别的编码。
以下示例给出创建数据库时指定编码的两种方式:
1)CREATE DATABASE ms_db CHARACTER SET utf8 COLLATE utf8_general_ci;
2)create database if not exists netctoss default character set utf8;
3)如果你采用的是外部接入的方式,可以在连接中确定请求的编码格式如:jdbc:mysql://localhost:3306
/mysql?useUnicode=true&characterEncoding=utf-8(注意:不要出现任何空格,否则出错)
4、若Commandline终端查看数据显示乱码则执行如下命令:
set names gbk(注意,不是UTF-8) 来修改窗口级别的编码格式解决终端乱码问题。
注意:set names
gbk只可以修改character_set_client、character_set_connection、
character_set_results的编码方式,并且这种修改是窗口级别的,只针对当前窗口有效,打开新窗口修改无效,数据库底层的编码方式不会
被改变,插入数据后还是以utf8编码方式存储。