Connection refused - connect(2) for "localhost" port 9200 with DigitalOcean - ruby-on-rails

I am using DigitalOcean for my Ruby on Rails app with Elasticsearch. So I am getting
Faraday::ConnectionFailed in PetsController#create
Connection refused - connect(2) for "localhost" port 9200
I am wondering if it is because I am not using localhost on DigitalOcean. I am guessing if I have to change it in my app to start listening to the ip address I am using for DigitalOcean, but am not sure where I should look at.

Can you check the following on digitalocean:
sudo vi /etc/elasticsearch/elasticsearch.yml
Look for:
network.bind_host: localhost
source: https://www.digitalocean.com/community/tutorials/how-to-install-elasticsearch-on-an-ubuntu-vps
also, are you sure elasticsearch is running? The following command should give you a hash as result:
curl localhost:9200
If not: try to start it and try again:
sudo service elasticsearch start
Also, you'll probably want to boot elasticsearch automatically like described here https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-service.html

Related

Flyway connection failing, connecting using psql command line works fine

I have a docker service for postgres that is running fine:
postgres:
restart: always
image: postgres:13.0-alpine
ports:
- "5432:5432"
environment:
- DEBUG=false
- POSTGRES_DB=yola
- POSTGRES_PASSWORD=password123
I can connect fine using terminal like this:
export PGPASSWORD=password123; psql -h localhost -p 5432 -d yola -U postgres
Now when I try and run flyway I get a connection problem.
I tried doing both 0.0.0.0 and localhost and the docker image name yola_postgres_1.
I still get this error running the following command:
docker run --rm -v /path/to/folder/migrations:/flyway/sql flyway/flyway -url=jdbc:postgresql://localhost:5432/yola -user=postgres -password=password123 info
ERROR: Unable to obtain connection from database
(jdbc:postgresql://localhost:5432/yola) for user 'postgres':
Connection to localhost:5432 refused. Check that the hostname and port
are correct and that the postmaster is accepting TCP/IP connections.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Connection to
localhost:5432 refused. Check that the hostname and port are correct
and that the postmaster is accepting TCP/IP connections.
Caused by: org.postgresql.util.PSQLException: Connection to
localhost:5432 refused. Check that the hostname and port are correct
and that the postmaster is accepting TCP/IP connections. Caused by:
java.net.ConnectException: Connection refused (Connection refused)
The Problem
You have PostgreSQL running in one container and Flyway in another, and localhost is per container, so Flyway isn't communicating with PostgreSQL
A Solution
You already map 5432 in PostgreSQL to 5432 on the host, so you can add --network=host to the docker run command for the Flyway container which makes localhost match that of the host
This will make your current set up work, and you could then look into container networking if you want your containers to communicate with each other directly, not through the host

Can not connect docker mysql that's forwarding to 3306

When I'm trying to connect to a docker MySQL that's running and forwarding to my local TCP:3306 I get the following answer
mysql -u root -pPASSWORD
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
but when I do mysql -u root -pPASSWORD -h127.0.0.1 it connects wonderfully.
Any clue?
[UPDATE]
Considering the comments and this post I create ~/.my.cnf
with this content
[Mysql]
user=root
password=yourpass
host=127.0.0.1
port=3306
Giving these changes I could connect the localhost with the 127.0.0.1 address.
If you don't specify a host with -h (or a host directive in your .my.cnf), then MySQL defaults to connect to localhost. Connections to localhost use the UNIX domain socket interface, not TCP/IP. So it's not connecting to a TCP/IP port, and therefore does not forward to your docker container.
This distinction between localhost and 127.0.0.1 is a historical oddity of MySQL. Normally localhost and 127.0.0.1 are assumed to be equivalent. But MySQL treats the hostname "localhost" as special, using it to invoke the UNIX domain socket interface. This is a bit faster than using TCP/IP, but of course only works if the connection is on the local computer.

could not connect to server: Connection refused (0x0000274D/10061)

I'm using rails to develop a website but i get this error when i try to open my localhost
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
How to fix it guys
In Windows, search for "services" and search for postgres in the list. Right click and select "Start".
It looks like your postgreql server is not running.
You can try to launch it with :
On Linux :
sudo service postgresql start
On windows : changing XX by your postgresql version run :
net start postgresql-XX
You can also stop using postgres and specify a sqlite database in your config/database.yml file :
changing this line :
adapter: postgresql
by this line :
adapter: sqlite3
If you are using postgreql this issue is due to not starting a server, so you should start the server, one way to do it is to cd to postgresql bin and start it with pg_ctl, here is an example:
cd "C:\Program Files\PostgreSQL\14\bin"
pg_ctl -D "C:\Program Files\PostgreSQL\14\data" start
If you are running postgres in a docker image, be sure to map the container port to the host. You can do this with docker run -p 5432:5432 <container-name> or by adding the following to your docker-compose.yml file:
ports:
- 5342:5342

Redirect 192.XX.XX to localhost - OSX Yosemite

I would like to redirect 192.168.199.128 to localhost on OSX Yosemite. I'm running a Rails server on port 3000 and I would like to access it using localhost:3000/home as well as 192.168.199.128:3000/home.
I understand that iptables was removed in OSX Yosemite and pf is the preferred method of doing port forwarding but I'm unable to get it working.
This appears in /etc/pf.anchors/com.analysis, with a newline below it
rdr pass on lo8 proto tcp from any to 192.168.199.128 port 3000 -> 127.0.0.1 port 3000
Additionally, I added the following line to the end of /etc/pf.conf
load anchor "com.analysis" from "/etc/pf.anchors/com.analysis"
Finally, I started pf with the following command:
sudo pfctl -ef /etc/pf.anchors/com.analysis
Unfortunately, I still cannot access my rails server at 192.168.199.128:3000/home.
Thanks in advance for your help.
rails server -b 192.xxx.xxx.xxx

rails app use postgresql shows PG::Error

I just started a new rails app using
rails new myapp --database=postgresql
Then I generate some static pages:
rails g controller StaticPages home about
When I started the server and visit localhost:3000/static_pages/home, the PG::Error shows up:
PG::Error
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I didn't change anything else
Any idea how to make postgresql work?
It looks like your postgres server is not running. Have you started it up? If you check your system processes do you see the postgres daemon running?
If you are trying to use rails + postgres, I would recommend checking out this great video by Pete Cooper.

Resources