应开发同事的要求,部署了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>