@@ -28,11 +28,12 @@ def __init__(self, *args, **kwargs):
2828 def configure (self , config_dict ):
2929 self .host = config_dict .get ('mysql' ).get ('host' , 'localhost' )
3030 self .port = config_dict .get ('mysql' ).get ('port' , 3306 )
31+ self .socket = config_dict .get ('mysql' ).get ('socket' , None )
3132
3233 self .username = config_dict .get ('mysql' ).get ('username' , 'root' )
3334 self .password = config_dict .get ('mysql' ).get ('password' , '' )
3435
35- self .max_reconnect = int (config_dict .get ('mysql' ).get ('max_reconnect' , 30 ))
36+ self .max_reconnect = int (config_dict .get ('mysql' ).get ('max_reconnect' , 5 ))
3637 self .max_recovery = int (config_dict .get ('mysql' ).get ('max_recovery' , 10 ))
3738
3839 #Set the stats checks for MySQL
@@ -56,17 +57,21 @@ def configure(self, config_dict):
5657 def setup_connection (self ):
5758 connection_attempt = 0
5859
59- while self .max_reconnect == 0 or connection_attempt <= self .max_reconnect :
60- try :
61- self .connection = mdb .connect (host = self .host , user = self .username , port = self .port , passwd = self .password )
62- return self .connection
63- except Exception :
64- pass
65-
66- # If we got here, connection failed
67- connection_attempt += 1
68- time .sleep (self .reconnect_delay )
69- print ('Attempting reconnect #{0}...' .format (connection_attempt ))
60+ while self .max_reconnect == 0 or connection_attempt < self .max_reconnect :
61+ try :
62+ if self .socket :
63+ self .connection = mdb .connect (user = self .username , unix_socket = self .socket , passwd = self .password )
64+ else :
65+ self .connection = mdb .connect (host = self .host , user = self .username , port = self .port , passwd = self .password )
66+
67+ return self .connection
68+ except Exception :
69+ pass
70+
71+ # If we got here, connection failed
72+ connection_attempt += 1
73+ print ('Attempting reconnect #{0}...' .format (connection_attempt ))
74+ time .sleep (self .reconnect_delay )
7075
7176 # If we get out of the while loop, we've passed max_reconnect
7277 raise ThreadMySQLMaxReconnectException
0 commit comments