- mysql_options(connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
- std::string host = config->getString("host");
- std::string user = config->getString("user");
- std::string pass = config->getString("pass");
- std::string dbname = config->getString("name");
- int port = config->getInt("port");
- bool rv = mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, 0);
- if (!rv)
- return rv;
- std::string initquery;
- if (config->readString("initialquery", initquery))
+
+ // Set the connection timeout.
+ unsigned int timeout = config->getDuration("timeout", 5, 1, 30);
+ mysql_options(connection, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
+
+ // Attempt to connect to the database.
+ const std::string host = config->getString("host");
+ const std::string user = config->getString("user");
+ const std::string pass = config->getString("pass");
+ const std::string dbname = config->getString("name");
+ unsigned int port = config->getUInt("port", 3306, 1, 65535);
+ if (!mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, CLIENT_IGNORE_SIGPIPE))
+ {
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Unable to connect to the %s MySQL server: %s",
+ GetId().c_str(), mysql_error(connection));
+ return false;
+ }
+
+ // Set the default character set.
+ const std::string charset = config->getString("charset");
+ if (!charset.empty() && mysql_set_character_set(connection, charset.c_str()))
+ {
+ ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Could not set character set for %s to \"%s\": %s",
+ GetId().c_str(), charset.c_str(), mysql_error(connection));
+ return false;
+ }
+
+ // Execute the initial SQL query.
+ const std::string initialquery = config->getString("initialquery");
+ if (!initialquery.empty() && mysql_real_query(connection, initialquery.data(), initialquery.length()))