注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

中华神鸟

QQ:935658701

 
 
 

日志

 
 

MySQL乱码问题已经如何修改MySQL默认编码  

2013-05-08 22:36:22|  分类: mysql |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1.查看当前MySQL的编码:

mysql> show variables like 'collation_%';

+----------------------+-----------------+

| Variable_name         | Value            |

+----------------------+-----------------+

| collation_connection | utf8_general_ci |

| collation_database    | utf8_general_ci |

| collation_server      | utf8_general_ci |

+----------------------+-----------------+

3 rows in set (0.02 sec)

mysql> show variables like 'character_set_%';

+--------------------------+----------------------------+

| 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/ |

+--------------------------+----------------------------+

8 rows in set (0.02 sec)

mysql>

如果当前字符集和我们项目的字符集不一致时可能会出现乱码问题。下面我们就来修改MySQL的编码。

2.修改编码:

(1)修改数据库的字符集

mysql>use mydb

mysql>alter database mydb character set utf-8;

创建数据库指定数据库的字符集

mysql>create database mydb character set utf-8;

 

(2)通过配置文件修改(蓝色部分为Linux系统内容,如果使用Windows可直接跳过):

修改/var/lib/mysql/mydb/db.opt

default-character-set=latin1

default-collation=latin1_swedish_ci

default-character-set=utf8

default-collation=utf8_general_ci

重起MySQL:

[root@bogon ~]# /etc/rc.d/init.d/mysql restart

 

(3)通过MySQL命令行修改(改为需要的编码,我的项目使用的是utf-8,故做如下修改,已经是目标编码的项不用进行修改):

mysql> set character_set_client=utf8;

Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_connection=utf8;

Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_database=utf8;

Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_results=utf8;

Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_server=utf8;

Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_system=utf8;

Query OK, 0 rows affected (0.01 sec)

mysql> set collation_connection=utf8;

Query OK, 0 rows affected (0.01 sec)

mysql> set collation_database=utf8;

Query OK, 0 rows affected (0.01 sec)

mysql> set collation_server=utf8;

Query OK, 0 rows affected (0.01 sec)

3.修改完毕,再次查看编码如下:

mysql> show variables like 'character_set_%';

+--------------------------+----------------------------+

| 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/ |

+--------------------------+----------------------------+

8 rows in set (0.03 sec)

mysql> show variables like 'collation_%';

+----------------------+-----------------+

| Variable_name         | Value            |

+----------------------+-----------------+

| collation_connection | utf8_general_ci |

| collation_database    | utf8_general_ci |

| collation_server      | utf8_general_ci |

+----------------------+-----------------+

3 rows in set (0.04 sec)


至此,乱码问题应该已经得到解决!

  评论这张
 
阅读(122)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017