Can Rails connect to MySQL using MySQL option files? - mysql

I'm looking at changing our database connections to use the MySQL configuration utility. Is there a way to tell Rails/ActiveRecord to use .mylogin.cnf instead of specifying connection info in database.yml?

mysql2 gem uses libmysqlclient, which in turn can use .mylogin.cnf starting with 5.6, but there's no way to control its version from rails. Also there seem to be some issues - mysql2 0.4.2 segfaults.
Actually this is possible by setting default_group (= mysql login path) (and default_file if needed) options upon creating mysql2 connection.
But prior to rails 5 these are not passed in connection adapter, but setting blank username and using client login path seems to work.
For example set:
development:
username: ""
default_group: client
database: some_db

Related

use .my.cnf for ruby mysql connection

In a ruby script (not rails) Iam using mysql. Because my mysql are available in a .my.cnf file, I can use mysql on the terminal without password. But that does not work in a ruby script, how can I achieve that?
If you are using mysql2, you may need to read the .my.conf manually, as show in the doc
Reading a MySQL config file
You may read configuration options from a MySQL configuration file by
passing the :default_file and :default_group parameters. For example:
Mysql2::Client.new(:default_file => '/user/.my.cnf', :default_group =>
'client')

Rails with MySQL v.4.1

Is there any chance to make Rails application connect to old MySQL v4.1.20 server?
rails db
command works fine, but I cannot run the application.
When I generate new app with
rails new app_name -d mysql
I see the following in database.yml file:
MySQL. Versions 4.1 and 5.0 are recommended.
Which should mean I am able to use it.
However Rails Active Record uses MySQL v.5.0.2+ feature (https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb):
# Make MySQL reject illegal values rather than truncating or blanking them, see
# http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_all_tables
# If the user has provided another value for sql_mode, don't replace it.
if strict_mode? && !variables.has_key?(:sql_mode)
variables[:sql_mode] = 'STRICT_ALL_TABLES'
end
Unfortunately I have nothing to do with old MySQL server version. I have to use it.
Adding
strict: false
to the connector settings in database.yml solved the problem.

How to read in username/password from ".my.cnf" in Ruby MySQL driver?

Is there a way to connect to the MySQL server using the mysql gem without passing a username/pwd combo and instead loading it from the .my.cnf file?
Easiest way to load them is using inifile gem.
require 'inifile'
client_options_hash = IniFile.new('~/.my.cnf')['client']
Then you can just use the values from client_options_hash to set up your connection.

Specify SSL for Heroku external MySQL database connection

I'm running a Rails 3.2 app on the Cedar stack at Heroku.
I'm using Amazon RDS for my MySQL database, and I have the proper DATABASE_URL setup in the Heroku config vars.
How do I get Heroku to use SSL in its connection to Amazon RDS?
Normally this would be specified as a value in database.yml, but since Heroku generates database.yml for us, I'm not sure how to control this setting.
Thanks!
You can specify some mysql2 SSL params through the DATABASE_URL config. They will get added as items to the dynamic database.yml that is generated during the Heroku build process, and so they'll be passed when mysql2 connections are created.
The only param you need to pass for this to work is sslca (not to be confused with sslcapath).
1. Download the Amazon RDS CA certificate and bundle it with your app.
(Edit) Amazon will be rotating this certificate in March 2015. You'll need the new file from that page instead of this one.
curl https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem > ./config/amazon-rds-ca-cert.pem
2. Add the file to git, and redeploy to Heroku.
3. Change DATABASE_URL to pass sslca:
heroku config:add DATABASE_URL="mysql2://username:password#hostname/dbname?sslca=config/amazon-rds-ca-cert.pem -a <app_id>
The relative path there is important—see below.
That's it! Now that you have SSL working, you may want to enforce that all connections with that user only allow SSL:
GRANT USAGE ON dbname.* TO 'username'#'%' REQUIRE SSL;
Troubleshooting
Make sure to pass a relative path to sslca! Otherwise, rake assets:precompile may break with an SSL error. If you receive an error like:
SSL connection error: ASN: bad other signature confirmation
or even just:
SSL connection error
...then there is likely something wrong with how the CA cert file is referenced.
From looking at the injected database.yml (see bottom of http://neilmiddleton.com/sharing-databases-between-heroku-applications/) you can pass in extra configuration as part of the db URL as query params.
In theory, this should let you configure it how you want although I've not tried it.

rails windows problem

I just installed ruby on rails on windows.
install mysql and created a new project. Then I changed database.yml to use my own mysql server as follow
development:
adapter: mysql
database: mytools
username: test
password: test
when I try to access story controller(http://localhost:3000/stories), error shows
"SQLite3::SQLException: no such table: stories: SELECT * FROM "stories" "
Why am I getting this error? I am not using mysql...
By default Rails creates and uses a SQLite database not a MySQL one.
You can specify the database to use with the -d flag when creating your Rails application. For example to create a rails app called "sample" using mysql as the database:
rails sample -d mysql
Taken from the Getting Started with Rails guide.
You might also want to check out the section on Configuring a MySQL Database in the same document.
From what I know - though I was coding in rails long ago, there are 3 separate databases there: development, production and test. Maybe You are trying to use test or production and You didn't configured them?
Did you try a restart on the webserver?
Check if the RAILS_ENV is set to production and if it is, set it for development:
set RAILS_ENV=development