[原創]Gerrit中文亂碼問題解決方案分享

散盡浮華發表於2016-07-15

 

應開發同事的要求,部署了Gitlab+Gerrit+Jenkins的持續整合環境.

但是發現了一個問題,Gerrit登陸後有中文亂碼出現.

具體情況如下:

(1)Git程式碼中的中文亂碼處理:

為妥善解決中文編碼的問題,對所有git repository做如下約定:
所有文字檔案都必須儲存成utf8編碼
全域性配置如下:
git config --global core.quotepath false
git config --global i18n.logoutputencoding utf8
git config --global i18n.commitencoding utf8

另外:安裝gerrit的時候對於資料庫(選擇myqsl方式的話)的編碼設定為utf8

(2)Gerrit登陸後,設定中文使用者名稱出現亂碼

即在“Full name”一欄中輸入中文名後,重新整理一下就會出現“???”的亂碼,如下:

 

後來想到,可能是在建立gerrit資料庫的時候沒有自定義編碼為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    | latin1 |
| character_set_system       | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

發現直接查詢目標表,不支援中文:

mysql> use gerritdb;
Database changed
mysql> select full_name,account_id from accounts;
+-----------+------------+
| full_name | account_id |
+-----------+------------+
| daizizhe | 5 |
| gerrit | 1 |
| jenkins | 3 |
| wangshibo | 2 |
+-----------+------------+
4 rows in set (0.00 sec)

mysql> update accounts set full_name = "王士博" where account_id = 2;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> select full_name,account_id from accounts;
+-----------+------------+
| full_name | account_id |
+-----------+------------+
| ???           | 2 |
| daizizhe     | 5 |
| gerrit        | 1 |
| jenkins      | 3 |
+-----------+------------+
4 rows in set (0.00 sec) 

解決辦法:

**********************************************

首先保證伺服器的系統編碼是支援中文的

[root@115 ~]# cat /etc/sysconfig/i18n
LANG="zh_CN.UTF-8"

**********************************************

修改mysql資料庫編碼為utf8,支援中文

1)臨時修改character_set_server的編碼

mysql> character_set_server='utf8'; 

2)永久性生效

修改mysql服務的配置檔案/etc/my.cnf

[root@115 ~]# vim /etc/my.cnf

.......

[mysqld]

......

character_set_server = utf8            【新增這一行內容】

然後重啟mysql服務

[root@115 ~]# /etc/init.d/mysqld restart
Shutting down MySQL. [確定]
Starting MySQL.. [確定]

**********************************************

已經修改了mysql的編碼,保證各個編碼引數均為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/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

但是發現Gerrit裡面的“Full name”設定成中文名還是亂碼

最後發現這是因為“Full name”欄位所在的表結構了就已經寫死了編碼為latin1 

先mysqldump到處gerritdb資料庫

[root@115 ~]#mysqldump gerritdb -p > gerritdb.sql

然後檢視gerritdb.sql備份檔案

發現gerritdb庫裡所有表的編碼都是latin1啊!!!!

[root@115 ~]#vim gerritdb.sql

補救措施:

現將gerritdb.sql備份檔案裡的latin1_bin替換成utf8_general_ci

再將gerritdb.sql備份檔案裡的latin1替換成utf8

vim替換如下:

:%s/latin1_bin/utf8_general_ci/g
:%s/latin1/utf8/g

接著刪除gerritdb資料庫

再建立空的gerritdb庫,建立的時候指定編碼為utf8

mysql>drop database gerritdb;

mysql>create database gerritdb CHARACTER SET utf8 COLLATE utf8_general_ci;

最後再將修改好的gerritdb.sql備份檔案(已經修改編碼為utf8了)匯入到gerritdb空庫呢

[root@115 ~]#mysql  gerritdb -p < gerritdb.sql

檢查下,發現可以支援中文了

mysql> use gerritdb;
Database changed
mysql> select full_name,account_id from accounts;
+-----------+------------+
| full_name | account_id |
+-----------+------------+
| daizizhe | 5 |
| gerrit | 1 |
| jenkins | 3 |
| wangshibo | 2 |
+-----------+------------+
4 rows in set (0.00 sec)

mysql> update accounts set full_name = "王士博" where account_id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select full_name,account_id from accounts;
+-----------+------------+
| full_name | account_id |
+-----------+------------+
| daizizhe       | 5 |
| gerrit          | 1 |
| jenkins        | 3 |
| 王士博          | 2 |
+-----------+------------+
4 rows in set (0.00 sec)

mysql>

相關文章