Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: Change charset and collation of an existing mysql database

Is it possible to change the charset and collation of an existing Mysql database using Rails migrations or other options ?!

What's the best way to initially configure database charset & collation ?!

like image 873
refaelos Avatar asked Nov 20 '11 09:11

refaelos


People also ask

How do I change a charset in MySQL?

You can change the default with an alter table set default charset but that won't change the charset of the existing columns. To change that you need to use a alter table modify column . Changing the charset of a column only means that it will be able to store a wider range of characters.

How do I change a charset in database?

To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace dbname with the database name: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.

What is charset and collation in MySQL?

A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set.


1 Answers

Native query could be executed in rails migration:

def self.up
  execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET charset_here COLLATE collation_here;"
end

And for initial configuration you can predefine charset and collation in your database.yml file, like this:

production:
  adapter: mysql2
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci
like image 198
ksarunas Avatar answered Oct 13 '22 00:10

ksarunas