I develop a Rails app linked with a Mysql db.
In rails console
irb(main):002:0> Rails.application.config.encoding
=> "utf-8"
My database
I use Scalingo for deployment (kind of Heroku), my database.yml looks:
"production"=>{"adapter"=>"mysql", "database"=>"****", "pool"=>5, "timeout"=>10000, "encoding"=>"utf8", "username"=>"****", "password"=>"<password>", "port"=>30324, "host"=>"****"}}
My app and my DB are well configured but on write of name: 'é' on db for example it returns:
=> #<Node id: 21, name: "\xC3\xA9", created_at: "2015-05-18 06:43:29", updated_at: "2015-05-18 06:43:29">
How can be this problem fixed?
SOLUTION
replace gem mysql with mysql2 and also the adapter to mysql2
First you need to set up database.yml by define utf-8 of encoding
development:
adapter: mysql
encoding: utf8
database: my_international_db
username: user
password: password
socket: /var/run/mysqld/mysqld.sock
Now also check your conf file of mysql
in /etc/mysql/my.conf
...
[client]
default-character-set=utf8
...
[mysqld]
default-character-set=utf8
character-set-server=utf8
default-collation=utf8_unicode_ci
...
If the above are there then no need to do anything. If you change anything on it then restart mysql server
sudo /etc/init.d/mysql restart
Please let me know
Mysql is returning you UTF-8 encoded data, but the mysql activerecord adapter is not correctly setting the encoding for the strings. ASCII-8BIT just means a byte strings.
I'm guessing you are using the "mysql" adapter. Try using the more modern "mysql2" adapter. To do this add activerecord-mysql2-adapter to you Gemfile, and change your database.yml file so that the new adapter is used:
"adapter"=>"mysql2"
Related
I am using ruby-2.2.4, Rails 4.2.5 and MySQL 5.7.16 with gem mysql2 in my Ruby on Rails application. I have created database with name 123_4 and set database name in /config/database.yml.
Why I am getting error ActiveRecord::NoDatabaseError: Unknown database '1234' when trying rake db:migrate?
If I try to run rake db:create database with name 1234 will be created.
If I use 123_abc4 for database name everything is fine.
my database.yml content:
production:
adapter: mysql2
database: 123_4
host: localhost
username: user
password: "pass"
encoding: utf8
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
MySQL Schema Object Name
So you can use 123_abc4 because it contains letters.
If it only includes number, you will need to quote it: '123_4'
Im making an application with a pre-existing database, I set up the database.yml to use the database.
database.yml
development:
adapter: mysql2
encoding: utf8
# database: ttlem_demo_development
database: ttle
pool: 5
username: root
password:
socket: /tmp/mysql.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
database: ttlem_demo_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
database: ttlem_demo_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
I only want one table out of the database it is called account views, I try to generate a scaffold for this with all the correct fields but it tells me i need to migrate (when i render it in the browser), if i migrate i wont be able to use the existing data, is this correct? How can i make a model that will use the existing data and fields?
Thank you for all your help :)
Two things to try:...
#1 Run your scaffold without a migration so it doesn't think you're missing one.
rails g scaffold yourmodelname fieldone:fieldtype ... etc --no-migration
#2 If that doesn't work you can go the long way round but dumping and reloading with a valid schema version number
Add this db to yml to your gemfile:
gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'
It works for either rails 3 or rails 4.
Do a schema dump of your current database so you'll get a schema.rb with a valid version number.
bundle exec rake db:schema:dump
Now that you have a valid schema dump your data.
bundle exec rake db:data:dump
Drop your database (you can do it manually using the mysql commands if you prefer or run rake db:drop)
Now recreate it with your schema file.
bundle exec rake db:schema:load
Now add back your data
bundle exec rake db:data:load
Start your server and assuming your correctly matched all your data fields in your model (so the proper strong parameters are set from your scaffold) you should be good.
I'm on a Mac and used brew to install an updated MySQL. In an effort to streamline my local database creation, I took the password off my local root user (I know, I know), and after creating a new Rails app (rails new myapp -d mysql), should just be able to run rake db:create, right?
However, despite having /etc/my.cnf set to define the standard mysql.sock location...
[mysqld]
socket=/usr/local/var/mysql/mysql.sock
[client]
socket=/usr/local/var/mysql/mysql.sock
... Rails still won't use the newly defined mysql.sock file on a rake db:create:
rake db:create
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>false, "database"=>"delayed_jobs_development", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost"}, charset: , collation:
Running mysqladmin variables lists the correct socket file location, as does mysql --help. I've restarted the MySQL server and even recreated the Rails app.
So my question: What else could be defaulting the socket file to /tmp/mysql.sock, or is there an alternate, preferably global, config file where I could specify its location that the rake task will honor? Could the rake task be calling a different commandline MySQL tool that uses a different config file?
Obviously, I could either create a symlink from the /tmp location to my real location, or edit my database.yml file to refer to it. I understand how to get Rails to talk to the DB server correctly, but the point is to have the proper defaults set up once, so any future Rails app I create locally is good to go without extra edits (/tmp/mysql.sock gets cleared on reboot).
In fact, I'm not even sure why it's trying to connect via the socket file at all, since my database.yml file tells it to use the hostname:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: root
password:
host: localhost
As the MySQL documentation says, /tmp/mysql.sock is the default location for the socket file. This is also what rails defaults to (more below).
As you mentioned, you can always set the socket (the one you configured and that is returned by mysqladmin variables) explicitly in your database.yml file:
socket: /usr/local/var/mysql/mysql.sock
I think the connection is made via the socket file as you set host: localhost. If you change that to 127.0.0.1, the connection should by made over TCP/IP.
I found rather old information (a really old version of the mysql adapter and a blog entry from 2007) which implies that the MYSQL_UNIX_ADDR environment variable is used in resolving the socket. Also see this comment on GitHub by the developer of Ruby/MySQL, in which he says to set the MYSQL_UNIX_PORT variable instead. A third env variable is MYSQL_SOCKET mentioned in the source of the Ruby/MySQL connector.
export MYSQL_UNIX_ADDR=/usr/local/var/mysql/mysql.sock
export MYSQL_UNIX_PORT=/usr/local/var/mysql/mysql.sock
export MYSQL_SOCKET=/usr/local/var/mysql/mysql.sock
I do not know if these variables get evaluated somewhere along the way. Still, you could try them out.
Right now, rails mysql_adapter.rb says that the configured socket is used and that it defaults to /tmp/mysql.sock (set in app_generator.rb). No other locations seem to get checked. I did not found anything else in the default Ruby/MySQL adapter used by rails.
So basically – without being an expert on this topic – I don't think there is a way to set a global default location for the MySQL socket file that gets evaluated by rails or the used default adapter.
I am having trouble in rails, I have just installed it but when I update after updating mysql settings and running
rake db:create
and then
rails server
It started server and then when I tried viewing it via browser there errors saying active record connection not established error in strange way. I am new to both ruby and rails so that's why not understanding by debugging info. I assume that there is some thing wrong in MySQL configuration. I am using it on windows and using railsinstaller and using MySQL that came with XAMPP.
So can anyone tell that what is wrong with it and how can it be solved? Or is it better to use Linux for RoR? I do many things on windows thats why if there will be some solution at windows then that would be helpeful.
thanks for your time, following is attached output image.
I also observed that rake db:create command is not creating db, I had to do this manually. Following is my configurations for db:
adapter:mysql2
host:localhost
encoding:utf8
database:kaasib_new
pool:5
username:root
password:~
So is this fine? I don't have password on local machine db and do I need to mention 3306 for mysql in it?
A couple of things to try:
If the tilde character in the password field above is a typo, ok,
but there shouldn't be anything there.
Not sure if it's a function
of posting here, but Whitespace matters in YAML files. It should be
set up with indents like below (socket is optional):
.
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: app_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
Open Gemfile from your project.
add line => gem 'mysql2'
run command => bundle update
restart your server.
I've this scenario.
A movie title:
$ title = "La leyenda de Osaín"
With this encoding:
$ title.encoding.name
>> UTF-8
I then saves it to the database.
$ movie = Movie.create!(:title => title)
Then I try to get the movie.
$ Movie.find(movie.id).title.encoding.name
>> "ASCII-8BIT"
$ Movie.find(movie.id).title
>> "La leyenda de Osa\xC3\xADn"
All other movies works that does not contain special characters like í and û.
This is my database.yaml file:
development:
adapter: mysql
database: development
username: linus
password: my_password
socket: /tmp/mysql.sock
encoding: UTF8
I'm getting the right sort of data when using forced_encoding.
$ Movie.find(movie.id).title.force_encoding("UTF-8")
>> "La leyenda de Osaín"
I'm using Rails 3.0.5.rc1 with MySQL 14.14.
Anyone knows what the problem may be?
I found a solution to my problem.
Now I'm using the newer mysql2 gem.
I replaced gem "mysql" with gem "mysql2" inside the Gemfile.
Then I changed the database adapter inside the database.yaml file.
From:
development:
adapter: mysql
database: development
username: linus
password: my_password
socket: /tmp/mysql.sock
encoding: UTF8
To:
development:
adapter: mysql2
database: development
username: linus
password: my_password
socket: /tmp/mysql.sock
encoding: UTF8
I think this was the deal breaker in my case:
Taken from Github MySQL2
[...]It also forces the use of UTF-8 [or binary] for the connection [and all strings in 1.9[...]
According to this link, rails scaffolding creates varchar(255) columns in mysql. The mysql documentation says the following about varchar(255):
For example, a VARCHAR(255) column can
hold a string with a maximum length of
255 characters. Assuming that the
column uses the latin1 character set
(one byte per character), the actual
storage required is the length of the
string (L), plus one byte to record
the length of the string.
My guess is that the column type in the database doesn't support characters that are represented by more than one byte. This link has more information about common pitfalls in rails when dealing with unicode strings and more specifically, it says you need to create your database as utf8 like so:
CREATE_DATABASE my_web_two_zero_development DEFAULT CHARSET utf8;