MySQL Flyway迁移,无法从DataSource获取Jdbc连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25186347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flyway migration, Unable to obtain Jdbc connection from DataSource
提问by TurboNerd
I am trying to use flyway to create and manage a MySQL database. Here is the code i have got so far.
我正在尝试使用 flyway 创建和管理 MySQL 数据库。这是我到目前为止得到的代码。
FlywayMigration.java : Class that applys the migration
FlywayMigration.java :应用迁移的类
public class FlywayMigration
{
public FlywayMigration(DatabaseConfiguration configuration, Flyway flyway)
{
flyway.setDataSource(configuration.getDataSource());
flyway.migrate();
}
public static void main(String[] args)
{
new FlywayMigration(new DatabaseConfiguration("database.properties"), new Flyway());
}
}
DatabaseConfiguration.java : Configuration class, this class will configure the datasource to be applyed to the Flyway.setDataSource method
DatabaseConfiguration.java : 配置类,这个类会配置要应用到 Flyway.setDataSource 方法的数据源
public class DatabaseConfiguration
{
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
private PropertiesUtil prop = null;
public DatabaseConfiguration(String file)
{
prop = new PropertiesUtil(file);
}
public String getDataSourceClass()
{
return prop.getProperty("mysql.data.source.class");
}
public String getURL ()
{
return prop.getProperty("mysql.url");
}
public String getHostName()
{
return prop.getProperty("mysql.host.name");
}
public String getDatabaseName()
{
return prop.getProperty("mysql.database.name");
}
public DataSource getDataSource()
{
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setURL(getURL());
dataSource.setUser(prop.getProperty("mysql.user.name"));
dataSource.setPassword(null);
return dataSource;
}
}
database.properties is the file where i store the database information, password can be null
database.properties 是我存放数据库信息的文件,密码可以为空
mysql.data.source.class=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/vmrDB
mysql.host.name=localhost
mysql.database.name=vmrDB
mysql.user.name=root
And i get the folowing error in my trace
我的跟踪中出现以下错误
Exception in thread "main" org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
at org.flywaydb.core.Flyway.execute(Flyway.java:1144)
at org.flywaydb.core.Flyway.migrate(Flyway.java:811)
at com.bt.sitb.vmr.migration.FlywayMigration.<init>(FlywayMigration.java:10)
at com.bt.sitb.vmr.migration.FlywayMigration.main(FlywayMigration.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Can someone please tell me why the DataSource from MySQL is not connecting. Thanks!
有人可以告诉我为什么 MySQL 的 DataSource 没有连接。谢谢!
回答by florian
It looks like Flyway cannot connect to the database.
看起来 Flyway 无法连接到数据库。
One reason for this is that the database in the database URL does not exist.
原因之一是数据库 URL 中的数据库不存在。
Question: does your database schema exist?
问题:你的数据库模式存在吗?
If your answer is no, then:
如果你的答案是否定的,那么:
- connect to jdbc:mysql://localhost:3306/mysql
- also specify the schema to use for migration with
flyway.setSchemas(configuration.getDatabaseName())
- you also need
flyway.init()
before you can initialize migration of your database.
- 连接到 jdbc:mysql://localhost:3306/ mysql
- 还指定用于迁移的架构
flyway.setSchemas(configuration.getDatabaseName())
- 您还需要
flyway.init()
在初始化数据库迁移之前。
回答by Ayo K
Ran into this same issue. Apparently, the problem was with my .properties
file. The jar was using the one packaged with it and not the external one. So I moved my external properties file out of the resources folder and into the root directory of the jar and problem solved!
Hope this helps someone.
遇到了同样的问题。显然,问题出在我的.properties
文件上。罐子使用的是与它一起包装的罐子,而不是外部罐子。所以我将我的外部属性文件从资源文件夹中移到了 jar 的根目录中,问题解决了!希望这可以帮助某人。