无法从 vibed 应用程序连接到 MySQL/MariaDB 数据库

Can't connect to MySQL/MariaDB database from vibed app

如果我使用自定义 main(void main() 而不是 shared static this()),一切正常。

使用默认的 main 我收到 "Access Violation" 错误。看起来 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.0localhost 但没有结果。每次新绑定后,我都会重新启动 MySQL 服务。

我正在使用这个驱动程序http://code.dlang.org/packages/ddbc

怎么了?

我 运行 在开发 mysql-lited 替代 MySQL/MariaDB 驱动程序时遇到了一个可能相关的问题。

我认为这个问题与 phobos/SHA1 中的模块初始化顺序错误有关,我相信它在 2.067.1 中仍然存在。建议的解决方法是改为使用 VibeCustomMain,并定义您自己的 main()。您可以从 appmain.d 复制默认的 main() 并使用它。

或者您可以尝试 mysql-lited 看看是否更适合您。

继续我的评论 ()。

刚刚测试了一下,肯定是事件循环;)

而不是:

auto parseconfig = new ParseConfig();
auto db = new DBConnect(parseconfig);

就这样:

runTask({
    auto parseconfig = new ParseConfig();
    auto db = new DBConnect(parseconfig);
});

为我工作(DMD 2.067.0 / Vibe 0.7.23 / ddbc 0.2.24 / colorize & dini master)。

回答您的评论 ():事件循环在主函数内部开始。 当您启动 D 应用程序时会发生什么?入口点是 运行 时间里的 C main,它初始化它(运行 时间),包括模块构造函数,运行 单元测试(如果你用 -unittest 编译) ,然后调用您的 main(名称为“_Dmain” - 有助于了解您是否要使用 GDB 设置断点)。 当 Vibe.d 的 main 被调用时,它解析命令行参数,一个可选的配置文件,最后,启动事件循环。一旦事件循环开始,任何希望 运行 的代码都应该使用 runTask 和类似的,或者 createTimer。他们不应该直接从静态构造函数调用代码(这实际上是从 Vibe.d 开始时最常见的错误之一)。