Using Tomcat JNDI + HAProxy + MySQL Cluster - mysql

I have a MySQL Cluster that sits behind a HAProxy
for NON-SSL, I've configured my Tomcat JNDI Global Resource( HikariJNDIFactory) to connect to the HAProxy. everything works...
Now I want to perform SSL-Termination/SSLOffload betwen a secure connection from Tomcat JNDI to HAProxy.
I've searched all over, all i see is pass through SSL, that requires MySQL Cluster to use SSL(which i don't want to do.).
is this possible.?
server.xml
...
<Resource name="jdbc/secondDataSource"
auth="Container"
factory="com.zaxxer.hikari.HikariJNDIFactory"
type="javax.sql.DataSource"
dataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
driverClass="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://url2:3306/schema?autoReconnect=true"
dataSource.user="user2"
dataSource.password="pwd2"
minimumIdle="5"
maximumPoolSize="50"
connectionTimeout="300000"
maxLifetime="1800000"
connectionTestQuery="SELECT 1 FROM DUAL" />
...
i have tomcat starting with SSL, however, I will not connect to HaProxy over SSL.
what would be the configuration for HaProxy to make this work.(my SQL Nodes behind the proxy are using private Ips(10.0.x.x) )

Related

Maxscale keep connections that are killed by the application

We are trying to use maxscale for DB load balancing (MySQL 5.7).
We are using Hikari for application connection pool.
We would like that the Hikari will manage the connection pooling and maxscale will follow the connections from Hikari's side.
For the test we only have the master connected to maxscale.
Both Hikari and the application are set to 20 max connections.
On max scale we use the following configuration:
Host configuration
persistpoolmax=0
persistmaxtime=60
service configuration
max_connections=20
We also commented out the connection_timeout on the service section.
What we see, is when we stop the applications, the connections remain open on maxscale and when we restart the application, if fails to connect on max-connections exceeded.
What are we doing wrong?

Using an AWS Elastic Load Balancer for MySQL Cluster

I have a web server which connects to a MySQL cluster (it's Galera cluster)... the cluster works fine (if I update a record on the first instance of MySQL, it is automatically updated on the second instance). The problem is I need a load balancer between the web server and MySQL instances...
So What I have done is to setup an AWS Network Load Balancer (NLB works on Network Layer, TCP in my case)... I have added the 2 instances of MySQL to the NLB's Target group (their private IPs).
Now I go ahead an update the connection string in my Web Server to connect to the NLB:
<add name="MyDB" connectionString="Server=my-nlb-ap-southeast-2.amazonaws.com; Port=3306; Database=mydb; Uid=user1; Pwd=mypassword; Protocol=TCP" providerName="MySql.Data.MySqlClient"/>
But I get the following error:
[MySqlException (0x80004005): Unable to connect to any of the
specified MySQL hosts.] MySql.Data.MySqlClient.NativeDriver.Open()
+1734 MySql.Data.MySqlClient.Driver.Open() +60 MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder
settings) +309
MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() +18
MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() +290
MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() +151
MySql.Data.MySqlClient.MySqlPool.GetConnection() +60
MySql.Data.MySqlClient.MySqlConnection.Open() +1588
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection
connection) +182
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection
connection)
When I try to connect to MySQL from my web server, I get the following:
This indicates that the web server is able to reach the MySQL instance through the NLB, but I don't know why the connection string is not working.
I have opened all the IPs on port 3306 , and I can connect to MySQL through NLB from my local computer which is not in the VPC:
But the web server connection string is not working...
I am not entirely sure if it's even possible to use an NLB for this purpose?

How to ensure a MySql connection through Tomcat 8?

I have a webapp that is running properly in Tomcat without the database.
If I add the following to tomcat context.xml it seems to kill the whole server, I get a blank page and connection refused for my app and the tomcat manager app:
<Context docBase="${catalina.base}/webapps/ROOT.war">
<Parameter name="application.properties" value="/home/users/tomcat/application.properties"/>
</Context>
There is an application.properties file in /home/users/tomcat/ which is the same user that runs tomcat. So how come this line is bringing my server down?
MySQL 5.5 is running and the properties file I used is:
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=automation
spring.datasource.password=xxxxxxxxxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#comment out below line to create schema on new database
spring.jpa.hibernate.ddl-auto=update
How do I troubleshoot the database connectivity?
I know mysql is configured for port 3306 and it will let me login with the details in that file, if I use SSH.

Grails / Tomcat: Database connections get stuck in sleep state

I have a Grails application running under Tomcat 7 and am having problems managing MySQL connections.
The problem is that each new request to the application (i.e., a page load) is creating a new MySQL connection, and these connections are not closing. Instead, they stay in a SLEEP state until the MySQL server finally refuses to accept more connections. Therefore, simply by reloading individual pages on the site at a fast rate, one can create numerous database connections.
So it seems that my connection pool isn't closing the connections with MySQL fast enough. There are a number of configuration settings for the connection pool, but I'm not sure what needs to be tuned to avoid this problem.
Here is the configuration from my context.xml file:
<Resource name="jdbc/Production" auth="Container" type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
minEvictableIdleTimeMillis="1800000"
timeBetweenEvictionRunsMillis="1800000"
numTestsPerEvictionRun="3"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
testOnBorrow="true"
testWhileIdle="true"
testOnReturn="true"
validationQuery="SELECT 1"
username=""
password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/Production"
/>
Thanks very much for any suggestions.
You did not define a connection pool.
Add following code to the context.xml(it seems to be a JNDI data source):
pooled = "true"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
If you haven't put JDBC Pool in to your dependency configuration, add following to the plugins closure in BuildConfig.groovy:
compile ":jdbc-pool:1.0.9.3"
You may use other connection pools, but JDBC Pool will be my recommendation.

How to manage connection to database?

I should manage a connection to a database from my web application (Tomcatserver/frameworkJSF) ,theconnection from my login page until the deconnection.
I have found that I should let the container manage the connection/deconnection to the database but the example that I found show how to configure tomcat server (http://christophej.developpez.com/tutoriel/j2ee/pooltomcat/)and use the connection from a servlet but I want to use the connection from my beans.
how can I instanciate a connection in my classess each time without creation of a new connection?
You create a datasource in Tomcat or somewhere else which is a pool of connections. You take a connection from that pool and then release it when you're done with it. Tomcat example (in context.xml):
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/yourname"
password="..." type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8"
username="..."/>
This creates a pool with a JNDI name 'jdbc/yourname'. You can get this from Spring like this:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/yourname" />
This will create a DataSource object that you can inject into your Java code as needed.
Or there are plenty of JNDI lookup examples on the web to do this programmatically instead of declaratively.
There are also loads of pool implementations, like c3po.