python bad authenticate error to mongodb atlas - python

my code:
import certifi
import ssl
from pymongo import MongoClient
connection = MongoClient("mongodb+srv://<USER>:<PASSWORD>#sensor-cluster-jhiup.mongodb.net/test", ssl_cert_reqs=ssl.CERT_REQUIRED, ssl_ca_certs=certifi.where())
print connection
db = connection.test
print db
items = db.items
print items
db.items.insert_one(
{"item": "canvas"})
I get the following error when trying a simple insert via python 2.7 to mongodb atlas:
MongoClient(host=['sensor-cluster-shard-00-02-jhiup.mongodb.net:27017',
'sensor-cluster-shard-00-00-jhiup.mongodb.net:27017',
'sensor-cluster-shard-00-01-jhiup.mongodb.net:27017'],
document_class=dict, tz_aware=False, connect=True,
ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem',
replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True,
authsource=u'admin')
Database(MongoClient(host=['sensor-cluster-shard-00-02-jhiup.mongodb.net:27017',
'sensor-cluster-shard-00-00-jhiup.mongodb.net:27017',
'sensor-cluster-shard-00-01-jhiup.mongodb.net:27017'],
document_class=dict, tz_aware=False, connect=True,
ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem',
replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True,
authsource=u'admin'), u'test')
Collection(Database(MongoClient(host=['sensor-cluster-shard-00-02-jhiup.mongodb.net:27017',
'sensor-cluster-shard-00-00-jhiup.mongodb.net:27017',
'sensor-cluster-shard-00-01-jhiup.mongodb.net:27017'],
document_class=dict, tz_aware=False, connect=True,
ssl_ca_certs='/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/certifi/cacert.pem',
replicaset=u'Sensor-Cluster-shard-0', ssl_cert_reqs=2, ssl=True,
authsource=u'admin'), u'test'), u'items')
Traceback (most recent call last): File
"/Users/danielmatthews/Python/sunfounderpy3.py", line 18, in
{"item": "canvas"}) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py",
line 682, in insert_one
session=session), File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py",
line 598, in _insert
bypass_doc_val, session) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py",
line 578, in _insert_one
True, _insert_command, session) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py",
line 1099, in _retryable_write
return self._retry_with_session(retryable, func, s, None) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py",
line 1066, in _retry_with_session
with self._get_socket(server) as sock_info: File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py",
line 17, in enter
return self.gen.next() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/mongo_client.py",
line 944, in _get_socket
with server.get_socket(self.all_credentials) as sock_info: File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py",
line 17, in __enter
return self.gen.next() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/server.py",
line 138, in get_socket
with self.pool.get_socket(all_credentials, checkout) as sock_info: File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py",
line 17, in enter
return self.gen.next() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py",
line 893, in get_socket
sock_info.check_auth(all_credentials) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py",
line 588, in check_auth
auth.authenticate(credentials, self) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py",
line 486, in authenticate
auth_func(credentials, sock_info) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py",
line 466, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/auth.py",
line 237, in _authenticate_scram_sha1
res = sock_info.command(source, cmd) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py",
line 496, in command
collation=collation) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py",
line 125, in command
parse_write_concern_error=parse_write_concern_error) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py",
line 146, in _check_command_response
raise OperationFailure(msg % errmsg, code, response) OperationFailure: bad auth Authentication failed.

Related

python request error. sys.exit(-(256 - signum))

try:
r = requests.get(url)
except requests.exceptions.Timeout:
_LOG.info(f"Loop {i} timeout")
continue
I got error as below,
return request(\'get\', url, params=params, **kwargs)\n
File "/venv/lib/python3.6/site-packages/requests/api.py", line 61, in request\n
return session.request(method=method, url=url, **kwargs)\n
File "/venv/lib/python3.6/site-packages/requests/sessions.py", line 542, in request\n resp = self.send(prep, **send_kwargs)\n
File "/venv/lib/python3.6/site-packages/requests/sessions.py", line 655, in send\n r = adapter.send(request, **kwargs)\n
File "/venv/lib/python3.6/site-packages/requests/adapters.py", line 449, in send\n timeout=timeout\n
File "/venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen\n chunked=chunked,\n
File "/venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 382, in _make_request\n self._validate_conn(conn)\n
File "/venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 1010, in _validate_conn\n conn.connect()\n
File "/venv/lib/python3.6/site-packages/urllib3/connection.py", line 421, in connect\n tls_in_tls=tls_in_tls,\n
File "/venv/lib/python3.6/site-packages/urllib3/util/ssl_.py", line 450, in ssl_wrap_socket\n sock, context, tls_in_tls, server_hostname=server_hostname\n
File "/venv/lib/python3.6/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl\n
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)\n
File "/usr/local/lib/python3.6/ssl.py", line 401, in wrap_socket\n _context=self, _session=session)\n
File "/usr/local/lib/python3.6/ssl.py", line 808, in __init__\n self.do_handshake()\n
File "/usr/local/lib/python3.6/ssl.py", line 1061, in do_handshake\n self._sslobj.do_handshake()\n File "/usr/local/lib/python3.6/ssl.py", line 683, in do_handshake\n self._sslobj.do_handshake()\n
File "/venv/lib/python3.6/site-packages/billiard/common.py", line 125, in _shutdown_cleanup\n
sys.exit(-(256 - signum))\n
File "/venv/lib/python3.6/site-packages/billiard/pool.py", line 280, in exit\n
return _exit()\nSystemExit\n'
Can anyone let me know what is the issue above, I am using celery and request, I got ^^ error. I am confused that if this is celery error or request error. Thanks.
Also my sqlalchemy got the same issue !
File "/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 976, in execute\n
return self._execute_text(object_, multiparams, params)\n
File "/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1151, in _execute_text\n parameters,\n
File "/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1288, in _execute_context\n e, statement, parameters, cursor, context\n
File "/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1485, in _handle_dbapi_exception\n util.raise_(exc_info[1], with_traceback=exc_info[2])\n
File "/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_\n raise exception\n
File "/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context\n cursor, statement, parameters, context\n
File "/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 588, in do_execute\n cursor.execute(statement, parameters)\n
File "/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute\n res = self._query(query)\n
File "/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query\n rowcount = self._do_query(q)\n
File "/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query\n self._do_get_result()\n
File "/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 184, in _do_get_result\n self._result = self._get_result()\n
File "/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 408, in _get_result\n return self._get_db().store_result()\n
File "/venv/lib/python3.6/site-packages/billiard/common.py", line 125, in _shutdown_cleanup\n sys.exit(-(256 - signum))\n
File "/venv/lib/python3.6/site-packages/billiard/pool.py", line 280, in exit\n
return _exit()\nSystemExit\n'
If you go this issue, please try to update the celery version, sometimes it is from your deprecated celery.
I successfully solved this issue after updating to celery==4.4.2.
Also the message queue retry sometimes need to be disable from sql, it might cause some wait lock timeout issue if celery unexpected retry.

Getting error sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "pool_pre_ping" in postgres insert

I have declared my connection function as below..
def create_psql_engine(db):
#conn_string = "postgresql+psycopg2://myuser:******#someserver:port/somedb"
conn_string = "postgresql+psycopg2://%s:%s#%s:%s/%s" % (
db.get('user'),
db.get('password'),
db.get('host'),
db.get('port'),
db.get('dbname')
)
conn_args = {
"sslmode": db.get('sslmode'),
"sslcompression": db.get('sslcompression'),
"sslrootcert": db.get('sslrootcert'),
"sslcert": db.get('sslcert'),
"sslkey": db.get('sslkey'),
"pool_pre_ping": True,
"pool_recycle": 300
}
try:
engine = create_engine(conn_string, connect_args=conn_args)
logging.info("sqlalchemy Engine to database created using psycopg2... ")
return engine
except psycopg2.Error as error:
logging.error(error)
And in my program I am connecting as below.
# Create PostgreSQL Engine
self.engine = abc.create_psql_engine(dbParam)
print('PSQL Engine Created to Table : {}'.format(self.tableName))
Then I use a pandas to_sql for inserting into DB
df.to_sql(self.table,self.engine,schema='abc',index=False,if_exists='append',chunksize=100)
It was working all this while with out the below 2 options.
"pool_pre_ping": True,
"pool_recycle": 300
But I was gettin connection interruption when handling large volume. And which is the reason I am trying out a solution with these 2 parameters.
However, I get this error , when I tried. Any clue what I am doing wrong.
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "pool_pre_ping"
Complete error log as below..
Traceback (most recent call last):
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect
return fn()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
self._dec_overflow()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
with_traceback=exc_tb,
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
self.__connect()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
with_traceback=exc_tb,
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/create.py", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/default.py", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib64/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/usr/local/lib64/python3.6/site-packages/psycopg2/extensions.py", line 175, in make_dsn
parse_dsn(dsn)
psycopg2.ProgrammingError: invalid dsn: invalid connection option "pool_pre_ping"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/apps/python/loadtoPostgreSQL.py", line 275, in <module>
MX123D.processFiles(jobIdentifier)
File "/opt/apps/python/loadtoPostgreSQL.py", line 246, in processFiles
df.to_sql(self.table,self.engine,schema='abc',index=False,if_exists='append',chunksize=100)
File "/usr/local/lib64/python3.6/site-packages/pandas/core/generic.py", line 2615, in to_sql
method=method,
File "/usr/local/lib64/python3.6/site-packages/pandas/io/sql.py", line 598, in to_sql
method=method,
File "/usr/local/lib64/python3.6/site-packages/pandas/io/sql.py", line 1393, in to_sql
table.create()
File "/usr/local/lib64/python3.6/site-packages/pandas/io/sql.py", line 721, in create
if self.exists():
File "/usr/local/lib64/python3.6/site-packages/pandas/io/sql.py", line 708, in exists
return self.pd_sql.has_table(self.name, self.schema)
File "/usr/local/lib64/python3.6/site-packages/pandas/io/sql.py", line 1431, in has_table
self.connectable.dialect.has_table, name, schema or self.meta.schema
File "<string>", line 2, in run_callable
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/deprecations.py", line 390, in warned
return fn(*args, **kwargs)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 3074, in run_callable
with self.connect() as conn:
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 3166, in connect
return self._connection_cls(self, close_with_result=close_with_result)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 96, in __init__
else engine.raw_connection()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 3245, in raw_connection
return self._wrap_pool_connect(self.pool.connect, _connection)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 3216, in _wrap_pool_connect
e, dialect, self
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 2070, in _handle_dbapi_exception_noconnection
sqlalchemy_exception, with_traceback=exc_infoÝ2¨, from_=e
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect
return fn()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
self._dec_overflow()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
with_traceback=exc_tb,
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
self.__connect()
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 72, in __exit__
with_traceback=exc_tb,
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/create.py", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib64/python3.6/site-packages/sqlalchemy/engine/default.py", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib64/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/usr/local/lib64/python3.6/site-packages/psycopg2/extensions.py", line 175, in make_dsn
parse_dsn(dsn)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "pool_pre_ping"
(Background on this error at: https://sqlalche.me/e/14/f405)
I myself found the issue.Sharing here so that it can help some one.
Issue was that the parameters were given in wrong place :-)
It should be given as below.
engine = create_engine(conn_string, connect_args=conn_args,pool_pre_ping=True,pool_recycle=300)

Get pymongo error: pymongo.errors.OperationFailure: bad auth : Authentication failed

I have a problem: I wrote python code to communicate with other people through a database that runs on Pymongo. My program was able to connect to the database but could not send files (this is how I tried to get my variable to the database: db.insert_one (msg)). The computer gives me this error:
Traceback (most recent call last):
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1394, in _get_socket
sock_info = self.sockets.popleft()
IndexError: pop from an empty deque
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Luis\PycharmProjects\PresentationGuru2021\main.py", line 133, in <module>
Startup()
File "C:\Users\Luis\PycharmProjects\PresentationGuru2021\main.py", line 63, in Startup
main()
File "C:\Users\Luis\PycharmProjects\PresentationGuru2021\main.py", line 128, in main
db.insert_one(msg)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\collection.py", line 705, in insert_one
self._insert(document,
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\collection.py", line 620, in _insert
return self._insert_one(
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\collection.py", line 609, in _insert_one
self.__database.client._retryable_write(
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1552, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1438, in _retry_with_session
return self._retry_internal(retryable, func, session, bulk)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1462, in _retry_internal
with self._get_socket(server, session) as sock_info:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 117, in __enter__
return next(self.gen)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\mongo_client.py", line 1308, in _get_socket
with server.get_socket(
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 117, in __enter__
return next(self.gen)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1331, in get_socket
sock_info = self._get_socket(all_credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1397, in _get_socket
sock_info = self.connect(all_credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 1297, in connect
sock_info.check_auth(all_credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 820, in check_auth
self.authenticate(credentials)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 837, in authenticate
auth.authenticate(credentials, self)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\auth.py", line 672, in authenticate
auth_func(credentials, sock_info)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\auth.py", line 590, in _authenticate_default
return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\auth.py", line 333, in _authenticate_scram
res = sock_info.command(source, cmd)
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\pool.py", line 710, in command
return command(self, dbname, spec, secondary_ok,
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\network.py", line 158, in command
helpers._check_command_response(
File "C:\Users\Luis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pymongo\helpers.py", line 167, in _check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}
Can someone help me fix this?
Here is the code (without my password from Pymongo):
cluster = MongoClient (
"mongodb + srv: // Luis: <NOPASSWORT HERE> # cluster0.mme3c.mongodb.net / myFirstDatabase? retryWrites = true & w = majority")
db = cluster ["socialMedia"] ["messaging"]
date = datetime.now (). strftime ("% x")
person = username
message = input ("Message:")
time = datetime.now (). strftime ("% X")
msg = {"User": person, "message": message, "Date": date, "Time": time}
print (msg)
db.insert_one (msg)

SQLAlchemy - Broken Pipe ERROR when trying to store large BLOB (i.e. machine learning model) to MySQL database

I would like to serialize many Gensim library Word2Vec models in MySQL database. For the purpose of my project I am utilizing: Python, Flask, SQLAlchemy, and MySQL+PyMySQL. Below you can see my Word2VecModel class which I am using in order to create word2vec table in MySQL DB:
from db.db import db
import db_models.influencer
class Word2VecModel(db.Model):
__tablename__ = "word2vec"
m_id = db.Column(db.Integer, primary_key=True)
m_username = db.Column(db.String(20), db.ForeignKey("influencer.m_username"))
m_binary = db.Column(db.LargeBinary)
influencer = db.relationship("InfluencerModel")
def __init__(self, m_binary, m_username):
self.m_binary = m_binary
self.m_username = m_username
def __repr__(self):
return '<Word2Vec {}>'.format(self.m_name)
#classmethod
def find_by_username(cls, m_username):
return cls.query.filter_by(name=m_username).first()
def save_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
Whenever I try to execute:
word2vec_model = Word2VecModel(pickle.dumps(word2vec), username)
word2vec_model .save_to_db()
I get the following error message:
Traceback (most recent call last):
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 705, in _write_bytes
self._sock.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 773, in _execute_command
self.write_packet(sql[:packet_size])
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 634, in write_packet
self._write_bytes(data)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 710, in _write_bytes
"MySQL server has gone away (%r)" % (e,))
pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 117, in <module>
word2vec.save_to_db()
File "/home/stefan/PycharmProjects/NLP - Influencer Text Analysis/src/flask_api/db_models/word2vec.py", line 25, in save_to_db
db.session.commit()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 163, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1042, in commit
self.transaction.commit()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 504, in commit
self._prepare_impl()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 483, in _prepare_impl
self.session.flush()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2523, in flush
self._flush(objects)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2664, in _flush
transaction.rollback(_capture_exception=True)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2624, in _flush
flush_context.execute()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 1136, in _emit_insert_statements
statement, params
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1014, in execute
return meth(self, multiparams, params)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1133, in _execute_clauseelement
distilled_params,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1318, in _execute_context
e, statement, parameters, cursor, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1512, in _handle_dbapi_exception
sqlalchemy_exception, with_traceback=exc_info[2], from_=e
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 773, in _execute_command
self.write_packet(sql[:packet_size])
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 634, in write_packet
self._write_bytes(data)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 710, in _write_bytes
"MySQL server has gone away (%r)" % (e,))
Killed
I've read somewhere that increasing MySQL connection timeout might help, but not in my case. Here is my DB engine configuration:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:''#localhost/********'
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"pool_recycle": 120}

PyMongo - Server timeout on localhost

I'm trying to learn how to work with MongoDB in Python but everytime I run my code it takes so long that I have to kill it and I get these tracebacks.
"/Volumes/ADATA HD720/flapPY/venv/bin/python" "/Volumes/ADATA HD720/flapPY/Database.py"
Traceback (most recent call last):
File "/Volumes/ADATA HD720/flapPY/Database.py", line 8, in <module>
x = high_scores.insert_one(mydict)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 693, in insert_one
session=session),
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 607, in _insert
bypass_doc_val, session)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 595, in _insert_one
acknowledged, _insert_command, session)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1247, in _retryable_write
with self._tmp_session(session) as s:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1576, in _tmp_session
s = self._ensure_session(session)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1563, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1516, in __start_session
server_session = self._get_server_session()
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1549, in _get_server_session
return self._topology.get_server_session()
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/topology.py", line 427, in get_server_session
None)
File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/topology.py", line 208, in _select_servers_loop
self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 300, in wait
gotit = waiter.acquire(True, timeout)
KeyboardInterrupt
Process finished with exit code 1
This is my code:
import pymongo
client = pymongo.MongoClient("localhost", 27017)
database = client["database"]
high_scores = database["highscore"]
mydict = {"highscore" : 5}
x = high_scores.insert_one(mydict)
print(x.inserted_id)
My belief is that it cannot connect to the localhost
Apparently mongodb was not started on my PC. All I had to do was to run
brew services start mongodb
and it worked perfectly.

Categories