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

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 ?!

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

Related

How to get default collation in rails?

In database.yml i have this configuration:
development:
adapter: mysql2
encoding: utf8
...
But ActiveRecord::Base.connection.collation returns latin1_swedish_ci this is not what i expected, because default collation is utf8_unicode_ci. How can i get default collation in rails?
As per my knowledge you can check it on two places, remember you have also to check your mysql database. For ruby on rails you can check configuration and add as per following at end of your database.yml file
encoding: utf8mb4
collation: utf8mb4_unicode_ci
Restart server and now it should be following this configuration. Than check with following command
ActiveRecord::Base.connection.collation
Now let us check on mysql
mysql> show variables like 'collation%';
Right now I can't check these things as mysql is not setup, so will edit later to confirm you all.

Can't change character-set-client & character-set-connection from utf8mb4 to utf8 using JDBC for mysql 5.6

I've this connection string & it was working fine with mysql Connector/J 5.1.40 and mysql 5.5 on windows:
jdbc:mysql://localhost:3306/db?noAccessToProcedureBodies=true&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&connectionCollation=UTF8_PERSIAN_CI
but when I upgraded to mysql 5.6, It seems that JDBC driver automatically changes connection encoding from utf8 to utf8mb4 according to the mysql change history :
Connector/J now auto-detects servers configured with character_set_server=utf8mb4 or treats the Java encoding utf-8 passed using characterEncoding=... as utf8mb4 in the SET NAMES= calls it makes when establishing the connection. (Bug #54175)
I don't know why it should treat utf8 as utf8mb4, but I even can't change it manually after connection established:
Statement stmt = (Statement) conn.createStatement();
stmt.executeQuery("SET NAMES UTF8 COLLATE UTF8_PERSIAN_CI");
stmt.close();
which has no effect on connection state parameters & I've still following situation:
character-set-client=utf8mb4
character_set_connection=utf8mb4
character_set_database=utf8
character_set_filesystem=binary
character_set_results=utf8
character_set_server=utf8
character_set_system=utf8
This is part of mysql config file:
[client]
default-character-set = utf8
[mysql]
default-character-set = utf8
[mysqld]
character-set-server=utf8
collation-server = utf8_persian_ci
I really need to change connection encodig to utf8 instead of utf8mb4 because it made some problems in my application.
Any workaround would be appreciated.

Why is my table's encoding wrong?

I'm using Rails 5 and MySQL 5.7. I have the following in my database.yml:
default: &default
adapter: mysql2
pool: 5
encoding: utf8mb4
charset: utf8mb4
collation: utf8mb4_unicode_ci
socket: /tmp/mysql.sock
development:
<<: *default
database: [...]
username: [...]
password: [...]
I run rake db:reset to recreate my database and I run show create table users and I see CREATE TABLEusers([...]) ENGINE=InnoDB DEFAULT CHARSET=utf8.
Why does my CHARSET=utf8 and how can I fix it (make it equal utf8mb4)? Thanks.
rake db:reset is wrong. rake db:drop && rake db:create && rake db:migrate is correct.
The reset just created the tables from the already stored schema which had the wrong charset and collation. But re-creating them from the migrations created them with the new charset and collation.
Be explicit when you create a table:
CREATE TABLE users (
name VARCHAR(99) NOT NULL,
...
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4;
Another technique is to ignore the table default, and be explicit on each column:
CREATE TABLE users (
name VARCHAR(99) CHARACTER SET utf8mb4 NOT NULL,
...
) ENGINE=InnoDB;
Probably what happened is that you specified the charset in neither place, but the DEFAULT on the database was utf8.
The problem is db:reset calls db:schema:load. If your db/schema.rb file has bad encoding values, it will load up the bad stuff. You can fix your db/schema.rb to have correct encoding before running db:reset

Rails fails to connect to mysql server if I add some default character and collate settings for mysqld

I habe install rails and everything else needs onto a new CentOS 7 server. I have create mysql database via command line and then I've edited it to use character set utf8 and collation utf8_general_ci.
Rails worked.
Then I configured /etc/my.cnf to include some default settings for charset and collation:
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
init_connect=‘SET collation_connection = utf8_general_ci’
[client]
default-character-set = utf8
Rails stopped working saying it can't connect to the mysql.
Obviously, I could reconfigure something in Rails to solve this, but I don't know what.
Thanks for the help.
In my.cnf, use only apostrophe ' or double quote ", not ‘ and ’.

grails table collation latin_swedish_ci although db collation is utf8_general_ci

I created a database for my grails project with default collation utf8_general_ci but now I found out that grails created all tables with latin_swedish_ci.
Why is that and how can I force grails to use utf8?
In other topics I found that I need to add some parameters at the db connection string but I already did that. This is my datasource:
dataSource {
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = ""
url = "jdbc:mysql://localhost:3306/xxx?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8"
}
My datasource looks exactly the same and my table's collations are set to 'utf8_general_ci' . My impression is that this needs to be configured in the database server itself, either generally or per database.
The forme would involve adding the following to your my.cnf file:
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
See https://dev.mysql.com/doc/refman/5.6/en/charset-applications.html for more info.
I usually do the latter one though. I use the following syntax to create my databases:
DROP DATABASE IF EXISTS $DB_NAME;
CREATE DATABASE $DB_NAME DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;