MXS-2896: Fix monitor connection creation
The connection is now correctly null after a failed attempt to connect.
This commit is contained in:
@ -1462,49 +1462,54 @@ mxs_connect_result_t mon_ping_or_connect_to_db(MXS_MONITOR* mon, MXS_MONITORED_S
|
|||||||
}
|
}
|
||||||
/** Otherwise close the handle. */
|
/** Otherwise close the handle. */
|
||||||
mysql_close(database->con);
|
mysql_close(database->con);
|
||||||
|
database->con = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
mxs_connect_result_t conn_result = MONITOR_CONN_REFUSED;
|
char* uname = mon->user;
|
||||||
if ((database->con = mysql_init(NULL)))
|
char* passwd = mon->password;
|
||||||
|
|
||||||
|
if (database->server->monuser[0] && database->server->monpw[0])
|
||||||
{
|
{
|
||||||
char* uname = mon->user;
|
uname = database->server->monuser;
|
||||||
char* passwd = mon->password;
|
passwd = database->server->monpw;
|
||||||
|
}
|
||||||
|
|
||||||
if (database->server->monuser[0] && database->server->monpw[0])
|
char* dpwd = decrypt_password(passwd);
|
||||||
|
|
||||||
|
mxs_connect_result_t conn_result = MONITOR_CONN_REFUSED;
|
||||||
|
|
||||||
|
for (int i = 0; i < mon->connect_attempts; i++)
|
||||||
|
{
|
||||||
|
MYSQL* mysql = mysql_init(nullptr);
|
||||||
|
|
||||||
|
mysql_optionsv(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (void*) &mon->connect_timeout);
|
||||||
|
mysql_optionsv(mysql, MYSQL_OPT_READ_TIMEOUT, (void*) &mon->read_timeout);
|
||||||
|
mysql_optionsv(mysql, MYSQL_OPT_WRITE_TIMEOUT, (void*) &mon->write_timeout);
|
||||||
|
mysql_optionsv(mysql, MYSQL_PLUGIN_DIR, get_connector_plugindir());
|
||||||
|
|
||||||
|
time_t start = time(nullptr);
|
||||||
|
bool result = (mxs_mysql_real_connect(mysql, database->server, uname, dpwd) != nullptr);
|
||||||
|
time_t end = time(nullptr);
|
||||||
|
|
||||||
|
if (result)
|
||||||
{
|
{
|
||||||
uname = database->server->monuser;
|
database->con = mysql;
|
||||||
passwd = database->server->monpw;
|
conn_result = MONITOR_CONN_NEWCONN_OK;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
char* dpwd = decrypt_password(passwd);
|
|
||||||
|
|
||||||
mysql_optionsv(database->con, MYSQL_OPT_CONNECT_TIMEOUT, (void*) &mon->connect_timeout);
|
|
||||||
mysql_optionsv(database->con, MYSQL_OPT_READ_TIMEOUT, (void*) &mon->read_timeout);
|
|
||||||
mysql_optionsv(database->con, MYSQL_OPT_WRITE_TIMEOUT, (void*) &mon->write_timeout);
|
|
||||||
mysql_optionsv(database->con, MYSQL_PLUGIN_DIR, get_connector_plugindir());
|
|
||||||
|
|
||||||
time_t start = 0;
|
|
||||||
time_t end = 0;
|
|
||||||
for (int i = 0; i < mon->connect_attempts; i++)
|
|
||||||
{
|
{
|
||||||
start = time(NULL);
|
mysql_close(mysql);
|
||||||
bool result = (mxs_mysql_real_connect(database->con, database->server, uname, dpwd) != NULL);
|
|
||||||
end = time(NULL);
|
|
||||||
|
|
||||||
if (result)
|
|
||||||
{
|
|
||||||
conn_result = MONITOR_CONN_NEWCONN_OK;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (conn_result == MONITOR_CONN_REFUSED && (int)difftime(end, start) >= mon->connect_timeout)
|
if (conn_result == MONITOR_CONN_REFUSED && (int)difftime(end, start) >= mon->connect_timeout)
|
||||||
{
|
{
|
||||||
conn_result = MONITOR_CONN_TIMEOUT;
|
conn_result = MONITOR_CONN_TIMEOUT;
|
||||||
}
|
}
|
||||||
MXS_FREE(dpwd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MXS_FREE(dpwd);
|
||||||
|
|
||||||
return conn_result;
|
return conn_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user