我有以下内容:
class FooData(object):
def __init__(self):
...
try:
self.my_cnf = os.environ['HOME'] + '/.my.cnf'
self.my_cxn = mysql.connector.connect(option_files=self.my_cnf)
self.cursor = self.my_cxn.cursor(dictionary=True)
except mysql.connector.Error as err:
if err.errno == 2003:
self.my_cnf = None
self.my_cxn = None
self.cursor = None
我能够使用my_cxn
和cursor
而没有任何明显的失败。我从未明确终止连接,并观察到以下信息在我的mysql错误日志中...
2017-01-08T15:16:09.355190Z 132 [Note] Aborted connection 132 to db:
'mydatabase' user: 'myusername' host: 'localhost'
(Got an error reading communication packets)
我这样做是不是有问题?每次运行查询时,初始化连接器和游标是否更有效率?
在mysql配置中,我需要注意哪些内容以避免连接中断?
此外,我经常在错误日志中看到以下消息:
2017-01-06T15:28:45.203067Z 0 [Warning] Changed limits: max_open_files: 1024
(requested 5000)
2017-01-06T15:28:45.205191Z 0 [Warning] Changed limits: table_open_cache: 431
(requested 2000)
这与上述内容有关吗?它是什么意思,我该如何解决它?
我尝试了各种解决方案,包括/lib/systemd/system/mysql.service.d/limits.conf和其他配置设置,但是我无法让它们中的任何一个起作用。
cursor.close()
,并且还需要显式调用my_cxn.close()
。有没有一种好的方法将mysql.connector连接集成到Python类中,以便不会创建不必要的连接到服务器,并负责关闭连接而无需明确指定?在使用with
时,使用__enter__
和__exit__
是实现 mysql.connector 类的良好思路,并确保适当清除吗? - Vishal