Error connecting Rails 5 app database.yml to AWS RDS instance - ruby-on-rails

I already set up SLL certificate like here:
export DATABASE_URL="mysql2://leder:password#pfhpdb.cyo1f7mucyku.eu-central-1.rds.amazonaws.com/pfhpdb?sslca=config/amazon-rds-ca-cert.pem"
when running my heroku rails 5 app w/ production environment and DB I get the following error:
ActionView::Template::Error (Can't connect to MySQL server on 'pfhpdb.cyo1f7mucyku.eu-central-1.rds.amazonaws.com' (101 "Network is unreachable")):
My database.yml is as follows:
production:
adapter: mysql2
encoding: utf8
host: pfhpdb.cyo1f7mucyku.eu-central-1.rds.amazonaws.com
database: pfhpdb
pool: 5
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
Comment: same error when connecting to RDS w/ SQuirreL - class java.net.SocketException: Network is unreachable (connect failed)!
UPDATE 20200503:
as of this post:
Mariadb connection client: Access denied for user (using password: NO) on mysql 8.0
I cannot login w/ password in MariaDB and cannot change DB table access b/c I cannot login w/ password in MariaDB. I am shut off from my DB...

Please check security group of your RDS instance; probably you have not opened the port. Also, please try this to see if port is open or not.
$ telnet cyo1f7mucyku.eu-central-1.rds.amazonaws.com 3306

In the end the authentication error went away by resetting the AWS RDS DB administrator password in the console settings of RDS.

Related

AWS ElasticBeanstalk EC2 gives postgresql connection error

When I deploy my app on aws beanstalk using eb deploy it gives an error
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
.ebextensions/postgres.config:
packages:
yum:
postgresql93-devel: []
config/database.yml:
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
production:
<<: *default
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
I had the exact same issue.
The problem is that you have to connect your database to the application through the environment variables.
First you need to create an RDS instance to connect to. This link shows you how: https://aws.amazon.com/getting-started/tutorials/create-connect-postgresql-db/?nc1=h_ls
Then, after the instance is created, you need to go to the RDS Service page, and choose instance in the left menu. Choose the instance you created in the list and proceed to the section called "Connect". Those are the values you put inside those environment variables, with the HOSTNAME value being the endpoint.
Hope it helped, worked for me!
I had a similar problem, but in my case the RDS connection was timing out. The solution was add the security group of my EC2 instance to the "inbound" section of the RDS security group.
To do this go to EC2 page and search for "Security Groups", there you can find your RDS and EC2 security groups, click in the RDS security group and add the EC2 security group.

Securing remote mongodb server to use with Mongoid

OK SO land, I have exhausted all attempts to find an answer for my issue.
I have a RoR app running Mongoid 3.16 and a remote server with Mongodb 3.05. Both servers are on the same network and I am using internal IP address to connect.
The problem that I am having is trying to secure the connection. First off, if I try to add the app IP to the bind_ip in the mongod.conf file I get an error:
Cannot assign requested address for socket: app_ip_address
Here is the bind_ip in the mongod.conf:
#bind_ip=127.0.0.1,app_ip_address
If I uncomment it and try to restart mongodb it gives me the error about "Cannot assign requested address..." So I can connect from the app to mongodb IF I leave the bind_ip commented out. I do not like this idea because it will allow any connection.
My mongoid.yml file is:
production:
sessions:
default:
database: my_database
hosts:
- mongodb_server_ip:27017
I also set up the database to have a user but if I set the auth to true for Mongodb, it will allow connection to the "my_database" without authorization, but cannot read the data unless I authenticate again (which I can do command line but not via the mongoid.yml file)
For example if I connect command line from the app server with:
mongo mongoddb_server_ip:27017/my_database
That works but when I try to view collections I have to db.auth(....) to see them.
I guess that if I can config the mongoid.yml with the username/password for mongodb that would work but I cannot figure out how to do that.
Update 11/23:
OK, I set the mongod.conf file to auth = true. Now I can connect from the app server via ssh:
mongo mnogodb_server_ip:27017/my_database --username some_username --password some_password
That allows me to connect and access all collections etc. Now that I am requiring authentication I updated my mongoid.yml file as so:
production:
sessions:
default:
database: my_database
username: some_username
password: some_password
hosts:
- mongodb_server_ip:27017
I restart the server and I cannot connect to the remote db.
Well, after all of this I find out that moped 2 does not support authentication for MongoDB 3+.
see here:
Rails Mongoid fails to authenticate - failed with error 13: "not authorized for query on my_db.my_collection"

Can't connect localhost to heroku database

I am trying to get a rails app to work with heroku but I am doing something wrong.
Database.yml
development:
adapter: postgresql
encoding: unicode
database: (heroku db)
user: (heroku db_user)
pool: 5
password: (heroku db_pass)
If I enter the information from my heroku database I get:
PG::ConnectionBad FATAL: password authentication failed for user
I am not rails guy, But this is something that i have done just now on my spring app, I wanted to connect my localhost development to heroku db.. I got db info from this:
heroku pg:credentials DATABASE
then I have passed params.. But I found that to connect remote heroku db, I needed to add this to my connection params..
ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
I guess, you also need to specify the host and port. To find out the host try running this command:
$ heroku config | grep HEROKU_POSTGRESQL
You should see something like
HEROKU_POSTGRESQL_RED_URL: postgres://user3123:passkja83kd8#ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398
if you already added the PostgreSQL add-on. More on this read here.
Then add the host and port info to your configuration:
development:
...
host: ec2-117-21-174-214.compute-1.amazonaws.com
port: 6212
...
But do remember that it's a bad idea to work with the production database from your local dev machine, unless you really know what you are doing.

Trying to connect to a mongodb instance on ec2 with mongoid as orm in rails

basically I have a mongodb instance running and working on ec2. On the side I have a rails 3.2 app with mongoid as orm working on local. What I want to do next is try to connect my rails app to the mongodb instance using mongoid. Also, intending to host my rails app on Dotcloud later
Ran the code rails g mongoid:config to generate the mongoid.yml file with the following code:
development:
host: localhost
database: mongotest_development
test:
host: localhost
database: mongotest_test
set these environment variables on your prod server
production:
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
# slaves:
# - host: slave1.local
# port: 27018
# - host: slave2.local
# port: 27019
From here onwards, I don't think I have a clear picture of how all this is going to work. But I did some trial and error. Firstly I wanted to try connecting to the mongodb instance on development, so I commented out the mongoid.yml defaults and added the following:
development:
host: <public dns of the mongodb instance>
port: 27017
# username:
# password:
database: <I ssh into the instance and created a database>
I commented the username and password out partly because I am not sure what to put, and partly because when I inspect the mongod.conf file on ec2, I saw that by default :auth is false, so I assume authentication is not required. So I ran rails console and got the following error:
Failed to connect to a master node at <public dns of the mongodb instance>:27017 (Mongo::ConnectionFailure)
from /Users/Kinglee/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/mongo-1.6.2/lib/mongo/connection.rb:589:in `setup'
from /Users/Kinglee/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/mongo-1.6.2/lib/mongo/connection.rb:114:in `initialize'
from /Users/Kinglee/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/mongo-1.6.2/lib/mongo/connection.rb:165:in `new'
from /Users/Kinglee/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/mongo-1.6.2/lib/mongo/connection.rb:165:in `from_uri'
from /Users/Kinglee/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/mongoid-2.4.10/lib/mongoid/config/database.rb:86:in `master'
from /Users/Kinglee/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/mongoid-2.4.10/lib/mongoid/config/database.rb:19:in `configure'
from /Users/Kinglee/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/mongoid-2.4.10/lib/mongoid/config.rb:290:in `configure_databases'
....
At this point, I am kind of confused. I kept asking myself, do I need the username and password to connect to mongodb ? I kind of 80% sure that I need them but I am not sure where to find them or rather not sure what am I connecting to, the mongodb ec2 instance or the mongodb database. How should I go about doing that ? Should I open port 27017 and 28017 on the instance ? Do I need to add config to database.yml (I highly doubt I need to since there is already mongoid.yml but just want to confirm)
I have been looking at a list of documentation and tutorial:
http://mongoid.org/docs/installation/configuration.html
http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-AbouttheKeyFile
MongoDB and Mongoid in production - looks like what I looking for, but not sure, going to try it.
http://craiccomputing.blogspot.com/2011/02/authentication-in-mongo-and-mongoid.html
Appreciate any advice from anyone here.
Ok finally found the problem. In the mongodb.conf file, there is a setting which called
bind_ip = 127.0.0.1
I was blind to not notice this, it means that the server can only be access locally and not externally, hence the fail connection error. A quick fix would be to change it to
bind_ip = 0.0.0.0
and it works. But thanks for the advice guys.
It is most likely a firewall issue. Check to see if the security group for your ec2 instance has the default mongodb port 27017 open.
This article will give you the gist of how it works if you haven't done something like that before:
http://cloud-computing.learningtree.com/2010/09/24/understanding-amazon-ec2-security-groups-and-firewalls/

Rails postgresql DB connection fails

I'm very new to Ruby and postgres.
Below is my database.yml
development:
adapter: postgresql
database: test_database
username: postgresql
password: mypassword
host: localhost
encoding: utf8
The user exists and I'm, able to login using same credentials in phpPgadmin. But when I start rails server and go to home page of app, I get FATAL: Ident authentication failed for user "postgresql".
Edit: In case pghba.conf matters,
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
#local all all peer
local all postgres md5
local all postgresql md5
Could anyone please help ?
open PostgreSQL client authentication configuration file
vi /var/lib/pgsql/data/pg_hba.conf
This file manage below stuffs
Which hosts are allowed to connect
How clients are authenticated
Which PostgreSQL user names they can use
Which databases they can access
By default Postgresql uses IDENT-based authentication. All you have to do is allow username and password based authentication for your network or webserver. IDENT will never allow you to login via -U and -W options. Append following to allow login via localhost only:
local all all trust
host all 127.0.0.1/32 trust
Save and close the file. Restart Postgresql server:
service postgresql restart OR
sudo /etc/init.d/postgresql restart
It should work
I can find my pg_hba.conf file in the path:
/etc/postgresql/8.4/main/pg_hba.conf
For anyone who still can't find their pg_hba.conf file, I'm using PostgreSQL v9.2 and I found mine in:
/var/lib/pgsql/9.2/data/pg_hba.conf

Resources