dpan.orz
By:
dpan.orz

rails with mysql utf8 error

July 5, 2014 1.8k views

Hi all

I am trying to deploy a ruby on rails application with mysql2 on this droplet, when I post some data into the mysql db, it shows the following error message, the database seems can't post utf8 data, anyone know what's wrong??

INFO -- : Started POST "/groups" for 61.64.150.230 at 2014-07-04 22:37:17 +0800 
I, [2014-07-04T22:37:17.803169 #28481] INFO -- : Processing by GroupsController#create as HTML 
I, [2014-07-04T22:37:17.803263 #28481] INFO -- : Parameters: {"utf8"=>"✓", "authenticity_token"=>"MaAEh3zwRVZcsqZn2OoUMtijmrgSRj/i5lYgiJj8ioM=", "group"=>{"title"=>"中文", "descr 
iption"=>""}, "commit"=>"Create Group"} 
E, [2014-07-04T22:37:17.808026 #28481] ERROR -- : Mysql2::Error: Incorrect string value: '\xE4\xB8\xAD\xE6\x96\x87' for column 'title' at row 1: INSERT INTO `groups` (`created_at`, 
`description`, `title`, `updated_at`, `user_id`) VALUES ('2014-07-04 14:37:17', '', '中文', '2014-07-04 14:37:17', 1) 
I, [2014-07-04T22:37:17.808531 #28481] INFO -- : Completed 500 Internal Server Error in 5ms 
F, [2014-07-04T22:37:17.820777 #28481] FATAL -- :
2 Answers

CHECK MYSQL CONF SERVER. USING PHPMYADMIN OR WEBMIN. ETC.

It seems like you are having issue with the character encoding. On the server, try changing your MySql configuration with:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
Have another answer? Share your knowledge.