How to connect to a MySQL database over JDBC

How to connect to a MySQL database over JDBC

book

Article ID: KB0073343

calendar_today

Updated On:

Products Versions
TIBCO Streaming 10.6

Description

How can I connect to a MySQL database from my Streaming and/or LiveView applications?
 

Issue/Introduction

Outlines the steps to connect to a MySQL jdbc data source.

Resolution

First, determine the correct URL of your MySQL database. The URL should be in the following format:
jdbc:mysql://<dbhost>:<dbport>/<dbname>
..where:
  • dbhost = the database hostname
  • dbport = the database listen port (default is 3306)
  • dbname = the database name (e.g. "mysql")
To quickly spin up a MySQL database for simple testing purposes, you can use Docker..
docker pull mysql:5.6.51
docker run --name mysql56 -e MYSQL_ROOT_PASSWORD=P@ssw0rd! -e MYSQL_DATABASE=mysql -e MYSQL_USER=dbuser -e MYSQL_PASSWORD=P@ssw0rd! -p 3306:3306 -d mysql:5.6.51
This will start a local MySQL 5.6 database with database name mysql. For this database, you would use the following connection URL:
jdbc:mysql://localhost:3306/mysql
Next, install a compatible MySQL jdbc driver into your Maven repository. To determine the correct driver for your MySQL version, refer to the guidance here. Typically, it is best to download the latest driver that is compatible with both your MySQL database version and the JDK version used to run your Streaming application. For example:
mvn install:install-file -Dfile=mysql-connector-java-8.0.24.jar -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=8.0.24 -Dpackaging=jar
Then add the driver as a dependency in your project's pom.xml:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.24</version>
</dependency>
Finally, create a JDBC HOCON configuration:
configuration = {
  JDBCDataSourceGroup = {
    jdbcDataSources = {
      "mysql56" = {
          driverClassName = "com.mysql.jdbc.Driver"
          serverURL = "jdbc:mysql://localhost:3306/mysql"
          userName = "dbuser"
          password = "P@ssw0rd!"
      }
    }
  }
}
You are now configured to connect to your MySQL data source.

Additional Information

For additional details on JDBC HOCON configuration, refer to the Help under Home > Configuration Guide > StreamBase Configuration Types > StreamBase JDBC DataSource Configuration