Connecting Rails 4 with remote MySQL database - mysql

I have been working with Rails since two months but until now I haven't use databases.
I would like that my web-app connects to a remote database (MySQL) and extract some data to plot it in Rails using Highcharts.
I have found many tutorials that explains how to connect to an existing database (editing config/database.yml) but this is in the same server.
I am using Ubuntu 14.04 LTS.
Could anyone explains me which are the steps or how can achieve this connection to the remote database with Rails and how to extract the data from a given database?

You just edit the database.yml file so that it points to the remote database:
adapter: mysql2
encoding: utf-8
pool: 5
username: "username"
password: "password"
host: "hostname"
port: "port number"
database: "database name"

After two days asking in forums and people, nobody could help me, however, I found a solution.
IMPORTANT: To connect to an external database you should first create the SSH tunnel!!!!
Configure the database.yml

Related

Remote Connection to MYSQL from Heroku Rails App

I am having some difficulties for the past few days while trying to connect to a remote MYSQL DB from a Heroku hosted Rails App.
Locally I am able to connect to the remote database and retrieve/modify data.
My database.yml file:
remote:
adapter: mysql2
host: host.address
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
database: databasename
username: username
password: password
port: 3306
Model that I try to use this remote database for:
class Test < ApplicationRecord
establish_connection(:remote)
self.table_name = "tests"
end
The mysql database is hosted on server with Cpanel which has remote database service, where I configure to be accessible from every ip (0.0.0.0), (%), (www.mywebsite.com).
When I try connecting to the database with e.g.: running Test.new via heroku rails console, I get the following:
app/vendor/bundle/ruby/3.0.0/gems/activerecord-7.0.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb:51:in `rescue in new_client': There is an issue connecting with your hostname: my.host.ip. (ActiveRecord::DatabaseConnectionError)
Please check your database configuration and ensure there is a valid connection to your database.
/app/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.4/lib/mysql2/client.rb:95:in `connect': Can't connect to MySQL server on 'my.host.ip:3306' (110) (Mysql2::Error::ConnectionError)
This is my secondary Database. My production one is postgresql used with the postgresql AddOn on heroku..
All the ENV variables I use in heroku are for the username, pass, database name, host.
I have read all the questions here regarding this and tried everything. Not sure what I am missing.
Thanks for any advice.

Connect to MySQL using Ruby on Rails

Im currently working with ruby on rails, which is an area that I'm entirely new to. I'm using MAMP and have set up a local MySQL database called my_db with a table called employees https://localhost/phpmyadmin.
Is there a way to connect to this database using ruby on rails and write to this table in the database. I have tried many examples but all of them show you how to connect to a mySQL db installed natively. I can't seem to work out how to connect to a database that lives in MAMP. Any suggestions?
Rails connects to whatever's defined in config/database.yml, so you should be able to connect in much the same way any PHP code does:
development:
driver: mysql2
host: localhost
username: my_username
password: my_password
database: my_database_name
Generally you connect over host with an optional port parameter.

can anyone help me to connect rails 4 application to ms sql database(remote server) in windows system?

I succeeded in connecting to the remote database through ruby program by using dbi,ruby-odbc gems and by creating a system DNS for the remote ms sql server.
But I don't know how to connect using rails application, and I searched for the solution but I found it is needed to use tiny_tds but I am not sure it may solve the situation.
any help would be appreciated.
thanks in advance..!!
In your database.yml file add your external db info ex:
# External Database for WW
external_database:
<<: *default
database: db_name
username: username
password: password
host: www.host.com
Then create the appropriate model.rb files for the tables in the remote database, tell it to connect for the query, and add a custom table name if needed ex:
#app/models/product.rb
class Product < ActiveRecord::Base
establish_connection :external_database
self.table_name = "PRODUCT"
end
then bin/rails c and test it out

Mysql and Nitrious IO

I just created a rails box in Nitrous IO. Rails boxes come predefined to use sqlite3 but I want to use mysql2. Since I can't use mysql from the box because it fires up an error that it can't connect to a socket, they can connect the box to a free database service in Heroku. I created an account in Heroku, and logged in from my box to heroku. I am having problems linking the database (cleardb) to the rails box in NitrousIO, since they only show procedure to link a postgres db.
My database.yml file has the following:
development:
adapter: mysql2
encoding: utf8
database: testdb_development
pool: 5
username: root
password:
host: localhost
Did you already configure the development settings within the database.yml file? You will need to change the host, username, and password fields which you listed above to match the settings of your cleardb.
Here is another post which states how to retrieve your host database URL: Remote connect to clearDB heroku database
On the top corner click on Addons and then select ClearDB MySQL
Database. Once there, click on your database and choose the 'Endpoint
Information' tab. There you see your username/password. The URL to the
database can be acquired by running heroku config --app
In my case, it was something like:
mysql://user:pass#us-cdbr-east.cleardb.com/TABLE?reconnect=true What
you need is this part: us-cdbr-east.cleardb.com
[Update January 2014]
Boxes should use Autoparts to manage packages / services like MySQL, Postgres, Redis, etc...
Please see this article for more information:
http://help.nitrous.io/mysql/

Database connection to remote machine in ruby on rails

Here is strange problem.
databse server ip : 170.2.14.131
application server ip : 170.2.14.137
Application is on cloud server. Database server is on another instance
and application hosted on different instance.
production:
adapter: mysql
database: database_name
username: ************
password: ************
host: 170.2.14.131
This is my database.yml configuration for production environment.
Now when i am trying to connect through my application it's using
application server IP instead of remote databse IP.
Please suggest.
By default, MySQL database server remote access disabled for security reasons. there're 3 solutions to this problem, i'll not describe them here, you can read about it in my blog here:
http://notes.kloop.kg/2011/11/17/enable-remote-access-to-mysql-database-server/