Skip to content

Commit

Permalink
Core/Database: Fix database connections failing to open over named pi…
Browse files Browse the repository at this point in the history
…pes for users using caching_sha2_password authentication plugin if this is the first time this user attempts to log in since starting MySQL server
  • Loading branch information
Shauren committed Sep 25, 2024
1 parent 3238175 commit 726412d
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/server/database/Database/MySQLConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "MySQLPreparedStatement.h"
#include "PreparedStatement.h"
#include "QueryResult.h"
#include "StringConvert.h"
#include "Timer.h"
#include "Transaction.h"
#include "Util.h"
Expand Down Expand Up @@ -96,42 +97,40 @@ uint32 MySQLConnection::Open()
return CR_UNKNOWN_ERROR;
}

int port;
char const* unix_socket;
int port = 0;
char const* unix_socket = nullptr;
//unsigned int timeout = 10;

mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8mb4");
//mysql_options(mysqlInit, MYSQL_OPT_READ_TIMEOUT, (char const*)&timeout);
#ifdef _WIN32
if (m_connectionInfo.host == ".") // named pipe use option (Windows)
if (m_connectionInfo.host != ".")
{
unsigned int opt = MYSQL_PROTOCOL_PIPE;
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
port = 0;
unix_socket = nullptr;
}
else // generic case
{
port = atoi(m_connectionInfo.port_or_socket.c_str());
unix_socket = nullptr;
port = Trinity::StringTo<int32>(m_connectionInfo.port_or_socket).value_or(0);
}
#else
if (m_connectionInfo.host == ".") // socket use option (Unix/Linux)
else // named pipe/unix socket option
{
unsigned int opt = MYSQL_PROTOCOL_SOCKET;
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
unsigned int opt = MYSQL_PROTOCOL_PIPE;
#else
m_connectionInfo.host = "localhost";
port = 0;
unix_socket = m_connectionInfo.port_or_socket.c_str();
unsigned int opt = MYSQL_PROTOCOL_SOCKET;
#endif
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);

#if !defined(MARIADB_VERSION_ID) && MYSQL_VERSION_ID >= 80000
/*
ensure connections over named pipes work for users authenticating with caching_sha2_password
If the mysql server is restarted, and you connect it using named pipe, the connection will fail and it will continue to fail until you connect it using tcp.
Source: https://bugs.mysql.com/bug.php?id=106852
*/
MySQLBool geterverPublicKey = MySQLBool(1);
mysql_options(mysqlInit, MYSQL_OPT_GET_SERVER_PUBLIC_KEY, (char const*)&geterverPublicKey);
#endif
}
else // generic case
{
port = atoi(m_connectionInfo.port_or_socket.c_str());
unix_socket = nullptr;
}
#endif

if (m_connectionInfo.ssl != "")
if (!m_connectionInfo.ssl.empty())
{
#if !defined(MARIADB_VERSION_ID) && MYSQL_VERSION_ID >= 80000
mysql_ssl_mode opt_use_ssl = SSL_MODE_DISABLED;
Expand Down

0 comments on commit 726412d

Please sign in to comment.