简体   繁体   English

cleardb (mySQL) 与 Heroku (spring) 的连接问题

[英]cleardb (mySQL) connection problem witth Heroku (spring)

I am creating a springboot application which has data stored in a mysql database.我正在创建一个 springboot 应用程序,它的数据存储在 mysql 数据库中。 I have used cleardb as an add-on and have deployed the app to Heroku.我使用cleardb作为附加组件并将应用程序部署到 Heroku。 When I run the app (ie heroku open ), the main page (index.html) displays successfully as it does not require access to the mySQL database.当我运行应用程序(即heroku open )时,主页面 (index.html) 显示成功,因为它不需要访问 mySQL 数据库。 However, all of the pages that do require access to the mySQL database do not display and return the error below:但是,所有需要访问 mySQL 数据库的页面都不会显示并返回以下错误:

There was an unexpected error (type=Internal Server Error, status=500).出现意外错误(类型=内部服务器错误,状态=500)。 Failed to obtain JDBC Connection;获取JDBC连接失败; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago.嵌套异常是 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 最后一个数据包成功发送到服务器是 0 毫秒前。 The driver has not received any packets from the server.驱动程序没有收到来自服务器的任何数据包。

Here is some of my code:这是我的一些代码:

Databaseconfig数据库配置

@Configuration

public class DatabaseConfig {公共类 DatabaseConfig {

@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
    return new NamedParameterJdbcTemplate(dataSource);
}

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost/NBA");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    return dataSource;
}

} }

pom.xml plugins pom.xml 插件

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>

database_url (heroku config) database_url(heroku 配置)

CLEARDB_DATABASE_URL: mysql://b---------9:9--------@us-cdbr-iron-east-05.cleardb.net/heroku_f61b74207636b77?reconnect=true
DATABASE_URL:         'mysql://b---------9:9--------@us-cdbr-iron-east-05.cleardb.net/heroku_f61b74207636b77?reconnect=true'

Some things to take note of:需要注意的一些事项:

  • My application.properties file is currently empty.我的application.properties文件目前是空的。

  • The application runs perfectly when I run it on localhost:8080.当我在 localhost:8080 上运行该应用程序时,它运行良好。

Any and all help would be greatly appreciated.任何和所有的帮助将不胜感激。

For your case your must change the dataSource configuration according your database_url(heroku config):对于您的情况,您必须根据您的 database_url(heroku config) 更改数据源配置:

dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://us-cdbr-iron-east-05.cleardb.net/heroku_f61b74207636b77?reconnect=true");
dataSource.setUsername("b05ee51c5eec59");
dataSource.setPassword("94a4e9eb");

I also suggest you to use following in your application.properties instead of using datasource bean:我还建议您在 application.properties 中使用以下内容,而不是使用数据源 bean:

spring.datasource.username=
spring.datasource.password=
spring.datasource.url=

Also very important: change your database password in the heroku and dont write it on the internet同样非常重要:在heroku中更改您的数据库密码,不要将其写在互联网上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM