首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从vibed应用程序连接MySQL/MariaDB数据库

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

Stack Overflow用户
提问于 2015-05-18 11:36:06
回答 2查看 515关注 0票数 4

如果我使用自定义main ( void main()而不是shared static this() ),所有这些都可以正常工作。

在默认的main中,我得到了“访问冲突”错误。它看起来像是MySQL不允许从localhost连接到它,但是在my.ini中我添加了字符串:

bind-address = 127.0.0.1

密码,如果有用的话:

代码语言:javascript
复制
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

怎么了?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-20 21:13:24

继续我的评论( 无法从vibed应用程序连接MySQL/MariaDB数据库 )。

我刚刚进行了测试,这肯定是事件循环;)

而不是:

代码语言:javascript
复制
auto parseconfig = new ParseConfig();
auto db = new DBConnect(parseconfig);

只要做:

代码语言:javascript
复制
runTask({
    auto parseconfig = new ParseConfig();
    auto db = new DBConnect(parseconfig);
});

为我工作过(DMD2.067.0/Vibe0.7.23/ ddbc 0.2.24 /着色和迪尼母版)。

要回答您的注释( 无法从vibed应用程序连接MySQL/MariaDB数据库):事件循环从主函数内部开始。当您启动一个D应用程序时会发生什么?入口点是运行时内部的C main,它初始化它(运行时),包括模块构造函数,运行unittest (如果您使用-unittest编译过),然后调用main (名称为"_Dmain“),这对于知道是否要用GDB设置断点很有用。当调用Vibe.d的main时,它解析命令行参数(一个可选的配置文件),最后启动事件循环。任何希望在事件循环启动后运行的代码都应该使用runTask和类似的,或者createTimer。他们不应该直接从静态构造函数调用代码(这实际上是从Vibe.d开始时最常见的错误之一)。

票数 3
EN

Stack Overflow用户

发布于 2015-05-20 09:55:00

在开发mysql- (另一种MySQL/MariaDB驱动程序)时,我遇到了一个可能相关的问题。

我认为这个问题与phobos/SHA1 1中的模块初始化顺序错误有关,我认为它在2.067.1中仍然是开放的。建议的解决办法是使用VibeCustomMain,并定义自己的main()。您只需从appmain.d复制defaul main()并使用它。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30302161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档