Core/MySQL: wait for the DB to be ready (#1286)
* Core/MySQL: wait for the DB to be ready - Closes #1285 * Fix build on windows
This commit is contained in:
@@ -14,6 +14,8 @@
|
|||||||
#include <mysql.h>
|
#include <mysql.h>
|
||||||
#include <mysqld_error.h>
|
#include <mysqld_error.h>
|
||||||
#include <errmsg.h>
|
#include <errmsg.h>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "MySQLConnection.h"
|
#include "MySQLConnection.h"
|
||||||
#include "MySQLThreading.h"
|
#include "MySQLThreading.h"
|
||||||
@@ -24,6 +26,9 @@
|
|||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
|
||||||
|
using namespace std::this_thread;
|
||||||
|
using namespace std::chrono;
|
||||||
|
|
||||||
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
|
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
|
||||||
m_reconnecting(false),
|
m_reconnecting(false),
|
||||||
m_prepareError(false),
|
m_prepareError(false),
|
||||||
@@ -107,36 +112,59 @@ bool MySQLConnection::Open()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(),
|
// Possible improvement for future: make ATTEMPTS and SECONDS configurable values
|
||||||
m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0);
|
uint32 const ATTEMPTS = 180;
|
||||||
|
uint32 const SECONDS = 10;
|
||||||
|
|
||||||
if (m_Mysql)
|
uint32 count = 0;
|
||||||
{
|
do {
|
||||||
if (!m_reconnecting)
|
m_Mysql = mysql_real_connect(
|
||||||
|
mysqlInit,
|
||||||
|
m_connectionInfo.host.c_str(),
|
||||||
|
m_connectionInfo.user.c_str(),
|
||||||
|
m_connectionInfo.password.c_str(),
|
||||||
|
m_connectionInfo.database.c_str(),
|
||||||
|
port,
|
||||||
|
unix_socket,
|
||||||
|
0);
|
||||||
|
|
||||||
|
if (m_Mysql)
|
||||||
{
|
{
|
||||||
sLog->outSQLDriver("MySQL client library: %s", mysql_get_client_info());
|
if (!m_reconnecting)
|
||||||
sLog->outSQLDriver("MySQL server ver: %s ", mysql_get_server_info(m_Mysql));
|
{
|
||||||
// MySQL version above 5.1 IS required in both client and server and there is no known issue with different versions above 5.1
|
sLog->outSQLDriver("MySQL client library: %s", mysql_get_client_info());
|
||||||
// if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
|
sLog->outSQLDriver("MySQL server ver: %s ", mysql_get_server_info(m_Mysql));
|
||||||
// sLog->outInfo(LOG_FILTER_SQL, "[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
|
// MySQL version above 5.1 IS required in both client and server and there is no known issue with different versions above 5.1
|
||||||
}
|
// if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
|
||||||
|
// sLog->outInfo(LOG_FILTER_SQL, "[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
|
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
|
||||||
sLog->outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str());
|
sLog->outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str());
|
||||||
#endif
|
#endif
|
||||||
mysql_autocommit(m_Mysql, 1);
|
mysql_autocommit(m_Mysql, 1);
|
||||||
|
|
||||||
// set connection properties to UTF8 to properly handle locales for different
|
// set connection properties to UTF8 to properly handle locales for different
|
||||||
// server configs - core sends data in UTF8, so MySQL must expect UTF8 too
|
// server configs - core sends data in UTF8, so MySQL must expect UTF8 too
|
||||||
mysql_set_character_set(m_Mysql, "utf8");
|
mysql_set_character_set(m_Mysql, "utf8");
|
||||||
return PrepareStatements();
|
return PrepareStatements();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
|
count++;
|
||||||
mysql_close(mysqlInit);
|
sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
|
||||||
return false;
|
sLog->outError("Retrying in 10 seconds...\n\n");
|
||||||
}
|
sleep_for(seconds(SECONDS));
|
||||||
|
}
|
||||||
|
} while (!m_Mysql && count < ATTEMPTS);
|
||||||
|
|
||||||
|
sLog->outError(
|
||||||
|
"Could not connect to MySQL database at %s: %s after %d attempts\n",
|
||||||
|
m_connectionInfo.host.c_str(),
|
||||||
|
mysql_error(mysqlInit),
|
||||||
|
ATTEMPTS);
|
||||||
|
mysql_close(mysqlInit);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MySQLConnection::PrepareStatements()
|
bool MySQLConnection::PrepareStatements()
|
||||||
|
|||||||
Reference in New Issue
Block a user