如果我使用自定义主函数 (
使用默认的主函数会导致 "访问冲突" 错误。看起来 MySQL 不允许从
我还运行了以下命令:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES;
我也尝试了不同的bind-address,如:0.0.0.0和localhost,但没有结果。每次绑定后,我都重新启动了MySQL服务。
我正在使用这个驱动程序http://code.dlang.org/packages/ddbc 问题出在哪里?
void main()
而不是 shared static this()
),一切都正常。使用默认的主函数会导致 "访问冲突" 错误。看起来 MySQL 不允许从
localhost
连接到它,但是在 my.ini 中我添加了这个字符串:
bind-address = 127.0.0.1
代码如下,如果有帮助:import std.stdio;
import std.path;
import std.file;
import std.string;
import dini;
import vibe.d;
import colorize;
import ddbc.all;
shared static this()
{
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, &hello);
auto parseconfig = new ParseConfig();
auto db = new DBConnect(parseconfig);
}
void hello(HTTPServerRequest req, HTTPServerResponse res)
{
res.writeBody("Hello, World!");
}
class ParseConfig
{
string dbname;
string dbuser;
string dbpass;
string dbhost;
string dbport;
this()
{
try
{
//getcwd do not return correct path if run from task shoulder
string confpath = buildPath((thisExePath[0..((thisExePath.lastIndexOf("\\"))+1)]), "config.ini");
//writefln(thisExePath[0..((thisExePath.lastIndexOf("\\"))+1)]); // get path without extention +1 is for getting last slash
//string confpath = buildPath(thisExePath, "config.ini");
if (!exists(confpath))
{
writeln("ERROR: config.ini do not exists");
}
auto config = Ini.Parse(confpath);
try
{
this.dbname = config.getKey("dbname");
this.dbuser = config.getKey("dbuser");
this.dbpass = config.getKey("dbpass");
this.dbhost = config.getKey("dbhost");
this.dbport = config.getKey("dbport");
}
catch (Exception msg)
{
cwritefln("ERROR: Can't parse config: %s".color(fg.red), msg.msg);
}
}
catch(Exception msg)
{
cwriteln(msg.msg.color(fg.red));
core.thread.Thread.sleep( dur!("msecs")(1000));
}
}
}
class DBConnect
{
Statement stmt;
ParseConfig parseconfig;
this(ParseConfig parseconfig)
{
try
{
this.parseconfig = parseconfig;
MySQLDriver driver = new MySQLDriver();
string url = MySQLDriver.generateUrl(parseconfig.dbhost, to!short(parseconfig.dbport), parseconfig.dbname);
string[string] params = MySQLDriver.setUserAndPassword(parseconfig.dbuser, parseconfig.dbpass);
DataSource ds = new ConnectionPoolDataSourceImpl(driver, url, params);
auto conn = ds.getConnection();
scope(exit) conn.close();
stmt = conn.createStatement();
writefln("\n[Database connection OK]");
}
catch (Exception ex)
{
writefln(ex.msg);
writeln("Could not connect to DB. Please check settings");
}
}
}
我还运行了以下命令:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES;
我也尝试了不同的bind-address,如:0.0.0.0和localhost,但没有结果。每次绑定后,我都重新启动了MySQL服务。
我正在使用这个驱动程序http://code.dlang.org/packages/ddbc 问题出在哪里?
flush privileges;
命令? - Mad Dog Tannenflush privileges;
。 - Dmitry BubnenkovGRANT ... @'127.0.0.1'...
- Rick Jamesstatic this()
之前连接呢?因为如果连循环都只能在所有函数构造函数返回后才能开始,那么它如何执行其他任务就很难理解了。或者是我没有理解到什么地方? - Dmitry Bubnenkov