ksaitoの日記

日々試したことの覚え書き

Redmineの文字化けは、MySQLの設定でした。

移転しました。

自動的にリダイレクトします。

Ubuntu 10.04のredmineパッケージとredmine-myslパッケージをインストールしましたが、トラッカや入力した文字が全て??と表示されていました。
原因は、MySQLエンコードでした。
Ubuntu 10.04でRedmineを使うredmineのApache組み込(Ubuntuの場合)で設定したRedmineがようやく使えるようになりました。

トラッカーは最初から??

新規プロジェクトに日本語名を付けても??

文字化けは、どこで

データは、データベースに既に??で格納されていました。

$ mysql -u redmine -p redmine_default
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 43
Server version: 5.1.41-3ubuntu12.3 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select name from trackers;
+------+
| name |
+------+
| ??   |
| ??   |
| ???? |
+------+
3 rows in set (0.00 sec)

mysql> select name from projects;
+------+
| name |
+------+
| ???  |
+------+
1 row in set (0.00 sec)

mysql> 

原因は、MySQLエンコード

MySQLエンコードを確認してみるとlatin1となっています。

mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> 

my.confにdefault-character-setをmysqld, mysqldump, mysqlの3箇所に追加してmysqldを再起動します。
(おっと、mysqlは、upstartに対応しているのですね。)

$ cd /etc/mysql
$ sudo bzr diff my.cnf 
=== modified file 'mysql/my.cnf'
--- mysql/my.cnf	2010-05-22 09:33:45 +0000
+++ mysql/my.cnf	2010-08-03 13:46:08 +0000
@@ -29,6 +29,7 @@
 
 [mysqld]
+default-character-set=utf8
 #
@@ -113,11 +114,13 @@
 
 [mysqldump]
+default-character-set=utf8
 quick
 quote-names
 max_allowed_packet	= 16M
 
 [mysql]
+default-character-set=utf8

$ sudo service mysql restart
mysql start/running, process 1473
$

これで、次から作るデータベースは、utf8になります。

$ mysqladmin -u root -p drop sampledb
Enter password: 
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'sampledb' database [y/N] y
Database "sampledb" dropped
$ mysqladmin -u root -p create sampledb
Enter password: 
$ mysql -u root -p sampledb
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.1.41-3ubuntu12.3 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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)

mysql> 

redmineのデータベース再作成

Apache2を停止して、redmine_defaultデータベースを削除、再作成をします。

$ sudo /etc/init.d/apache2 stop
 * Stopping web server apache2
 ... waiting .   ...done.
$ mysqladmin -u redmine -p drop redmine_default
Enter password: 
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'redmine_default' database [y/N] y
Database "redmine_default" dropped

$ sudo dpkg-reconfigure redmine
dbconfig-common: writing config to /etc/dbconfig-common/redmine/instances/default.conf

Creating config file /etc/redmine/default/database.yml.new with new version
granting access to database redmine_default for redmine@localhost: already exists.
creating database redmine_default: success.
verifying database redmine_default exists: success.
dbconfig-common: flushing administrative password
Populating database for redmine instance "default".
This may take a while.
Done.
Default configuration data loaded.
Migrating engines...
Migrating acts_as_activity_provider...
Migrating acts_as_attachable...
Migrating acts_as_customizable...
Migrating acts_as_event...
Migrating acts_as_list...
Migrating acts_as_searchable...
Migrating acts_as_tree...
Migrating acts_as_versioned...
Migrating acts_as_watchable...
Migrating awesome_nested_set...
Migrating classic_pagination...
Migrating coderay-0.7.6.227...
Migrating gravatar...
Migrating open_id_authentication...
Migrating rfpdf...
Migrating ruby-net-ldap-0.0.4...
$ sudo service apache2 start
 * Starting web server apache2
   ...done.
$ 

これで、トラッカーも日本語入力も問題なくなりました。