# Database.yml and encoding: utf8 -\> mysql error

**URL:** <https://www.ruby-forum.com/t/database-yml-and-encoding-utf8-mysql-error/61034>\
**Category:** Rails\
**Created:** [May 19, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/database-yml-and-encoding-utf8-mysql-error/61034 "2006-05-19T15:05:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [May 19, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/database-yml-and-encoding-utf8-mysql-error/61034/1 "2006-05-19T15:05:52Z")

</div>

I have Rails 1.1, MySql 5.0 and would like to stor text as utf8 in the  
db.

I have changed the character set and collation in the db to utf8 and  
utf8\_danish\_ci - also checked the mysql to if its ok:

show collations like “%danish%”;  
utf8\_danish\_ci utf8 203 Yes 8

I then add the following  
environment.rb:  
$KCODE = ‘u’  
require ‘jcode’

och database.yml  
encoding: utf8

If I then restart the webserver I get:  
Character set ‘utf8’ is not a compiled character set and is not  
specified in the ‘/usr/local/mysql/share/mysql/charsets/Index’ file  
rake aborted!  
Can’t initialize character set utf8 (path:  
/usr/local/mysql/share/mysql/charsets/)

First thought that I was missing the charset but it was ok.

Then I removed encoding: utf8 and added the following to my application  
controller:

before\_filter :configure\_charsets  
def configure\_charsets  
headers[“Content-Type”] = “text/html; charset=utf-8”  
ActiveRecord::Base.connection.execute ‘SET NAMES UTF8’  
end

This then works fine. Has anyone else encountered the same or have a  
solution?

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [July 28, 2006, 2:36am UTC](https://www.ruby-forum.com/t/database-yml-and-encoding-utf8-mysql-error/61034/2 "2006-07-28T02:36:09Z")

</div>

Bam! I’ve been looking for a solution and this seems to have fixed  
everything. By default MySQL 4.1 was setup to use some weird latin  
encoding.  
I switched all the tables manually to utf-8 bin…Even after that on my  
main  
site I was getting funny characters displayed for quotes and things of  
that  
nature. This fixed it all.

The weird thing is, on the back-end admin site where I’m using forms and  
such the data was displaying fine. I’m wondering if by default Rails  
sets  
this charset and content type when using the form helpers???

On 5/19/06, Mattias B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> before\_filter :configure\_charsets  
> def configure\_charsets  
> headers[“Content-Type”] = “text/html; charset=utf-8”  
> ActiveRecord::Base.connection.execute ‘SET NAMES UTF8’  
> end
> 
> This then works fine. Has anyone else encountered the same or have a  
> solution?
> 
> –  
> seth at subimage interactive  
> [http://www.subimage.com/sublog/](http://www.subimage.com/sublog/)
