Use another way to connect to database. (#3)
* Use another way to connect to database. Actual way crash if mysql user doesn't have for example the password. * remove tabs -> spaces
This commit is contained in:
@@ -92,7 +92,11 @@ WrongPass.BanType = 0
|
|||||||
# Unix/Linux)
|
# Unix/Linux)
|
||||||
# Default: "127.0.0.1;3306;username;password;auth"
|
# Default: "127.0.0.1;3306;username;password;auth"
|
||||||
|
|
||||||
LoginDatabaseInfo = "127.0.0.1;3306;username;password;auth"
|
LoginDatabaseInfo.Host = "127.0.0.1"
|
||||||
|
LoginDatabaseInfo.Port = "3306"
|
||||||
|
LoginDatabaseInfo.Username = "username"
|
||||||
|
LoginDatabaseInfo.Password = "password"
|
||||||
|
LoginDatabaseInfo.Database = "auth"
|
||||||
|
|
||||||
#
|
#
|
||||||
# DBQueryWorkerInterval
|
# DBQueryWorkerInterval
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
namespace Framework.Database
|
||||||
|
{
|
||||||
|
public class ConnectionObject
|
||||||
|
{
|
||||||
|
public string Host { get; set; }
|
||||||
|
public string Port { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
public string Database { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,14 +35,16 @@ namespace Framework.Database
|
|||||||
bool updatesEnabled = database.IsAutoUpdateEnabled(_updateFlags);
|
bool updatesEnabled = database.IsAutoUpdateEnabled(_updateFlags);
|
||||||
_open.Add(() =>
|
_open.Add(() =>
|
||||||
{
|
{
|
||||||
string dbString = ConfigMgr.GetDefaultValue(name + "DatabaseInfo", "");
|
ConnectionObject co = new ConnectionObject
|
||||||
if (string.IsNullOrEmpty(dbString))
|
{
|
||||||
{
|
Database = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Database", ""),
|
||||||
Log.outError(LogFilter.ServerLoading, "Database {0} not specified in configuration file!", name);
|
Host = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Host", ""),
|
||||||
return false;
|
Password = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Password", ""),
|
||||||
}
|
Port = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Port", ""),
|
||||||
|
Username = ConfigMgr.GetDefaultValue(name + "DatabaseInfo.Username", "")
|
||||||
|
};
|
||||||
|
|
||||||
var error = database.Initialize(dbString);
|
var error = database.Initialize(co);
|
||||||
if (error != MySqlErrorCode.None)
|
if (error != MySqlErrorCode.None)
|
||||||
{
|
{
|
||||||
// Database does not exist
|
// Database does not exist
|
||||||
@@ -57,7 +59,7 @@ namespace Framework.Database
|
|||||||
Log.outInfo(LogFilter.ServerLoading, "Creating database \"{0}\"...", name);
|
Log.outInfo(LogFilter.ServerLoading, "Creating database \"{0}\"...", name);
|
||||||
string sqlString = string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", name);
|
string sqlString = string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", name);
|
||||||
// Try to create the database and connect again if auto setup is enabled
|
// Try to create the database and connect again if auto setup is enabled
|
||||||
if (database.Apply(sqlString) && database.Initialize(dbString) == MySqlErrorCode.None)
|
if (database.Apply(sqlString) && database.Initialize(co) == MySqlErrorCode.None)
|
||||||
error = MySqlErrorCode.None;
|
error = MySqlErrorCode.None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,16 @@ namespace Framework.Database
|
|||||||
Poolsize = poolSize;
|
Poolsize = poolSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MySqlConnectionInfo(ConnectionObject connectionObject, int poolSize = 10)
|
||||||
|
{
|
||||||
|
Host = connectionObject.Host;
|
||||||
|
Port = connectionObject.Port;
|
||||||
|
Username = connectionObject.Username;
|
||||||
|
Password = connectionObject.Password;
|
||||||
|
Database = connectionObject.Database;
|
||||||
|
Poolsize = poolSize;
|
||||||
|
}
|
||||||
|
|
||||||
public MySqlConnection GetConnection()
|
public MySqlConnection GetConnection()
|
||||||
{
|
{
|
||||||
return new MySqlConnection(string.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};Allow Zero Datetime=True;Allow User Variables=True;Pooling=true;MaximumPoolSize={5};",
|
return new MySqlConnection(string.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};Allow Zero Datetime=True;Allow User Variables=True;Pooling=true;MaximumPoolSize={5};",
|
||||||
@@ -81,6 +91,26 @@ namespace Framework.Database
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MySqlErrorCode Initialize(ConnectionObject connectionObject)
|
||||||
|
{
|
||||||
|
_connectionInfo = new MySqlConnectionInfo(connectionObject);
|
||||||
|
_updater = new DatabaseUpdater<T>(this);
|
||||||
|
|
||||||
|
using (var connection = _connectionInfo.GetConnection())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
connection.Open();
|
||||||
|
Log.outInfo(LogFilter.SqlDriver, "Connected to MySQL(ver: {0}) Database: {1}", connection.ServerVersion, _connectionInfo.Database);
|
||||||
|
return MySqlErrorCode.None;
|
||||||
|
}
|
||||||
|
catch (MySqlException ex)
|
||||||
|
{
|
||||||
|
return (MySqlErrorCode)((MySqlException)ex.InnerException).Number;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Execute(string sql, params object[] args)
|
public void Execute(string sql, params object[] args)
|
||||||
{
|
{
|
||||||
Execute(new PreparedStatement(string.Format(sql, args)));
|
Execute(new PreparedStatement(string.Format(sql, args)));
|
||||||
|
|||||||
@@ -131,6 +131,7 @@
|
|||||||
<Compile Include="Cryptography\SARC4.cs" />
|
<Compile Include="Cryptography\SARC4.cs" />
|
||||||
<Compile Include="Cryptography\SessionKeyGeneration.cs" />
|
<Compile Include="Cryptography\SessionKeyGeneration.cs" />
|
||||||
<Compile Include="Cryptography\ShaHmac.cs" />
|
<Compile Include="Cryptography\ShaHmac.cs" />
|
||||||
|
<Compile Include="Database\ConnectionObject.cs" />
|
||||||
<Compile Include="Database\DatabaseLoader.cs" />
|
<Compile Include="Database\DatabaseLoader.cs" />
|
||||||
<Compile Include="Database\Databases\CharacterDatabase.cs" />
|
<Compile Include="Database\Databases\CharacterDatabase.cs" />
|
||||||
<Compile Include="Database\DatabaseUpdater.cs" />
|
<Compile Include="Database\DatabaseUpdater.cs" />
|
||||||
|
|||||||
@@ -42,10 +42,29 @@ LogsDir = "./Logs"
|
|||||||
# "127.0.0.1;3306;username;password;characters" - (CharacterDatabaseInfo)
|
# "127.0.0.1;3306;username;password;characters" - (CharacterDatabaseInfo)
|
||||||
# "127.0.0.1;3306;username;password;hotfixes" - (HotfixDatabaseInfo)
|
# "127.0.0.1;3306;username;password;hotfixes" - (HotfixDatabaseInfo)
|
||||||
|
|
||||||
LoginDatabaseInfo = "127.0.0.1;3306;username;password;auth"
|
LoginDatabaseInfo.Host = "127.0.0.1"
|
||||||
WorldDatabaseInfo = "127.0.0.1;3306;username;password;world"
|
LoginDatabaseInfo.Port = "3306"
|
||||||
CharacterDatabaseInfo = "127.0.0.1;3306;username;password;characters"
|
LoginDatabaseInfo.Username = "username"
|
||||||
HotfixDatabaseInfo = "127.0.0.1;3306;username;password;hotfixes"
|
LoginDatabaseInfo.Password = "password"
|
||||||
|
LoginDatabaseInfo.Database = "auth"
|
||||||
|
|
||||||
|
WorldDatabaseInfo.Host = "127.0.0.1"
|
||||||
|
WorldDatabaseInfo.Port = "3306"
|
||||||
|
WorldDatabaseInfo.Username = "username"
|
||||||
|
WorldDatabaseInfo.Password = "password"
|
||||||
|
WorldDatabaseInfo.Database = "world"
|
||||||
|
|
||||||
|
CharacterDatabaseInfo.Host = "127.0.0.1"
|
||||||
|
CharacterDatabaseInfo.Port = "3306"
|
||||||
|
CharacterDatabaseInfo.Username = "username"
|
||||||
|
CharacterDatabaseInfo.Password = "password"
|
||||||
|
CharacterDatabaseInfo.Database = "characters"
|
||||||
|
|
||||||
|
HotfixDatabaseInfo.Host = "127.0.0.1"
|
||||||
|
HotfixDatabaseInfo.Port = "3306"
|
||||||
|
HotfixDatabaseInfo.Username = "username"
|
||||||
|
HotfixDatabaseInfo.Password = "password"
|
||||||
|
HotfixDatabaseInfo.Database = "hotfixes"
|
||||||
|
|
||||||
#
|
#
|
||||||
# WorldServerPort
|
# WorldServerPort
|
||||||
|
|||||||
Reference in New Issue
Block a user