- 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");
- unsigned int port = config->getUInt("port", 3306);
- 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;
-
- // Enable character set settings
- std::string charset = config->getString("charset");
- if ((!charset.empty()) && (mysql_set_character_set(connection, charset.c_str())))
- ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not set character set to \"%s\"", charset.c_str());
-
- 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()))