无法从振动应用程序连接到MySQL / MariaDB数据库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了无法从振动应用程序连接到MySQL / MariaDB数据库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3418字,纯文字阅读大概需要5分钟。
内容图文
![无法从振动应用程序连接到MySQL / MariaDB数据库](/upload/InfoBanner/zyjiaocheng/905/0b4045a9552a468893272ebc1bcdc9a7.jpg)
如果我使用自定义main(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");
}
}
}
我也运行下一个命令:
授予所有特权*.*””””””””””””””””””””””””””’
FLUSH特权;
我也尝试了不同的绑定地址,如:0.0.0.0和localhost但没有结果.每次新绑定后,我都重启了MySQL服务.
我正在使用这个驱动程序http://code.dlang.org/packages/ddbc
怎么了?
解决方法:
继续我的评论(Can’t connect to MySQL/MariaDB database from vibed app).
我刚刚测试过,它绝对是事件循环;)
代替:
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).
要回答你的评论(Can’t connect to MySQL/MariaDB database from vibed app):事件循环在main函数内部开始.
启动D应用程序会发生什么?入口点是运行时内部的C main,它初始化它(运行时),包括模块构造函数,运行unittest(如果你用-unittest编译),然后调用你的main(名字是“_Dmain” – 有用知道你是否想用GDB设置断点.
当调用Vibe.d的main时,它会解析命令行参数,一个可选的配置文件,最后启动事件循环.希望在事件循环开始后运行的任何代码都应该使用runTask和类似的,或createTimer.他们不应该直接从静态构造函数调用代码(这实际上是从Vibe.d开始时最常见的错误之一).
内容总结
以上是互联网集市为您收集整理的无法从振动应用程序连接到MySQL / MariaDB数据库全部内容,希望文章能够帮你解决无法从振动应用程序连接到MySQL / MariaDB数据库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。