Rails is not connecting to remote mongo db through SSH tunnelling - ruby-on-rails

I have my mongoid.yml file set up like (eventually production environment will be called staging):
development:
host: staging.domain.com
port: 27018
username: domain
password: passw0rd
database: domain_production
production:
host: localhost
port: 27017
username: domain
password: passw0rd
database: domain_production
I can open up my tunneling like:
ssh deployer#staging.domain.com -L 27018:staging.domain.com:27017
I can open up my mongo shell with
mongo --port 27018
I can run mongod on the remote port but again, none of the entries on the staging server show up. (Do I need to run mongod?)
But when I go to the domain_production I don't see the documents that show up on the website itself. And when I try to start the rails server it hangs. Am I missing a step?

mongod is the mongo database process. It needs be run prior to running the "mongo" shell. mongo is the interactive javascript shell that you use to interact with the mongod process.
mongod picks up the configuration file, and starts listening on port 21017 by default. 21017 + 1000 = 22017 is the port where the http interface shows up. Did i answer your question?

Related

Logs say "fe_sendauth: no password supplied" but rails console working without problems

I'm trying to deploy rails app with capistrano, nginx and puma. When I visit the app I get errors like this in my production.log:
[11c972c9-c8fb-404f-93c3-9fc614ad815b] ActiveRecord::ConnectionNotEstablished (connection to server at "localhost" (127.0.0.1), port 5432 failed: fe_sendauth: no password supplied
):
When I log to this machine and run bin/rails console it works without any problems, I can create records, query records. Connection to database works without problems.
RAILS_ENV is set to production in /etc/environment
Any idea what could be the problem?
database.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
production:
<<: *default
database: app_production
username: app
pg_hba.conf
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
The solution for me was to delete PostgreSQL completely using,
sudo apt-get --purge remove postgresql\*
Click yes on that pop-up, re-install PostgreSQL using,
sudo apt update
then,
sudo apt install postgresql postgresql-contrib
then,
sudo service postgresql start
Finally, you'll also need to create a database user so that you are able to connect to the database from Rails. First, check what your operating system username is:
whoami
If your username is "stack", for example, you'd need to create a Postgres user with that same name. To do so, run this command to open the Postgres CLI:
sudo -u postgres -i
From the Postgres CLI, run this command (replacing "stack" with your username):
createuser -sr stack
Then enter control + d or type logout to exit.

Rails not able to access Postgresql deployed by Docker

I have a docker container that spins up my Postgres DB, Elasticsearch, and Redis; however, my rails server is unable to connect to the Postgres DB... The server starts, but when it attempts to connect to the DB, I get the error
PG::ConnectionBad (could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
):
When Docker launches the DB, it does so on localhost:5432. Via the terminal, I'm able to connect to the DB by running psql -h localhost -U myUser, but running psql results in the same error...
My rails database.yml file is configured to connect to the database at localhost:5432 with myUser, but I cannot figure out why it is giving me this error. It seems like Rails is attempting to connect to my Postgres.app server instead of the server Docker is running.
Here is my database.yml file
default: &default
adapter: postgis // I have tried "postgresql" as well
encoding: unicode
reconnect: true
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV['DATABASE_URL'] %>
development:
<<: *default
database: my_db
username: <my username>
password: <my password>
Here is my .env
DATABASE_URL=postgres://***:***#novus-postgis-localdev:5432
My docker container shows the following
ENVIRONMENT:
DATABASE_URL: postgres://<my username>:<my password>#novus-postgis-localdev:5432
PATH:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
...
PORTS:
5432/tcp: 0.0.0.0:5432
5432/tcp: :::5432
Therefore, my Rails environment is supposed to connect to my docker DB server, but it appears to be attempting to connect to my Postgres.app server (which isn't running).
What am I missing here / what is wrong with my configurations that have Rails looking at the wrong server?
FWIW, I'm on macOS and I installed Postgres via the Postgres.app.
When the Postgres.app is running a server, the psql command connects to it.
When the Docker container is running, I am unable to start the Postgres. App server because port 5432 is already used (as expected)
UPDATE
I did find out that my Docker DB container is listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" whereas my Rails is pointing to the socket /tmp/.s.PGSQL.5432. I am unable to figure out how to change either of these to be the same (rails to look at /var/run/postgresql/... or docker to launch socket at /tmp/...
In the Postgresql configuration file, try and enter,
listen_addresses='*';

How to connect to localhost PostgreSQL on Mac using PSequel GUI?

I followed this tutorial to install PG on my Mac. It's my first time using PG locally. I used Sqlite a lot in my Rails projects before this.
I found this PSequel GUI app and I just installed it. But I've no idea how to connect to my localhost PostgreSQL databases.
What is my:
Host/Socket?
Port? (I use localhost:3000 to test my Rails app locally)
User?
Password?
Database?
Thanks.
localhost:3000 is the default url of webrick webserver. By default postgre SQL service connection is to localhost on 5432 post.
$ cat /etc/services | grep postgres
postgres 5432/tcp # POSTGRES
postgres 5432/udp # POSTGRES
That is for unix like systems. And I have postgres server listening on 5432 port.
$ netstat -l --numeric-ports |grep 5432
tcp 0 0 localhost.localdomain:5432 *:*
unix 2 [ ACC ] STREAM LISTENING 11396 /tmp/.s.PGSQL.5432
So the fields will have the following default values:
Host/Socket
localhost
For test/development purposes most people use local server to connect to db.
Port
5432
That is just postgres server (not webserver) connection port. To validate just list and grep the postgres configuration file, like follows:
# cat /var/lib/pgsql/data/postgresql.conf | grep port
#port = 5432 # (change requires restart)
Here it is commented out, so default values are applied.
User
you are, but you must create the user role before connection as of the postgres user with createuser or psql terminal utilities.
Password
When you will create the user just leave password empty. So you be able then stay the field empty in the form.
Database
That is the database name. Create it with the command if needed.
You should enter below detail for configuration:
Host/Socket? - localhost
Port? - 5432
User? - DB user(My case its: postgres)
Password? - Password for above user
Database? - DB name of your project
You can see screenshot in this site: http://www.psequel.com/

setting ip address of database on different server

I deployed a sample Rails app to a remote server with postgres hosted on the same server. The database.yml file was like this.
production:
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: localhost
username: mrmann
password: secret
Everything worked fine. I then went into the database.yml file and replaced host: localhost with the ip address of a postgres database on another server host: 178.XXX.XXX like this
production:
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: 178.XXX.XXX.XXX #ip address of server with other postgres database
username: mrmann
password: secret
When I restarted postgres on the server with the Rails app, the sample application now gave me the 'something went wrong' page for Rails.
The username and the password for the username are the same on both dbs. Can you suggest what the problem might be? Thanks
Update
These are the settings in pg_hba.conf on the server with the database that I want to connect to.
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Fake Rails app IP: 192.241.XXX.X
Fake Database IP: 192.34.XX.XXX
Do you have access to the database logs? If your connection got as far as attempting to connect to the database, there should be some messages there. My WAG is that you need to add that IP to server's pg_hba.conf file and reload Postgres.
To be more clear, the IP to add to the server's pg_hba.conf is the IP that your application is connecting from, and you should also check the value of the database's listen_addresses setting. If the latter is not set to '*' or to the IP of your application, you'll need to change it in the postgresql.conf and restart the db cluster.
[edit, new information provided]
The line in your pg_hba.conf should be
host all all 192.241.XXX.X md5
Reload (not restart) postgres (eg. pg_ctl reload) and then try connecting again. If it fails, find the db log and see what it shows. If there is no message, then it might be a firewall problem. If the latter is suspected, look for any rules relating to port 5432 in the output of iptables -nvL

can't connect to rds at ec2 server, missing mysql socket

I have an ec2 server and rds server.
and a Ruby On Rails App
connecting to the rds with these settings worked for me in local ENV:
host: myappnameandhash.us-west-2.rds.amazonaws.com
adapter: mysql2
encoding: utf8
reconnect: false
database: mainDb
pool: 20
username: root
password: xxxx
socket: /var/run/mysqld/mysqld.sock
port: 3306
but on my EC2 server I don't have that mysqld.sock file
so i get this error:
FATAL: failed to connect to MySQL (error=Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2))
what do i need to install in order to have the socket?
thanks
update:
I removed the socket definition and the port.
I deploy using capistrano , now i ssh to my server and go to the "current" folder. there i try to run: rake ts:start
and i get the following:
rake aborted!
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
but i don't even have the socket definition in my database.yml file anymore
Remove the socket and port number from your database.yml file and then try, it will work.
You must specify the "host" of your RDS, and also configure the security groups in the RDS to allow your instance to connect to it.
I know this has been a while, just encountered this issue with my EC2 server connecting to RDS. Would like to share how I resolve this.
First removing socket and port from the database.yml help
#In your database.yml
development:
#some configuration
staging:
#some configuration
production:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 20
host: url.to.rds.server
database: your-database
username: your-username
password: your-password
To run this rake command on production, you need to specify your rails-env, e.g. if you are using production environment, it should be
bundle exec rake ts:start RAILS_ENV=production

Resources