MySQL資料庫中文亂碼問題解決

來源:互聯網
上載者:User

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&amp;useUnicode=true&amp;characterEncoding=utf8&amp;sessionVariables=storage_engine%3DInnoDB</property>

使用Unicode編碼,字元編碼為UTF-8,自動重連。

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.