I have a crawler written in Python, which can't connect to the database after crawling for a while. Sometimes it works for 10 minutes, after which the following error log appears:
(2003, "Can't connect to MySQL server on 'localhost' (10055)")
(2003, "Can't connect to MySQL server on 'localhost' (10055)")
Traceback (most recent call last):
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Crawlers\Zanox.py", line 73, in <module>
c.main()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Crawlers\Zanox.py", line 38, in main
self.getInfo()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Crawlers\Zanox.py", line 69, in getInfo
comparator.main()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\CrawlerHelpScripts\Comparator.py", line 23, in main
self.compare()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\CrawlerHelpScripts\Comparator.py", line 36, in compare
deliveryInfo = self.db.getDeliveryInfo()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Database\dell.py", line 29, in getDeliveryInfo
result = self.db.select(com, vals)
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Database\Database.py", line 24, in select
self.con.close()
_mysql_exceptions.ProgrammingError: closing a closed connection
So at a certain point it just can't connect to the database, running on localhost, and afterwards produces a ProgrammingError. Handling this exception is not the problem, because it will keep running but also will keep producing the Can't connect Error.
Here is the code I use for inserting/selecting in my database:
def select(self, com, vals):
try:
self.con = mdb.connect(self.host, self.user, self.password, self.database)
cur = self.con.cursor()
cur.execute(com,vals)
ver = cur.fetchall()
return ver
except mdb.Error as e:
print e
finally:
if self.con:
self.con.close()
def insert(self, com, vals):
try:
self.con = mdb.connect(self.host, self.user, self.password, self.database)
cur = self.con.cursor()
cur.execute(com, vals)
self.con.commit()
except mdb.Error as e:
print e
finally:
if self.con:
self.con.close()
Also, the crawler is NOT multi-threaded. Any ideas why it keeps losing connection?
EDIT: It seems that the crawler works fine untill it has inserted about 15.000 records in the database. If the table holds about 15.000 records, the crawler produces the error a lot quicker.
Related
I have a python script that accesses a mysql database to get the most recent request added to the database and place that request in a queue. The way I want the script to run is that the function check_requests runs in an infinite while loop and constantly checks the mysql database for new requests that have been added, and then places those requests in a queue. The other part of the script then takes each request in the queue and processes them using some functions. I want both of these processes to run at the same time until there are no longer any requests in the queue after which both the script, and the thread that i've created for checking database requests stop. Right now nothing seems to happen and I get an error message. How can I fix it?
q = Queue()
def check_requests():
global q
global requests_queue
while True:
mydb = mysql.connector.connect(user='****',
password='****',
host='****',
database='****',
port ='****')
mycursor = mydb.cursor()
mycursor.execute("""
SELECT request
FROM table
ORDER BY id DESC LIMIT 1;
""")
myresult = mycursor.fetchall()
for data in myresult:
request = data[0]
q.put(request)
if __name__ == "__main__":
thread1 = Thread(target=check_requests)
thread1.start()
while not q.empty():
time.sleep(3)
item = q.get()
print(item)
Exception in thread Thread-1 (check_requests):
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 263, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on '127.0.0.1:3306' (10048)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "c:\Users\Random\Desktop\container_content\checkdb.py", line 18, in check_requests
mydb = mysql.connector.connect(user='root',
File "C:\Python310\lib\site-packages\mysql\connector\pooling.py", line 286, in connect
return CMySQLConnection(*args, **kwargs)
File "C:\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 101, in __init__
self.connect(**kwargs)
File "C:\Python310\lib\site-packages\mysql\connector\abstracts.py", line 1095, in connect
self._open_connection()
File "C:\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 268, in _open_connection
raise get_mysql_exception(
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (10048)
You are connecting, mysql db with every request. Use a single connection and use that connection object everywhere. Please refer sample code.
def connect_db(con=None):
if con is not None:
if not con._closed:
return con
try:
con = pymysql.connect(host=***,
user=***,
password=***,
db=***,
cursorclass=pymysql.cursors.DictCursor
)
return con
except:
con = pymysql.connect(host=***,
user=***,
password=***,
db=***,
cursorclass=pymysql.cursors.DictCursor
)
return con
def func():
global con
con = connect_db(con)
cur = con.cursor()
query = """SELECT * FROM table WHERE condition"""
try:
cur.execute(query)
result = cur.fetchone()
cur.close()
return result
fixed: just using mysql.connector package now.
i am a few programming with python now and i wanted to create a use login/logout system with a database linked to a self created web platform for managment, logging, etc...
now i wanted to perform a query to get all users from my database but for some reason im not able to get any results i tried:
# as requested, connector method.
def initiate_connection(self):
return MySQLdb.connect("localhost", "root", "", "tester")
# This works !
def get_database_version(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT VERSION();")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
# This doesnt? :(
def get_users(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT name FROM users")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
But i get an uknown column error, so i tried selecting everything to see what i get from that result: Nonetype, Also ! i am able to retrieve version from database so i assume im connected properly.
Im pretty clueless in what im doing wrong here any ideas?
Also db structure is:
db->tester
table->users
- id
- name
- password
- salt
- email
Edit:
Actual error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 25, in <module>
print(database_handler().get_users())
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 18, in get_users
curs.execute("SELECT name FROM users")# Query command
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
res = self._query(query)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
db.query(q)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\connections.py", line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'name' in 'field list'")
I trying to import my csv file into database. but it fails.
# -*- coding: utf-8 -*-
import MySQLdb
class Database:
def __init__(self):
self.host = 'localhost'
self.user = 'root'
self.port = 3306
self.password = 'root'
self.db = 'test'
self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db, self.port, local_infile = 1)
self.cursor = self.connection.cursor()
def insert_csv_test(self):
query = "LOAD DATA LOCAL INFILE ‘/Users/ankr/Desktop/output’ INTO TABLE details FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’"
self.cursor.execute(query)
self.connection.commit()
self.connection.close()
print("Done")
def close_connection(self):
self.connection.close()
database = Database()
database.__init__()
database.insert_csv_test()
database.close_connection()
It fails. Seeing this below.
Traceback (most recent call last): File "test.py", line 30, in
database.insert_csv_test() File "test.py", line 20, in insert_csv_test
self.cursor.execute(query) File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/MySQLdb/cursors.py",
line 202, in execute
self.errorhandler(self, exc, value) File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/MySQLdb/connections.py",
line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near
'\xe2\x80\x98/Users/ankr/Desktop/output\xe2\x80\x99 INTO TABLE details
FIELDS TERMINATED BY \xe2\x80\x98,\xe2\x80\x99 LI' at line 1")
Any Help would be appreciated.
This may be a little naive answer, but I think the problem lies in ‘ character. It's getting interpreted as a UTF-8 character. Try replacing it with a regular single quote - ' .
Looks like you at least have a problem in the call. You are connecting to the database twice:
database = Database()
database.__init__()
You should just run:
database = Database()
You should be using \' inside the SQL query (not ') since you want to avoid them being directly interpreted as mentioned already in another comment.
I am currently trying to send data to my database that is hosted on the web using python. There are no MySql errors being thrown however running in ninja I get this output:
Traceback (most recent call last):
File "/home/nekasus/tryme.py", line 8, in <module>
port = "3306") # name of the data base
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required
This is my code:
import fakevalues
import MySQLdb
fakevalues.randomnumber()
db = MySQLdb.connect(host="sql8.freemysqlhosting.net",
user="sql8167767",
passwd="placeholder",
db="sql8167767",
port = "3306")
cur = db.cursor()
try:
cur.execute("INSERT INTO hydroponics (ph, electricalconductivity, nutrientsolutiontemperature, nutrientsolutiondepth) VALUES (1, 1, 1, 1)")
except MySQLdb.Error as e:
print(e)
db.close()
You are missing the commit.
db.commit()
port needs to be an integer, not a string.
I'have a little problem when I want to connect to my DB with psycopg2 and python. I have this little script :
#!/usr/bin/python3
import time
import psycopg2
import sys
def main():
# Get a connection
conn = psycopg2.connect(database='my_db', host='10.10.2.1', port='5433', user='me', password='my_password_that_i_dont_show_here')
# conn.cursor will return a cursor object, to perform queries
cursor = conn.cursor()
# Execute our query
cursor.execute("select date(created_at), email, firstname, lastname, locale from users where date(created_at) = current_date;")
# retrieve the records from the database
records = cursor.fetchall()
print(records)
if __name__ == "__main__":
main()
That worked well on windows but now I'm on ubuntu and I have this error :
Traceback (most recent call last):
File "Bureau/script.py", line 29, in <module>
main()
File "Bureau/script.py", line 15, in main
conn = psycopg2.connect(database='my_db', host='10.10.2.1', port='5433', user='me', password='best_password_ever')
File "/usr/local/lib/python3.5/dist-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection timed out
Is the server running on host "10.10.2.1" and accepting
TCP/IP connections on port 5433?