红联Linux门户
Linux帮助

ubuntu mysql中中文乱码问题

发布时间:2015-12-04 09:47:27来源:linux网站作者:指尖de烟火

建表的时候utf8为缺省编码:
DROP TABLE IF EXISTS `companies`;
CREATE TABLE `companies` (
`id` int(8) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`address` varchar(255) default NULL,
`memo` text,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1DEFAULT CHARSET=utf8;


通过命令对数据操作都不会出现乱码问题。但是一使用java对其进行操作后,所有中文都变为了乱码。网上查了点资料。改了一下

mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |

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


原来是存在latin1的字符编码

SET character_set_client =utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

可输入mysql>SHOW VARIABLES LIKE '%character%' ;命令查看效果。

都变为utf8了。但是运行java 还出现乱码。重新终端mysql.

SHOW VARIABLES LIKE '%character%' ;

mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+
没改变。只能修改myconf。

sudo gedit /etc/mysql/my.cnf

[mysql]

default-character-set=utf8

[mysqld]
default-character-set=utf8

重启 sudo /etc/init.d/mysql restart

sudo /etc/init.d/mysql restart
* Stopping MySQL database server mysqld                                 [ OK ]
* Starting MySQL database server mysqld                                [fail]
不行。继续……

……

……

原来5.5以前和5.5以后的系统是有区别的。

sudo gedit /etc/mysql/my.cnf 。(5.5以前系统)在【client】下面加入 default-character-set=utf8

在【mysqld】下面加入default-character-set=utf8

Notice:注意 如果修改后不能启动报错试试把default-character-set=utf8改为character_set_server=utf8,仅仅加入到mysqld下面的.client就不需要加了

sudo gedit /etc/mysql/my.cnf 。(5.5以后系统)如下修改:

[client]
default-character-set=utf8

[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci

然后重启sudo /etc/init.d/mysql restart

administrator@fly:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.35-0ubuntu0.13.10.2 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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.00 sec)

mysql>

+++++++++++++++++++++++++问题解决++++++++++++++++++++++++


总结:
1、在将表的时候最好设置默认编码。养成习惯

2、数据库的编码,在安装完mysql的时候就先改正。
3、java JDBC 操作时,可能会出现乱码,但是还是得从根源上更正问题。


解决MySQL中文乱码的问题:http://www.linuxdiyf.com/linux/12305.html

Ubuntu 14.10下编译安装MySQL 5.6.23:http://www.linuxdiyf.com/linux/12221.html

MySQL设置数据库的默认编码:http://www.linuxdiyf.com/linux/11889.html

MySQL字符集GBK转换到UTF8:http://www.linuxdiyf.com/linux/11500.html

Java写入MySQL中文乱码解决方法:http://www.linuxdiyf.com/linux/9593.html