今天发现我mysql插入中文时老出现????号了,但保存英文是没有问题的,下面我来给各位同学介绍一下linux下mysql中文乱码解决方法。
linux 下则需要修改/etc/my.cnf,在[mysqld]下加入default-character-set = utf8,[client]下加入default-character-set = utf8,在[mysql]字段里加入default-character-set=utf8

实现:
1.查看原编码

 代码如下 复制代码
mysql> show variables like 'character%'; //执行编码显示,可以看到默认是latin1
+--------------------------+----------------------------+ 
| 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/ | 
+--------------------------+----------------------------+  

2.修改/etc/my.cnf,分别在[client],[mysqld],[mysql]字段里添加default-character-set=utf8。注意[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。

# vim /etc/my.cnf  //添加default-character-set=utf8

 代码如下 复制代码

[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8

[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8

[mysql]
no-auto-rehash

default-character-set=utf8修改完成后,service mysql restart重启mysql服务器,使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8

 代码如下 复制代码
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/local/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+ 

3.如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上。解决方法是在发送查询前执行一下下面这句(直接写在SQL文件的最前面):

 代码如下 复制代码

SET NAMES ‘utf8′;

它相当于以下三指令:

 代码如下 复制代码
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

附录:
朋友的一个站点转到我这边。导入mysql时,mysql还是latin的编码,因此,虽然按上面的步骤,在/etc/my.cnf文件中[client],[mysqld],[mysql]字段里添加default-character-set=utf8,但站点部分中文是乱码。在将mysql编辑改为utf8再重新导入数据库后,就正常了。
估计是mysql是latin编码是,导入数据时,因为mysql无法识别,直接将数据保存成了???这种乱码的形式了

相关内容


linux下mysql中文乱码(中文问号)解决办法

www.111cn.net 编辑:ever 来源:转载
今天发现我mysql插入中文时老出现????号了,但保存英文是没有问题的,下面我来给各位同学介绍一下linux下mysql中文乱码解决方法。
width="300" height="250" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="left: 0px; position: absolute; top: 0px;">
id="cproIframe_u1339684_1" width="300" height="250" src="http://pos.baidu.com/acom?adn=3&at=134&aurl=&cad=1&ccd=24&cec=GBK&cfv=18&ch=0&col=zh-CN&conOP=0&cpa=1&dai=1&dis=0&layout_filter=rank%2Ctabcloud&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DjEFVyS6UIRPO8Ri5ATgS-HaPYn_gBgPb5nvcvmCWUaaSxY02pVJFGVrrSm7Et0V-4y6saBOj0iXLLRk8NaMPgq%26wd%3D%26eqid%3Da66eb1c90003eed400000003565579a3&ltu=http%3A%2F%2Fwww.111cn.net%2Fsys%2Flinux%2F53209.htm&lunum=6&n=mailangel123_cpr&pcs=1920x955&pis=10000x10000&ps=434x811&psr=1920x1080&pss=1920x725&qn=c993fbe993228559&rad=&rsi0=300&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=1339684&tn=text_default_300_250&tpr=1448442561291&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1339684&ti=linux%E4%B8%8Bmysql%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%EF%BC%88%E4%B8%AD%E6%96%87%E9%97%AE%E5%8F%B7%EF%BC%89%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95-linux-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F-%E5%A3%B9%E8%81%9A%E6%95%99%E7%A8%8B%E7%BD%91&tt=1448442561260.34.87.90" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true">

linux下则需要修改/etc/my.cnf,在[mysqld]下加入default-character-set = utf8,[client]下加入default-character-set = utf8,在[mysql]字段里加入default-character-set=utf8

实现:
1.查看原编码

 代码如下 复制代码
mysql> show variables like 'character%'; //执行编码显示,可以看到默认是latin1
+--------------------------+----------------------------+ 
| 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/ | 
+--------------------------+----------------------------+  

2.修改/etc/my.cnf,分别在[client],[mysqld],[mysql]字段里添加default-character-set=utf8。注意[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。

# vim /etc/my.cnf  //添加default-character-set=utf8

 代码如下 复制代码

[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8

[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8

[mysql]
no-auto-rehash

default-character-set=utf8修改完成后,service mysql restart重启mysql服务器,使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8

 代码如下 复制代码
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/local/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+ 

3.如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上。解决方法是在发送查询前执行一下下面这句(直接写在SQL文件的最前面):

 代码如下 复制代码

SET NAMES ‘utf8′;

它相当于以下三指令:

 代码如下 复制代码
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

附录:
朋友的一个站点转到我这边。导入mysql时,mysql还是latin的编码,因此,虽然按上面的步骤,在/etc/my.cnf文件中[client],[mysqld],[mysql]字段里添加default-character-set=utf8,但站点部分中文是乱码。在将mysql编辑改为utf8再重新导入数据库后,就正常了。
估计是mysql是latin编码是,导入数据时,因为mysql无法识别,直接将数据保存成了???这种乱码的形式了

相关内容

Logo

更多推荐