Error while connecting to DB using PyMySQL - python

I get the following error while connecting to the database.
Traceback (most recent call last):
File "PycharmProjects/test.py", line 13, in <module>
connection = pymysql.connect(host=serverip,user=dbuser,password=dbpassword,db=dbname,port=dbport)
File "PycharmProjects\untitled\venv\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "PycharmProjects\untitled\venv\lib\site-packages\pymysql\connections.py", line 325, in __init__
self.connect()
File "PycharmProjects\untitled\venv\lib\site-packages\pymysql\connections.py", line 589, in connect
self.host_info = "socket %s:%d" % (self.host, self.port)
TypeError: %d format: a number is required, not str
Following is my code:
serverip = "1.2.3.4"
dbuser="root"
dbpassword="passwrd"
dbname="db"
dbport="3306"
connection = pymysql.connect(host=serverip,user=dbuser,password=dbpassword,db=dbname,port=dbport)
def main():
cur = connection.cursor()
cur.execute("SELECT VERSION()")
data = cur.fetchone()
print(data)
# disconnect from server
connection.close()
if __name__ == "__main__":
main()
Even if I try changing the hostname to 'test.aws.com' I get the similar error.

Related

Connectin Clear Db in heroku application with mysql.connector

I am trying to connect to ClearDb with Heroku from python app I use mysql.connector
My code look like this:
conn = mysql.connector.connect(
host="clearDbHost",
user="123qwe",
password="123qwe",
database="heroku_4fsdfsdf30daf8",
port=3306,
autocommit = True
)
curs = conn.cursor()
Is connection crating the tables, but after this is disconnect and can't execute curs.execute() command later in the code I got this error:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 535, in cmd_query
self._cmysql.query(query,
_mysql_connector.MySQLInterfaceError: Lost connection to MySQL server during query
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/app/twitter.py", line 855,
in
getMyFollowersToDatabse() File "/app/twitter.py", line 186, in getMyFollowersToDatabse
curs.execute("INSERT INTO users (username, follow_id) VALUES ('" + user.username + "', '" + user.id + "')") File
"/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/cursor_cext.py",
line 269, in execute
result = self._cnx.cmd_query(stmt, raw=self._raw, File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py",
line 540, in cmd_query
raise errors.get_mysql_exception(exc.errno, msg=exc.msg, mysql.connector.errors.OperationalError: 2013 (HY000): Lost connection
to MySQL server during query
I see in the Clear Db like is havin ?reconnect=true in the end of the database name.I am not using curs.close()

Problems executing a MySQL query in python

I have problems when executing a method that allows me to eliminate a box in my program, this method is in charge of first eliminating everything that is inside the box and then it eliminates the box, this to avoid conflicts when dealing with foreign keys.
Here is my configuration for the connection:
import mysql.connector
connMySQL = mysql.connector.connect(
host='localhost',
db=wmszf,
user=root,
passwd='',
)
Here is the method:
def deleteBoxComplete(idBox):
cursor = connMySQL.cursor()
cursor.execute('FLUSH QUERY CACHE;')
cursor.close()
cursor = connMySQL.cursor()
cursor.execute(queryDelAllRefInBox(idBox))
connMySQL.commit()
cursor.close()
cursor = connMySQL.cursor()
cursor.execute(queryDeleteBox(idBox))
connMySQL.commit()
cursor.close()
You may notice that I clear the cache, as it is my priority to get the most up-to-date information possible.
Then I leave the query "queryDelAllRefInBox(idBox)":
DELETE FROM
picking_boxitem
WHERE
idBox_id = """+idBox+""";
Then I leave the query "queryDeleteBox(idBox)":
DELETE FROM
picking_box
WHERE
idBox = """+idBox+""";
The problem when executing the "deleteBoxComplete(idBox)" method is that it suddenly closes the connection with the database, it does so arbitrarily, sometimes yes, sometimes not, why does this happen? How can I prevent it? Is there a good practice that allows me to better execute this type of instructions?
Here is the output corresponding to the error:
Traceback (most recent call last):
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\USUARIO\Desktop\Projects\Produccion\wms\wms\picking\views.py", line 160, in listBoxesInPicking
boxes = getAllBoxInPicking(id)
File "C:\Users\USUARIO\Desktop\Projects\Produccion\wms\wms\MySQL\views.py", line 385, in getAllBoxInPicking
cursor = connMySQL.cursor()
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 809, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.
Like other times this can come out:
Traceback (most recent call last):
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\USUARIO\Desktop\Projects\Produccion\wms\wms\picking\views.py", line 160, in listBoxesInPicking
boxes = getAllBoxInPicking(id)
File "C:\Users\USUARIO\Desktop\Projects\Produccion\wms\wms\MySQL\views.py", line 383, in getAllBoxInPicking
cursor.execute(queryGetAllBoxInPicking(idPicking))
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 384, in _handle_result
elif packet[4] == 0:
IndexError: bytearray index out of range
I appreciate your collaboration.
Additional things I've tried after no response
After looking for information, I found that the mysql engine probably works with much less performance than I imagined when cleaning the cache, so I chose to make a different configuration, instead of making a simple connection I decided to make a "pool connection" for the connections to be managed, the new configuration is as follows:
from django.conf import settings
from mysql.connector import Error
from mysql.connector import pooling
poolname="mysqlpool"
varHost='localhost'
varUser=settings.DATABASES['default']['USER']
varPasswd=settings.DATABASES['default']['PASSWORD']
varDB=settings.DATABASES['default']['NAME']
try:
connection_pool = pooling.MySQLConnectionPool(
pool_name="pynative_pool",
pool_size=10,
pool_reset_session=True,
host=varHost,
database=varDB,
user=varUser,
password=varPasswd)
print("Printing connection pool properties ")
print("Connection Pool Name - ", connection_pool.pool_name)
print("Connection Pool Size - ", connection_pool.pool_size)
connection_object = connection_pool.get_connection()
if connection_object.is_connected():
db_Info = connection_object.get_server_info()
print("Connected to MySQL database using connection pool ... MySQL Server version on ", db_Info)
cursor = connection_object.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("Your connected to - ", record)
except Error as e:
print("Error while connecting to MySQL using Connection pool ", e)
finally:
if connection_object.is_connected():
db_Info = connection_object.get_server_info()
print("Connected to MySQL database using connection pool ... MySQL Server version on ", db_Info)
cursor = connection_object.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("Your connected to - ", record)
However, I got the error that I got before, then I leave the traceability of the error:
Internal Server Error: /picking/listReferencesInBox/179/
Traceback (most recent call last):
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\USUARIO\Desktop\Projects\Produccion\wms\wms\picking\views.py", line 250, in listReferencesInBox
references = getReferencesInBoxMonitor(id)
File "C:\Users\USUARIO\Desktop\Projects\Produccion\wms\wms\MySQL\views.py", line 279, in getReferencesInBoxMonitor
cursor.execute(queryGetReferencesInBoxMonitor(idBox))
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Users\USUARIO\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 384, in _handle_result
elif packet[4] == 0:
IndexError: bytearray index out of range
The error occurs to me with the same frequency as before.

Python and MySQL connector - Connection timed out error

I have a Python script which connects to my database, gets all the users' hashes and their emails, then parses through those hashes and gets some other data from the DB based on the user's hash value.
The problem is, that my MySQL Python connector breaks at various points and gives me this exception:
Traceback (most recent call last):
File "/home/antonio/.local/lib/python3.9/site-packages/mysql/connector/network.py", line 509, in open_connection
self.sock.connect(sockaddr)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/antonio/.local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/antonio/.local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/antonio/.local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/antonio/.local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/antonio/.local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/antonio/.local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/antonio/Desktop/PythonProjects/crypton-portfolio-api/crypto_tracking_coins/main_with_flask.py", line 701, in get_all_users_with_portfolio_and_accounting_data
user_coins_from_db = perform_db_query_fetchall('SELECT `portfolio`, `date`, `type`, `invested` FROM `users` WHERE `hash` = %s AND `fake` = "no"', (user_hash, ))
File "/home/antonio/Desktop/PythonProjects/crypton-portfolio-api/crypto_tracking_coins/helpers.py", line 6, in perform_db_query_fetchall
mydb = mysql.connector.connect(
File "/home/antonio/.local/lib/python3.9/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/home/antonio/.local/lib/python3.9/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/home/antonio/.local/lib/python3.9/site-packages/mysql/connector/abstracts.py", line 716, in connect
self._open_connection()
File "/home/antonio/.local/lib/python3.9/site-packages/mysql/connector/connection.py", line 206, in _open_connection
self._socket.open_connection()
File "/home/antonio/.local/lib/python3.9/site-packages/mysql/connector/network.py", line 511, in open_connection
raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (timed out)
My helpers.py file, which includes all the logic for performing SQL queries, looks like this:
import mysql.connector
from config import *
def perform_db_query_fetchall(query: str, params: tuple):
# Initiate DB connection
mydb = mysql.connector.connect(
host=DatabaseConfig.host_db,
user=DatabaseConfig.user_db,
password=DatabaseConfig.password_db,
database=DatabaseConfig.database_db
)
# Initiate DB cursor
c = mydb.cursor()
# Execute SQL query and get the results
c.execute(query, params)
results = c.fetchall()
# Close the DB connection
c.close()
mydb.close()
# Return the results
return results
def perform_db_query_fetchone(query: str, params: tuple):
# Initiate DB connection
mydb = mysql.connector.connect(
host=DatabaseConfig.host_db,
user=DatabaseConfig.user_db,
password=DatabaseConfig.password_db,
database=DatabaseConfig.database_db
)
# Initiate DB cursor
c = mydb.cursor()
# Execute SQL query and get the results
c.execute(query, params)
results = c.fetchone()
# Close the DB connection
c.close()
mydb.close()
# Return the results
return results
def perform_db_query_with_commit(query: str, params: tuple):
# Initiate DB connection
mydb = mysql.connector.connect(
host=DatabaseConfig.host_db,
user=DatabaseConfig.user_db,
password=DatabaseConfig.password_db,
database=DatabaseConfig.database_db,
autocommit=True
)
# Initiate DB cursor
c = mydb.cursor()
# Execute SQL query and get the results
c.execute(query, params)
# Close the DB connection
c.close()
mydb.close()
return
I'm looping with a for loop (about 1900 iterations of the loop) and doing approximately as twice as many SQL queries in that for loop.
It always ends not finishing the job that I need the script to do with just printing out the above mentioned Connection Timed Out exception.
Interesting fact is, that it always crashes on different stage. The last I tried, it failed on the 1600th~ iteration. Sometimes it fails on the 30th~ iteration...
Any ideas what can I do to fix it?
Thank you.
This can happen if the database server is running out of free space. In my case the database server was full.

Hive - python connection error

Here is code :
#!/usr/bin/env python
import pyhs2
try:
with pyhs2.connect(host='localhost',
port=10001,
authMechanism="PLAIN",
user='root',
password='test',
database='test') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()
#Execute query
cur.execute("select * from raw_stats")
#Return column info from query
print cur.getSchema()
#Fetch table results
for i in cur.fetch():
print i
except Thrift.TException, tx:
print '%s' % (tx.message)
Error!
Traceback (most recent call last): File "/usr/local/py/test.py", line
8, in database='default') as conn: File
"/usr/lib/python2.6/site-packages/pyhs2/init.py", line 7, in
connect
return Connection(*args, **kwargs) File "/usr/lib/python2.6/site-packages/pyhs2/connections.py", line 46, in
init
transport.open() File "/usr/lib/python2.6/site-packages/pyhs2/cloudera/thrift_sasl.py", line
55, in open
self._trans.open() File "/usr/lib64/python2.6/site-packages/thrift/transport/TSocket.py", line
101, in open
message=message) thrift.transport.TTransport.TTransportException: Could not connect to localhost:10001
It resolved by starting hiveServer2 service and changing the port 10000.

Python, MySQL and a weird error

I have a bug that I don't know how to fix or even reproduce:
query = "SELECT id, name FROM names ORDER BY id"
results = database.execute(query)
where the class Database contains:
def execute(self, query):
cursor = self.db.cursor()
try:
cursor.execute(query)
return cursor.fetchall()
except:
import traceback
traceback.print_exc(file=debugFile)
return []
This is how I open the database connection:
self.db = MySQLdb.connect(
host=mysqlHost,
user=mysqlUser,
passwd=mysqlPasswd,
db=mysqlDB
)
This is the stacktrace of the error:
File "foo.py", line 169, in application results = config.db.execute(query)
File "Database.py", line 52, in execute
return cursor.fetchall()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 340, in fetchall
self._check_executed()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 70, in _check_executed
self.errorhandler(self, ProgrammingError, "execute() first")
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: execute() first
Do you have any ideas of why this is happening and how can I fix it? I searched on the internet and I found out that the reason may be having 2 cursors, but I have only one.
try this in your traceback it's for debugging:
except ProgrammingError as ex:
if cursor:
print "\n".join(cursor.messages)
# You can show only the last error like this.
# print cursor.messages[-1]
else:
print "\n".join(self.db.messages)
# Same here you can also do.
# print self.db.messages[-1]

Categories

Resources