I am trying to connect to a free tier redshift cluster through a python script on a local machine, and keep getting the following error:
> psycopg2.OperationalError: connection to server at "<clusterendpoint>" (<clusterip>), port 5439 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
I am using psycopg2 using the following code:
rs_conn = psycopg2.connect(
dbname= dbname,
user= username,
password= password,
host = endpointurl,
port = port)
My VPC is publicly available. I have tried adding my ip to the security group, and have also tried adding 0.0.0.0/0 to the security group, but haven't had any luck.
I used boto3 describeclusters() to investigate the problem further, and I did notice that neither the public nor private ip matched the ip shown in the error above associated with the endpoint url. Is that normal? I know that the endpoint url is correct.
Is there a better way to investigate this issue that will give me a better idea as to where the connection is going wrong?
Related
Hello , i am trying to connect to the database on my Hosting . I followed this tutorial : https://www.thepythoncode.com/article/connect-to-a-remote-mysql-server-in-python
This is my code :
import pymysql.cursors
import pymysql
try:
connection = pymysql.connect(host='195.201.204.153',
user='turboweb_jobsuser',
password='TYcfvA*****',
db='turboweb_jobsdb_user',
)
print("Connected to:", connection.get_server_info())
except Exception as e:
print('connection failed')
print(e)
However, everytime I try to connect, this error occurs :
(2003, "Can't connect to MySQL server on '195.201.204.153' (timed out)")
The website address is : assamjobsapp.turboweb.online and doing a ping here,give me this IP :
195.201.204.153
I have created the Database , User and tables from the Hosting PHPMyadmin, but I dont know how to connect to it.
Should I provide the database username/pass or the Cpanel username/pass for the connection
Please Help. Thanks
As comments have mentioned, many (most!) hosting providers put a firewall between their database servers and the public internet. Because cybercreeps. Attempts to do things that firewalls forbid usually results in the kind of timeout you got.
If you want to connect to your MySQL database from your laptop or some other machine that's outside the provider's firewall, you must ask them to adjust their firewall rule to allow your particular machine to connect. Once the firewall allows your machine to connect, use your database username / password to establish the connection.
Some providers have self-service user interfaces for this, often in their cpanels. Others require you to ask a customer support agent to do it.
I am trying to connect to Teradata using teradatasql module in Python. The code is running fine on localhost, but once deployed on the server as part of the server code, it is throwing the error.
the code:
import teradatasql
try:
host, username, password = 'hostname', 'username', '****'
session = teradatasql.connect(host=host, user=username, password=password, logmech="LDAP")
except Exception as e:
print(e)
Error I am getting on server:
[Version 16.20.0.60] [Session 0] [Teradata SQL Driver] Failure receiving Config Response message header↵ at gosqldriver/teradatasql.
(*teradataConnection).makeDriverError TeradataConnection.go:1101↵ at gosqldriver/teradatasql.
(*teradataConnection).sendAndReceive TeradataConnection.go:1397↵ at gosqldriver/teradatasql.newTeradataConnection TeradataConnection.go:180↵ at gosqldriver/teradatasql.(*teradataDriver).
Open TeradataDriver.go:32↵ at database/sql.dsnConnector.Connect sql.go:600↵ at database/sql.(*DB).conn sql.go:1103↵ at database/sql.
(*DB).Conn sql.go:1619↵ at main.goCreateConnection goside.go:275↵ at main.
_cgoexpwrap_212fad278f55_goCreateConnection _cgo_gotypes.go:240↵ at runtime.call64 asm_amd64.s:574↵ at runtime.cgocallbackg1 cgocall.go:316↵ at runtime.cgocallbackg cgocall.go:194↵ at runtime.cgocallback_gofunc asm_amd64.s:826↵ at runtime.goexit asm_amd64.s:2361↵Caused by read tcp IP:PORT->IP:PORT: wsarecv: An existing connection was forcibly closed by the remote host
The root cause of this error is outlined here by tomnolan:
The stack trace indicates that a TCP socket connection was made to the database, then the driver transmitted a Config Request message to the database, then the driver timed out waiting for a Config Response message from the database.
In other words, the driver thought that it had established a TCP socket connection, but the TCP socket connection was probably not fully successful, because a failure occurred on the initial message handshake between the driver and the database.
The most likely cause is that some kind of networking problem prevented the driver from properly connecting to the database.
I had this issue today and resolved it by altering my host. I am also on a VPN and found that the actual host name in DNS didn't work, but the ALIAS available did. For example on Windows:
C:\WINDOWS\system32>nslookup MYDB-TEST # <-- works
Server: abcd.domain.com
Address: <OMITTED>
Name: MYDB.domain.com # <-- doesn't work
Address: <OMITTED>
Aliases: mydb-test.domain.com # <-- works
I recognize this may be a specific solution option that may not work for everyone, but the root of the problem is confirmed to be a TCP connection issue from my experience.
I am trying to build a tunnel to then connect to an Oracle DB, but tunnel cannot be opened. Error is the following:
ERROR | Problem setting SSH Forwarder up: Couldn't open tunnel localhost:1521 <> XXXXXXXXX:1521 might be in use or destination not reachable.
sshtunnel.HandlerSSHTunnelForwarderError: An error occurred while opening tunnels.
My code is set as:
self.tunnel = sshtunnel.SSHTunnelForwarder((conn_data['gateway'], int(conn_data['gateway_port'])),
ssh_username=conn_data['username'],
ssh_password=password,
remote_bind_address=(conn_data['remote_bind'],
int(conn_data['remote_port'])),
local_bind_address=(conn_data['local_bind'],
int(conn_data['local_port'])))
The code works fine if I am inside the network of the company I work for. But if I am connected through VPN, I get the above error. My guess is that the VPN is built over the same tunnel.
I tried changing the local_port and removing the local bind, but if I do that, I get the error:
cx_Oracle.DatabaseError: ORA-12541: TNS:no listener
So, how can I dynamically set the port of SSHTunnelForwarder so it can access my DB through my already set VPN?
Note: changing the VPN's configuration or not using it is not an option.
Problem solved. The issue was that my VPN was using the same port as me (which caused the first error), and my Oracle connection was pointing to this port also (what caused error ORA-12541).
To solve it, I had to change conn_data['local_port'] to another port and set the port of my oracle connection to this same port:
self.tunnel = sshtunnel.SSHTunnelForwarder((conn_data['gateway'],
int(conn_data['gateway_port'])),
ssh_username=conn_data['username'],
ssh_password=password,
remote_bind_address=(conn_data['remote_bind'], int(conn_data['remote_port'])),
local_bind_address=(conn_data['local_bind'], 1234))
self.connection.connect(conn_data['host'],
port=1234,
username=conn_data['username'],
password=password,
look_for_keys=False)
I'm trying to connect to a postgresql database using the following python code:
try:
conn = psycopg2.connect("host = '10.47.65.237' dbname = 'testDB' user = 'pi' password = 'raspberry'")
except:
print("Unable to connect to testDB at 10.47.65.237. Sending Alert.")
This code works with localhost 127.0.0.1 but when I go to a different machine and try to run it with its ip above it won't connect.
Things I've done:
1. Port 5432 is open
2. edited postgresql.conf by adding the line "listen_addresses='10.47.65.138'"
3. edited pg_hba.conf by adding the following configuration "host all all 10.47.65.138 md5"
Any other things I could try or I'm missing?
Running telnet 10.47.65.237 5432 on the client should result in a Connection Refused error, which indicates that the problem has nothing to do with psycopg2.
You have misconfigured the server. listen_addresses controls which IPs the server will answer on, not which IPs the server will permit connections from. Your server's postgresql.conf should have either listen_addresses='10.47.65.237' or listen_addresses='*'. Edit the configuration and restart PostgreSQL on the server, then you should be able to connect successfully using telnet and psycopg2.
For my Django project I am using 'postgresql_psycopg2' and my DB is residing at a common server on the network. How can I connect to that DB using the IP address as 'host' like we do in MySQL? I have tried but it always shows the following error:
OperationalError at /
could not connect to server: Connection refused
Is the server running on host "" and accepting TCP/IP connections on port 5432?
Your problem is not related to Django, you just need to simply put the database server's ip in DATABASES['default']['host'], as you did.
The problem is postgresql denies remote access by default. You have to first edit the pg_hba.conf file on your database server and put a line like this in it:
host db_name user_name 192.168.1.1/32 md5
where you put your target database and user (same as those you enter in django settings) and specify the ip range of the allowed hosts to connect to that database via that user. Then you restart postgresql and now you can remotely connect to the your database. Also check if there isn't any firewall blocking you to access that port on the database server.
For more detailed instructions, see [1] or [2].