日期:2014-05-16  浏览次数:20729 次

mysql修改编码设置
一、mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
默认就是瑞典latin1,一下是换成我们自己的编码,如utf8:
外部访问数据乱码的问题就出在这个connection连接层上,解决方法是在发送查询前执行一下下面这句:

1. SET NAMES 'utf8';

它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

一般只有在访问之前执行这个代码就解决问题了,下面是创建数据库和数据表的,设置为我们自己的编码格式。
2. 创建数据库
mysql> create database name character set utf8;

3. 创建表
CREATE TABLE `type` (
`id` int(10) unsigned NOT NULL auto_increment,
`flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N',
`flag_type` int(5) NOT NULL default '0',
`type_name` varchar(50) character set utf8 NOT NULL default '',
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;

4. 修改数据库成utf8的.
mysql> alter database name character set utf8;

5. 修改表默认用utf8.
mysql> alter table type character set utf8;

6. 修改字段用utf8
mysql> alter table type modify type_name varchar(50) CHARACTER SET utf8;

二、编码算是MySql最难弄的问题了,研究了一下,总结点结果,部分来自其他人的经验,如有不妥之处,请踊跃叽歪啊。。。

设置步骤:

一、编辑MySql的配置文件
MySql的配置文件Windows下一般在系统目录下或者在MySql的安装目录下名字叫my.ini,可以搜索,Linux下一般是 /etc/my.cnf

--在 [mysqld] 标签下加上三行
default-character-set = utf8
character_set_server = utf8
lower_case_table_names = 1        //表名不区分大小写(此与编码无关)

--在 [mysql]  标签下加上一行
    default-character-set = utf8

--在 [mysql.server]标签下加上一行
    default-character-set = utf8

--在 [mysqld_safe]标签下加上一行
    default-character-set = utf8

--在 [client]标签下加上一行
    default-character-set = utf8


二、重新启动MySql服务
Windows可在服务管理器中操作,也可使用命令行:
net stop mysql 回车
net start mysql 回车
服务名可能不一定为mysql,请按自己的设置

Linux下面可是用 service mysql restart

如果出现启动失败,请检查配置文件有没有设置错误





三、查看设置结果
登录MySql命令行客户端:打开命令行
mysql –uroot –p 回车
输入密码
进入mysql后 执行 :show variables like "%char%";
显示结果应该类似如下:

| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |

如果仍有编码不是utf8的,请检查配置文件,也可使用mysql命令设置:
        set character_set_client = utf8;
        set character_set_server = utf8;
        set character_set_connection = utf8;
        set character_set_database = utf8;
        set character_set_results = utf8;
        set collation_connection = utf8_general