Connect to MySQL using Ruby on Rails - mysql

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.

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.

Ruby on Rails - Mysql2::Error: Can't connect to MySQL server on 'localhost' (10061)

Hey I'm just getting started with Ruby on Rails and need some help. I installed Ruby on Rails and then created a project with MySQL but I am unable to connect to the MySQL server to create a database. Side note I am using Windows 10.
After running the command (rake db:create) I get this error.
#<Mysql2::Error: Can't connect to MySQL server on 'localhost' (10061)>
Here is my database.yml file
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: 1234
host: localhost
Assuming you're running Linux, try the following command to see if MySQL is running:
/etc/init.d/mysql status
Make sure you're specifying a database name to connect to as well, but that would likely give you a different error code than the one you're seeing.

Connecting Rails 4 with remote MySQL database

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

Do I need MySQL installed locally as well?

I have 2 ubuntu 11.04 servers on IPs 192.168.9.14 and 192.168.9.15. On .14 I have installed ruby + rails and on .15 I have installed mysql and the database for my site resides on .15
I now want to get rails 2.3.8 to connect from .14 to the database on .15. I have the following in my database.yml on .14.
production:
adapter: mysql
encoding: utf8
reconnect: false
database: gtt_production
pool: 5
username: root
password: admin
host: 192.168.9.15
port: 3306
I'm currently getting an error showing up in my rails log as follows:
Can't connect to MySQL server on 192.168.9.15.
I originally thought this was due to user privileges on the mysql server but I have now added that in and still getting the same problem.
So I then tried connecting via a terminal (command line) as follows:
mysql -h 192.168.9.15 -uroot -p
and I got a message saying :
The program 'mysql' can be found in the following packages:
* mysql-client-cor-5.1
Try: sudo apt-get install <selected package>
...And that's what makes me think I need a local copy of mysql even though the database completely resides on another server.
Please can someone confirm whether my 'guess' is correct or whether there is some 'clever' way of connecting to the mysql server on .15 without also installing mysql on .14.
you need the client - sudo apt-get install mysql The server is usually mysql-server
Did you install the mysql gem for ruby? Rails will need this in order to connect to the DB. Also make sure .15 can accept remote requests from .14.
Have you configured your mysql db to listen to external IP traffic? I dont think MySQL by default allows external connections, only localhost traffic. Check your my.cnf.

How do I connect a mysql database file to a local ruby on rails application

I have a database file (name.sql) that was sent to me that I am supposed to connect to a rails app locally hosted on my mac, that I downloaded (forked?) from github. How do I set up my database.yml file to connect with the sql files.
You can't connect a Rails application directly to a SQL file. The Rails application gets its data from a database server and you import the contents of the SQL file into a database hosted by the server.
You can download a DMG archive which will install MySQL Community Server on your Mac from http://dev.mysql.com/downloads/mysql/#downloads
That download also includes a handy Preference Pane for starting and stopping the server.
Once you have MySQL up and running then you should set a password for the root user (i.e. the database system administrator) using
mysqladmin -u root password "secret"
—Obviously replace secret with the real password you want to use.
Then you can set up the database.yml file for the Rails application. For an application named app it would look like this:
development:
adapter: mysql
database: app_development
username: root
password: secret
host: localhost
test:
adapter: mysql
database: app_test
username: root
password: secret
host: localhost
production:
adapter: mysql
database: app_production
username: root
password: secret
host: localhost
Note that typically in production you'd create a separate limited privilege database user account for the Rails application to connect to MySQL with, but for development on your local machine the root account is fine.
After this step you can run rake db:create from the root of the Rails application within the Terminal. This command will create the app_development database in MySQL (rake db:create:all creates the test and production databases too). Finally, you can import your SQL file by entering the following command in the Terminal:
mysql -u root -p app_development < path/to/file/name.sql
You will be prompted for the MySQL root password. Replace path/to/file with the full path to the SQL file if it's not within the Terminal's current directory. For example, use ~/Desktop/name.sql if it's on your desktop.
Probably easiest: you need to run a database server on your mac. Then import your data into your database server.
Tutorials on installing rails on a mac will also talk about how to install the local database server and setting up the database.yml file