應開發同事的要求,部署了Gitlab+Gerrit+Jenkins的持續集成環境.mysql
可是發現了一個問題,Gerrit登錄後有中文亂碼出現.git
具體狀況以下:sql
(1)Git代碼中的中文亂碼處理:數據庫
爲妥善解決中文編碼的問題,對全部git repository作以下約定:
全部文本文件都必須存儲成utf8編碼
全局配置以下:
git config --global core.quotepath false
git config --global i18n.logoutputencoding utf8
git config --global i18n.commitencoding utf8vim
另外:安裝gerrit的時候對於數據庫(選擇myqsl方式的話)的編碼設定爲utf8服務器
(2)Gerrit登錄後,設置中文用戶名出現亂碼編碼
即在「Full name」一欄中輸入中文名後,刷新一下就會出現「???」的亂碼,以下:spa
後來想到,多是在建立gerrit數據庫的時候沒有自定義編碼爲utf83d
登錄數據庫,查看編碼:rest
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>