Can not connect docker mysql that's forwarding to 3306 - ios

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.

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

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

Unix domain or TCP

I have a fairly simple Rails app that connect to Postgres server (locally).
Uptil now, I was under the impression if not specified (socket in the database.yml) the pg client connect with postgres over TCP on a loopback interface.
Fyi,this is how my configuration looks like.
{:adapter=>"postgresql", :encoding=>"unicode", :database=>"scp_development"}
But I think I was wrong I can't sniff any packet over the loopback interface at port 5432.
sudo tcpdump -nnvvXSs 1514 -i lo0 dst port 5432
Which is letting me believe that it working over Unix domain socket.
So, is there a fairly reliable way to determine ...
Whether I'm connected to TCP socket or Unix socket?

host can not recognized in postgres

We have seen a issue in log
FATAL: lock file "/tmp/.s.PGSQL.5432.lock" already exists
HINT: Is another postmaster (PID 4696) using socket file "/tmp/.s.PGSQL.5432"?
LOG: could not bind IPv4 socket: Address already in use
We have deleted /tmp/.s.PGSQL.5432.lock
We restarted the postgres service the service restarted
But the psql -h localhost -d database -U newuser
shows error role 'newuser' does not exists
but if we connect without host
psql -d database -U newuser
It worked, How can We work with host options ?
Is there we ,missing somthing after deleting tmp file?
Same as the last one, you have two instances of PostgreSQL running on the machine.
You delete the socket file rather than shutting down the instance. This allows your second instance to start, but it's only listening on the unix socket. Presumably listen_addresses is unset/empty so it doesn't attempt to bind to a TCP/IP socket, since it'd fail if it tried and port 5432 was already in use.
The other instance is still running and still listening on port 5432 for TCP/IP connections. When you specify -h localhost you get a TCP/IP connection instead of the default unix socket connection, so you are connecting to the other instance of PostgreSQL on your system.
All your problems are caused by going around deleting and killing things rather than identifying the underlying problem - the second PostgreSQL instance - and addressing that.

Rails PostgreSQL - Changing socket name/location

I have changed the port of my PostgreSQL server, hence the socket it creates is no longer named ".s.PGSQL.5432".
Is there a way to update/force Rails/the pg gem to change the socket name it looks for? I have tried uninstalling/reinstalling the pg gem in line with recommendations but it still tries to look for a socket named .s.PGSQL.5432, not the .s.PGSQL.50000 that it should and as per the database.yml comments, the 'socket' config line does not apply for PostgreSQL.
I can obviously create a symlink, but I was hoping there was a more elegant way around this.
Change the port in your database.yml
PostgreSQL supports two kinds of connections as you are aware: tcp/ip and unix domain sockets. Both can use host and port but use them in different ways.
When working with TCP/IP, host and port have the standard meanings with regard to networs running TCP/IP.
When working with Unix domain sockets, host provides the directory, and port provides the numeric portion of the socket name. Paths should be absolute. The same should hold true for any libpq-based connection (including through the pg gem), so consider this:
psql -h /tmp -p 5000
This gives an error message of:
$ psql -h /tmp -p 5000
psql: 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.5000"?
Similarly:
$ psql -h /foo -p 50000
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/foo/.s.PGSQL.50000"?

Resources