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.
Related
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
I have PHP 5.5.9 and MySQL 5.5.43 Innodb. This is in Kubuntu LTS /etc/mysql/my.cnf
printf("Current character set: %s\n", mysqli_character_set_name($conn));
Current character set: latin1
I've tried
default-character-set = utf8
first but then my mysql server didn't restart
Then I've tried
character-set-server = utf8
then my mysql shell didn't start
How can I rescue this issue? I've managed with
init_connect = 'SET NAMES utf8'
solve the problem for nonroot users, but the charset remains the same (it only displays as nonprivileged users UTF8, it is still Latin1).
And if (a big if) smnd could tell me WHERE should I use utf8mb4? Also in my.cnf file? I had my tables in utf8 and have migrated those with which I am working to utf8mb4 ...
Another Q: IS that applicable for Linux (Kubuntu) server:
https://www.youtube.com/watch?v=3M1Wpw4uOoM
Please help me, thanks, Gregor from lilaum.com
mysqli_set_charset('utf8').
init_connect is ignored for root connections.
utf8mb4 is effectively a superset of utf8. It is needed for Chinese and the new emoji, plus a scattering of obscure character sets.
PHP manual says:
MySQLnd always assumes the server default charset. This charset is sent during connection hand-shake/authentication, which mysqlnd will use.
MySQLnd is the MySQL native driver included with PHP 5.4 and above.
Unfortunately, I have to confirm that even in my system (PHP 5.6.17 with MySQL 5.5.47 on Debian Jessie 8.3) despite having configured /etc/mysql/my.cnf with the following entries
[mysqld]
character-set-server=utf8
[mysql]
default-character-set=utf8
the mysqlnd does not seem to assume the server default charset (utf8) but sets the connection charset to latin1 with collate latin1_swedish (maybe a default value considering MySQL AB was a swedish company).
So, it seems that there's no permanent way to set utf8 charset when connecting from a PHP application. You have to call
$mysqli->set_charset("utf8")
after each connection. This could be embedded in a custom 'dbconnect' PHP function in order to provide a sort of abstraction layer.
I am trying to get Doctrine2 to work properly with a mysql db in Symfony2 on a debian squeeze system.
The charset and collation of the DB are set to utf8 and utf8_general_ci and the db is created manually by reading in an sql script.
Now I enter some values for one of the tables with danish special chars like æøå and they simply wont display correctly.
My parameters.yml has set encoding: utf8 and in my config.yml the doctrine configuration has:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
When I do a select in the mysql console, the characters display correctly and also if I try by replacing the output array in the controller by som hardcoded values, so the problem seems definately to stem from some kind of error/bug in doctrine2 configuration...
Does anybody have any clues on how to solve this?
Thanks a lot.
Ok I finally figured it out... there is some kind of double encoding going on with this configuration. Commenting out charset: UTF8 from doctrine dbal config solves the problem.
the problem might be in your application charset. it can be found in config.yml
framework:
charset: UTF-8
#translator: { fallback: lt }
#secret: %secret%
#other options
In my database config I have:
adapter: mysql
database: xxx
username: xxx
password: xxx
host: localhost
encoding: 'latin1'
My table definition has: DEFAULT CHARSET=latin1
I put this at the top of my file:
# encoding: iso-8859-1
And yet still when I save my record and load it again the latin1 data has been mysteriously converted to utf8. Can someone suggest something I'm missing?
What about the columns' encoding? You have to set the connection, the tables, the columns and the program encoding/charset to the same one to have your code work properly.
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