Python MySQL connecting to database error - python

I'm trying to create a small app with Python and MySQL.
I managed to connect Python and MySQL using the MySQL connection. But now when I try to run this script to actually connect to the database:
import mysql.connector
mydb = mysql.connector.connect(
passwd="12345",
db="DB",
host="hostname",
user="root"
)
print(mydb)
I get a lot of errors like:
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 485, in open_connection
socket.SOL_TCP)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
File "c:/Users/Krystian/Desktop/DB/db.py", line 7, in <module>
user="root"
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 94, in __init__
self.connect(**kwargs)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 722, in connect
self._open_connection()
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 207, in _open_connection
self._socket.open_connection()
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\network.py", line 501, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'hostname:3306' (11001 getaddrinfo failed)
I tried other ways of conecting to Database but they didn't work and I'm not really sure what to do now.
Thanks in advance for any help!
EDIT1
As Juergen mentioned, after fixing the hostname most of the errors above are gone, but new ones showed up:
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 94, in __init__
self.connect(**kwargs)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 722, in connect
self._open_connection()
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 211, in _open_connection
self._ssl)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 141, in _do_auth
auth_plugin=self._auth_plugin)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\protocol.py", line 102, in make_auth
auth_data, ssl_enabled)
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\protocol.py", line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "C:\Users\Krystian\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\authentication.py", line 191, in get_auth_plugin
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supporte

I guess, your computer (where the database runs) is not named "hostname":
host="hostname",
If it is the local machine, try localhost. "hostname" is just a placeholder inside example coding. You always have to replace such.
About your edit:
Looks as your db server is misconfigured. Maybe you need that plugin that is named in the error message.

try this :
mysql.connector.connect(
host="localhost",
user="user_name",
passwd="db_password",
database="db_name")

Related

psycopg2.OperationalError: FATAL: sorry, too many clients already on heroku

I tried to push my Flask app to heroku and my app uses Postgres as db. However, when I tried to migrate db on heroku using this command
heroku run python manage.py db init --app app
It gives this "too many clients" error for both psycopg2 and sqlalchchemy.
Running python manage.py db migrate on ⬢ petscom... up, run.5707 (Hobby)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2285, in _wrap_pool_connect
return fn()
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 773, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
rec = pool._do_get()
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 238, in _do_get
return self._create_connection()
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 657, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 652, in __connect
connection = pool._invoke_creator(self)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 488, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: sorry, too many clients already
FATAL: sorry, too many clients already
After a bit googling, I tried:
heroku pg:killall
heroku restart
None of those works.....
And I checked heroku pg, it shows 0/20 connections......
Some said dont put db connection in loop, but I have to check if some data is in db. Here's my code:
try:
connection = psycopg2.connect(user="postgres",
password="pass",
host="127.0.0.1",
port="5432",
database="pets")
cursor = connection.cursor()
select_all = "select * from pets"
cursor.execute(select_all)
# Check if api data in db and save to db
for index in range(len(myData)):
# if not exist in db => save to db
key = myData[index]["animal_id"]
exists = check_exists(key, cursor)
if(exists):
pass
else:
doSomething()
I can connect to my local postgres db but not the db on heroku.
How can I deal with this problem? Any advise would help :) Thanks!
I had a similar problem. I was able to fix it by setting SQLALCHEMY_TRACK_MODIFICATIONS=False in the environment, destroying the database, and creating a new one.
You can destroy it by going to https://data.heroku.com/datastores, opening the database, going to Settings, and selecting 'Destroy Database'.
Then provision a new one as you normally would.

Connect Cloud Run container to Cloud SQL

UPDATE:
I re-forked the GoogleCloudPlatform example project and tried again. Suddenly, it's working.
Half of my problem had to do with the fact that my target project uses Flask-SQLAlchemy. For that, I needed to use the MySQLdb dialect as shown in this answer:
https://stackoverflow.com/a/10900826/4455571
I'm still not sure why I couldn't get the GoogleCloudPlatform example to work the first time and why it suddenly started working after I re-forked.
Original post:
I am trying to connect a container running on Cloud Run to Cloud SQL using the following guide:
https://cloud.google.com/sql/docs/mysql/connect-run?hl=en_US
I have made sure to do the following:
Enable the API for my project
Add the "Cloud SQL Client" role to my
service account REDACTED-compute#developer.gserviceaccount.com
However, the connection fails with the following error:
File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py",
line 630, in connect raise exc sqlalchemy.exc.OperationalError:
(pymysql.err.OperationalError) (2003, "Can't connect to MySQL server
on 'localhost' ([Errno 2] No such File or directory)")
I made a second attempt using the sample code from GitHub:
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/cloud-sql/mysql/sqlalchemy
These are the steps I followed:
Mirrored the repository and made it private
Edited app.yaml to add my credentials similar to the following (I didn't wrap the values in quotes - should I?):
env_variables:
CLOUD_SQL_CONNECTION_NAME: my-project-270323:us-central1:database
DB_USER: root
DB_PASS: areallygreatpassword
DB_NAME: database
Connected the repo to Cloud Build and triggered a new build
Deployed my container in a new service on Cloud Run making sure to select by database under Connections > Cloud SQL connections
I get the same error. What am I doing wrong?
EDIT
Here's the full error dump:
Traceback (most recent call last):()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request()()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1944, in full_dispatch_request self.try_trigger_before_first_request_functions()()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1992, in try_trigger_before_first_request_functions func()()
File "/app/main.py", line 81, in create_tables with db.connect() as conn:()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2209, in connect return self._connection_cls(self, **kwargs)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 103, in __init__ else engine.raw_connection()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2306, in raw_connection return self._wrap_pool_connect(()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2279, in _wrap_pool_connect Connection._handle_dbapi_exception_noconnection(()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1547, in _handle_dbapi_exception_noconnection util.raise_from_cause(sqlalchemy_exception, exc_info)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 152, in reraise raise value.with_traceback(tb)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2276, in _wrap_pool_connect return fn()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 303, in unique_connection return _ConnectionFairy._checkout(self)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 773, in _checkout fairy = _ConnectionRecord.checkout(pool)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 492, in checkout rec = pool._do_get()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get self._dec_overflow()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__ compat.reraise(exc_type, exc_value, exc_tb)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 153, in reraise raise value()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get return self._create_connection()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection return _ConnectionRecord(self)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 437, in __init__ self.__connect(first_connect_check=True)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 652, in __connect connection = pool._invoke_creator(self)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect return dialect.connect(*cargs, **cparams)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 489, in connect return self.dbapi.connect(*cargs, **cparams)()
File "/usr/local/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect return Connection(*args, **kwargs)()
File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 325, in __init__ self.connect()()
File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 630, in connect raise exc sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such()
File or directory)")
Also, I did this via the web console, not the CLI. Here are the settings I passed to the example project provided by Google:
drive.google.com/open?id=17nl_rQVTU2ZirCEu64bjfe90zGweg3a6
drive.google.com/open?id=1_Riy1HNSPvZZUGl4tJ0Z8puYuuMbUMPH
The question includes some wide-ranging research, nicely done. Addressing a couple points directly:
The app.yaml file is specific to App Engine, and is a method to pass Environment Variable configuration to your app. In Cloud Run, you use the --update-env-vars flag on deploy.
You may also be missing a flag in your service deploy operation: --add-cloudsql-instances INSTANCE-CONNECTION-NAME
These steps together:
gcloud run deploy SERVICE --image gcr.io/PROJECT/SERVICE \
--add-cloudsql-instances my-project-270323:us-central1:database
--update-env-vars CLOUD_SQL_CONNECTION_NAME=my-project-270323:us-central1:database \
--update-env-vars DB_USER=root \
--update-env-vars DB_PASS=areallygreatpassword \
--update-env-vars DB_NAME=database
Note that database in the INSTANCE_CONNECTION_NAME is the Cloud SQL instance, while database as the DB_NAME is a database created within that instance.
The configuration values need to be used as part of creating your database connection.
You can see more of the connection between these environment variables and creating the connection object in the complete sample code on Github.

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.

should I close each connection to the DB in rethinkDB?

I have a server built in python, using Tornado and RethinkDB. Its been running for a long time, opening several connections to the DB per user. I never had any major problems by leaving them open -never closing them-. But checking out the log file I found out there's a lot of this warnings:
File "/home/bundleroot/commentserver/app.py", line 30, in <module>
db_connection = r.connect(RDB_HOST,RDB_PORT)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 542, in connect
return conn.reconnect(timeout)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 475, in reconnect
return self._instance.connect(timeout)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 360, in connect
self._socket = SocketWrapper(self, timeout)
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 268, in __init__
(self.host, self.port, ex))
rethinkdb.errors.RqlDriverError: Could not connect to localhost:28015. Error: [Errno 111] Connection refused
Traceback (most recent call last):
File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 244, in __init__
socket.create_connection((self.host, self.port), timeout)
File "/usr/lib/python3.4/socket.py", line 509, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 500, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Should I worry about it? In that case, what are the good practices?
Yes, you need to close the connection to avoid connection leaks. This Flask example, from RethinkDB, does it:
https://github.com/rethinkdb/rethinkdb-example-flask-backbone-todo/blob/master/todo.py#L47-L65
Even if RethinkDB does not enforce a hard limit on number of connections (I'm not sure if it does or not), you can run into the limits of the OS.

Categories