Can't connect to RDS database with PyMySQL - python

I'm trying to connect to an RDS database in AWS but i get the following traceback:
File "/Users/andreschaves/miniconda3/lib/python3.8/site-packages/pymysql/connections.py", line 613, in connect
sock = socket.create_connection(
File "/Users/andreschaves/miniconda3/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/Users/andreschaves/miniconda3/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Server.py", line 133, in <module>
main(sys.argv)
File "Server.py", line 31, in main
cur, connection = start_connection(host, user, psswd, db_name)
File "Server.py", line 48, in start_connection
connection = pymysql.connect(host=host, user=user, password=psswd)
File "/Users/andreschaves/miniconda3/lib/python3.8/site-packages/pymysql/connections.py", line 353, in __init__
self.connect()
File "/Users/andreschaves/miniconda3/lib/python3.8/site-packages/pymysql/connections.py", line 664, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '**********' (timed out)")
When i created the database i check that i had public access.
This is the code i'm using for the connection:
connection = pymysql.connect(host=host, user=user, password=psswd, database=db_name)

Related

Celery send task to rabbitmq [Errno 111] Connection refused

I'am trying to send task from django local app too remote rabbitmq server (preprod through vpn) but when I'm trying to send it always same error occure. Two errors code are related [Errno -9] Address family for hostname not supported and [Errno 111] Connection refused. The port 5672 is open the user is not the default user and the broker url is amqp://dist:dist#amqp.nopow-link.com:5672.
If someone can help me to understand what I'm doing wrong.
From the client:
>>> task.hello_task.apply_async()
Traceback (most recent call last):
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/utils/functional.py", line 30, in __call__
return self.__value__
AttributeError: 'ChannelPromise' object has no attribute '__value__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/amqp/transport.py", line 173, in _connect
host, port, family, socket.SOCK_STREAM, SOL_TCP)
File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -9] Address family for hostname not supported
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 447, in _reraise_as_library_errors
yield
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 438, in _ensure_connection
callback, timeout=timeout
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/utils/functional.py", line 312, in retry_over_time
return fun(*args, **kwargs)
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 878, in _connection_factory
self._connection = self._establish_connection()
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 813, in _establish_connection
conn = self.transport.establish_connection()
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/transport/pyamqp.py", line 201, in establish_connection
conn.connect()
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/amqp/connection.py", line 323, in connect
self.transport.connect()
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/amqp/transport.py", line 113, in connect
self._connect(self.host, self.port, self.connect_timeout)
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/amqp/transport.py", line 184, in _connect
"failed to resolve broker hostname"))
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/amqp/transport.py", line 197, in _connect
self.sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/celery/app/task.py", line 579, in apply_async
**options
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/celery/app/base.py", line 788, in send_task
amqp.send_task_message(P, name, message, **options)
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/celery/app/amqp.py", line 519, in send_task_message
**properties
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/messaging.py", line 180, in publish
exchange_name, declare, timeout
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 524, in _ensured
return fun(*args, **kwargs)
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/messaging.py", line 186, in _publish
channel = self.channel
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/messaging.py", line 209, in _get_channel
channel = self._channel = channel()
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/utils/functional.py", line 32, in __call__
value = self.__value__ = self.__contract__()
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/messaging.py", line 225, in <lambda>
channel = ChannelPromise(lambda: connection.default_channel)
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 896, in default_channel
self._ensure_connection(**conn_opts)
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 438, in _ensure_connection
callback, timeout=timeout
File "/usr/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/home/callmarl/env/nopow_link/website/application/lib/python3.7/site-packages/kombu/connection.py", line 451, in _reraise_as_library_errors
raise ConnectionError(str(exc)) from exc
kombu.exceptions.OperationalError: [Errno 111] Connection refused
On the server:
user
Listing permissions for user "dist" ...
/ .* .* .*
port
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 115995/beam.smp
On server nothing append when listening with tcpdump on port 5672 and no logs are displayed too (log.file.level = debug)
Notice: If I'am sending the same task from the server it's working well.
[2022-05-04 13:24:12,678: INFO/MainProcess] Task core.task.hello_task[a34f9869-03b8-4f4b-8139-b56f90deb469] received
[2022-05-04 13:24:12,680: WARNING/ForkPoolWorker-1] You want me to say hello?
[2022-05-04 13:24:12,680: WARNING/ForkPoolWorker-1] Hello.
[2022-05-04 13:24:12,680: INFO/ForkPoolWorker-1] Task core.task.hello_task[a34f9869-03b8-4f4b-8139-b56f90deb469] succeeded in 0.0006248119752854109s: None

Unable to Connect to database in phpmyadmin through Python

I'm trying to connect to database of https://www.freesqldatabase.com but it's not connecting through given code.
But as I connect to my database through phpmyadmin it connects.
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pass',
host='host',
database='database',
port='3306')
try:
cursor = cnx.cursor()
cursor.execute("""
select * FROM USERS
""")
result = cursor.fetchall()
print(result)
finally:
cnx.close()
Everytime I run this code I get these errors.
Traceback (most recent call last):
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\network.py", line 482, in open_connection
addrinfos = socket.getaddrinfo(self.server_host,
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "as.py", line 2, in <module>
cnx = mysql.connector.connect(user='user', password='pass',
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect
self._open_connection()
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 206, in _open_connection
self._socket.open_connection()
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\network.py", line 500, in open_connection
raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host:3306' (11001 getaddrinfo failed)

I tried to make my first database and I got error

I just started learning MySql for Python on W3Schools. I copied the code that was given as an example and I run it in IDLE, but I got an error.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="myusername",
passwd="mypassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
Here is the full traceback:
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 509, in open_connection
self.sock.connect(sockaddr)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/risto/AppData/Local/Programs/Python/Python37-32/mysql1.py", line 6, in <module>
passwd="mypassword"
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect
self._open_connection()
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 206, in _open_connection
self._socket.open_connection()
File "C:\Users\risto\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 512, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
Can someone help me?
the service about MySQL is not running, or filtered by some firewall rule.
Check it out

After connecting to remote database, migrating gives error: Errno61, Connection Refused

I recently started using sql and connected to an remote sql server. I can run queries on it in python shell and get correct results. However, when I run
manage.py runserver
It tells me that there I need to migrate, however when I migrate I get an error saying:
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '*host*' ([Errno 61] Connection refused)")
What should I do to fix this error.
setting.py database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '*name*',
'USER': '*user*',
'PASSWORD': '********',
'HOST': '*host*',
'PORT': '****',
'OPTIONS': {
'sql_mode': 'traditional',
}
}
}
full error list here:
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/pymysql/connections.py", line 583, in connect
**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 233, in get_new_connection
return Database.connect(**conn_params)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/pymysql/connections.py", line 630, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'masspike.ctqk1lhawxna.us-west-2.rds.amazonaws.com' ([Errno 61] Connection refused)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/management/base.py", line 361, in execute
self.check()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 64, in _run_checks
issues = run_checks(tags=[Tags.database])
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
issues.extend(conn.validation.check(**kwargs))
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
issues.extend(self._check_sql_mode(**kwargs))
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
with self.connection.cursor() as cursor:
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/base/base.py", line 256, in cursor
return self._cursor()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/base/base.py", line 233, in _cursor
self.ensure_connection()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 233, in get_new_connection
return Database.connect(**conn_params)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/pymysql/connections.py", line 325, in __init__
self.connect()
File "/Users/lwyss/PycharmProjects/NewWebsite/lib/python3.7/site-packages/pymysql/connections.py", line 630, in connect
raise exc
The error comes from the MySQLConnector
"Can't connect to MySQL server on 'masspike.ctqk1lhawxna.us-west-2.rds.amazonaws.com' ([Errno 61] Connection refused)"
So this is a networking problem in which your application is unable to establish a TCP connection to your database. Maybe because of a lack of configuration on your RDS instance? wrong hostname? wrong port? firewall (as mentioned by #Willem van Onsem)? Maybe the Amazon AWS guide on connecting to their database service helps you
In any case it has nothing to do with your program code, apart from the connection string if its wrong.

OperationalError Could Not Connect To Server -- Django/Heroku/AWS/Postgres

Im having an odd problem when trying to run my website on my local machine. If I type in python manage.py runserver, I get the following error and my site won't load.
I tried to restart my Postgres database on my local machine and restarted the computer but having the same problems.
My site is built in Python/Django hosted on Heroku with files served via Cloudfront/AWS.
System check identified 1 issue (0 silenced).
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x072D7780>
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
self.connect()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\postgresql\base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "ec2-107-20-188-239.compute-1.amazonaws.com" (107.20.188.239) and accepting
TCP/IP connections on port 5432?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\runserver.py", line 123, in inner_run
self.check_migrations()
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 427, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\loader.py", line 206, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\recorder.py", line 61, in applied_migrations
if self.has_table():
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\recorder.py", line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 255, in cursor
return self._cursor()
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 232, in _cursor
self.ensure_connection()
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
self.connect()
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
self.connect()
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\postgresql\base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "ec2-107-20-188-239.compute-1.amazonaws.com" (107.20.188.239) and accepting
TCP/IP connections on port 5432?

Categories