Database connection to remote machine in ruby on rails - mysql

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/

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.

How can I connect to a local SQL server?

I want to create my own database with my tables to practice some SQL. I cannot create a database unless i connect to a server. I have tried Microsoft SQL Server Management Studio and MySQL Workbench but both failed.
In Microsoft SQL Server Management Studio, I tried:
Server Type: Database Engine
Server Name: "DESKTOP-MUO9FQ2" and "." and "local"
Authentication: Windows Authentication
GRAYED OUT:
Username: DESKTOP-MUO9FQ2\15072
Password:
In MySQL Workbench:
Connection Name: localhost
Host Name: 127.0.0.1
Port: 3306 and 1433
Username: root
How can I connect to any server so I can create my own database and play around with my data using SQL?
I installed MySql which includes MySql CLI, and it works fine.
You just have to install MySql and you will have the CLI includes.
In the installation, you will have to define a root user and a user each with password.
Then run MySql CLI and you will have to enter the root password you choose, and then it's done.
Now you can can create databases ,tables and so on...

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.

Use Ruby on Rails and SSH to access remote MySQL database on remote server

How do I use SSH to make my RoR db calls to a remote MySQL db on a remote server. I have the host ip, ssh username, ssh password, db, db username, db password, and port. Any examples I have seen show the code just in a ruby script but I do not know how to apply that solution to my web application. Do I need to change anything in my database.yml file? I appreciate any help...
The way to go about this is to create an SSH Tunnel to the destination database host and point the local server to it.
Check out: How do I configure Rails for password-less access to remote database
You can connect to remote databases from Rails. Simply edit your database.yml:
development:
adapter: mysql
encoding: unicode
pool: 5
username: username
password: password
host: hostname
port: port_number
database: database_name

Can't connect to MySQL server on 'myhost.com' (4)

I'm able to connect to the remote database from a MySQL client and using terminal as well. But when trying to access the database from the Rails app, I got this error:
Mysql2::Error (Can't connect to MySQL server on 'myhost.com' (4))
My database.yml has the following configuration for development:
adapter: mysql2
encoding: utf8
reconnect: false
host: myhost.com
port: 3306
database: the_database
pool: 5
username: myusername
password: mypassword
socket: /var/lib/mysql/mysql.sock
I have changed the name of the host to post this question, as the username, password and database.
Googling around, I could see different reasons for not connecting to the database, but none of them are similar to mine and with this code 4 (is that an error code?).
I'm new to Rails, so I have no clue what should I do next in order to fix it. I have double checked my configuration, even the socket. I've personally checked on the server side and this is the right path to the socket.
I'm not sure if it's relevant, but this user has read only access to this database.
I'm using Rails 3.2.8 and Ruby 1.8.7.
I was having the same headache error (The error is "Can't connect to MySQL Server (4)) for about 3 weeks, finally i discovered that my problem was based on a firewall rule.
The solution was to ask to my hosting provider to open the port 3306 but in the OUT way.
I was using CPanel and the main point here was that i does not have any kind of control to manage the CPanel firewall.
DonĀ“t try to expect as this is the big solution, because there are many things to be considered in configuration, but hey! i finally got it and it was all about the firewall of my provider.
Hope this help for someone.
You are using both a host which can be a remote host or the localhost but you also use socket which is a path to a UNIX Socket thats located in your servers filesystem. The socket is the prefered way to connect to your local database. If yo dont know the path tpo your mysql socket you can look it up in your mysql config file thats located in /etc/mysql/my.cnf in most cases.
If you want to connect to a remote host you should check if you can ping it and if you can you should try telneting the port like this:
telnet <hostname> 3306
Which will allow you to check if theres a deamon listening on the port your trying to connect to. If you cant connect to the server and you control the server there might be some firewall rules restricting access. Its definitfly not a mysql restricting problem because host restrictions causes other error messages.