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.
Related
We previously had a successful connection from Rails to an AWS MySQL instance through ActiveRecord, but as of our latest push to Heroku, that connection was severed. We don't understand why, as we have all traffic enabled on the AWS RDS settings, and the credentials in our database.yml config file have not changed. The only big thing that changed in the push to Heroku was adding the dovenv gem and passing control over environment variables to that gem.
When booting up our rails server in any of our three environments (dev, test, production), we receive a NoDatabaseError from ActiveRecord:
ActiveRecord::NoDatabaseError - Unknown database '<database_name>.us-east-1.rds.amazonaws.com'
We can successfully connect to the database through MySQL CLI.
database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: "<username>"
password: "<password>"
host: <database_name>.us-east-1.rds.amazonaws.com
port: <port>
development:
<<: *default
database: test
test:
<<: *default
database: test
production:
<<: *default
Relevant local dependencies:
activerecord (5.1.4)
mysql2 (0.4.10)
rails (5.1.4)
AWS security groups:
default (<group_id>) CIDR/IP - Inbound 0.0.0.0/0
default (<group_id>) CIDR/IP - Outbound 0.0.0.0/0
Publicly accessible: Yes
What we have tried:
Restarting MySQL server: $ brew services list $ brew services restart mysql
Viewing MySQL logs on AWS, which reveals the error: [Warning] IP address '<ip_address>' could not be resolved: Name or service not known
Testing in production, which when attempting to connect to the database, gives the error: ActiveRecord::StatementInvalid: Mysql2::Error: No database selected
Running rake db:create RAILS_ENV=development, which gives the error:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`<possibly_private_id>`.`us-east-1`.`rds`.`amazonaws`.`com` DEFAULT CHARACTER SET `utf8`' at line 1
Uninstalling mysql, /usr/local/var/mysql, and reinstalling mysql (through brew)
Checking ENV variables, of which there are none that relate to MySQL.
Confirmed that my mysql server is running by running mysql.server status
I have a theory that when installing the gem dotenv-rails, we may have lost some MySQL credentials that granted access to the server from our project's local directory... Again, we still can connect to the server through MySQL CLI so we know everything on AWS is fine and healthy. I also can't seem to log in to mysql as my local username, or root. Currently trying to figure that issue out.
Solved - turns out it was related to the modifications I made in my environment variable setup. In this section of the Rails Guides, it notes that specifically declaring "DATABASE_URL" as an environment variable will automatically set or override the database variable declared in your config/database.yml file.
In my case, the host variable (set in config/database.yml) was correctly set to MY_DATABASE_NAME.amazon.aws, but DATABASE_URL variable in my environment was silently setting the config file's database variable to the same MY_DATABASE_NAME.amazon.aws.
I do not have a database called 'MY_DATABASE_NAME.amazon.aws', which is why it was freaking out. Hope this helps someone else!
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.
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
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/
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