MySQL数据库中文乱码问题解决
迁移了一套内部系统(confluence)之后,页面打开,发现全是问号乱码了。分析原因:
我是按照如下方式创建数据库的:
mysql> create database confluence character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on conflence.* to 'confluence'@'%' identified by '456456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on confluence.* to 'confluence'@'localhost' identified by '456456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
已经设置好了字符集为utf-8,为什么还会乱码了。
查看了一下数据库的编码格式,如下:
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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
找到问题原因,character_set_server不支持UTF8,修改mysql配置并重新加载。
1./etc/init.d/mysqld stop
2.vi /etc/my.cnf
添加:
character_set_server=utf8
init_connect='SET NAMES utf8'
3. /etc/init.d/mysqld start
查看数据库编码字符集:
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 | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
重新导入之前的数据,界面恢复正常。
提示:
在配置confluence连接mysql数据库的时候切记一定要修改URL:
原始如:
jdbc:mysql://localhost/confluence?sessionVariables=storage_engine%3DInnoDB</property>
有中文,必须让其支持utf8,不然默认是latin1,需要修改称如下所示:
jdbc:mysql://localhost/confluence?autoReconnect=true&useUnicode=true&characterEncoding=utf8&sessionVariables=storage_engine%3DInnoDB</property>
使用Unicode编码,字符编码为UTF-8,自动重连。
如图: