Cannot connect Bitbucket Server to MySQL database

Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.

Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Symptoms

Bitbucket Server fail to connect to MySQL database. The following appears in the atlassian-bitbucket.log:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 2012-11-27 21:38:56,806 INFO [http-7990-5] 1298x15x1 1aan3w6 0:0:0:0:0:0:0:1%0 "POST /setup HTTP/1.1" c.a.s.i.m.DefaultMigrationService Setting up external database at jdbc:mysql://localhost:3306/bitbucket131?autoReconnect=true&characterEncoding=utf8&useUnicode=true&sessionVariables=storage_engine%3DInnoDB 2012-11-27 21:39:01,305 WARN [http-7990-5] 1298x15x1 1aan3w6 0:0:0:0:0:0:0:1%0 "POST /setup HTTP/1.1" c.a.s.i.m.DefaultMigrationService A connection could not be opened with the DataSource com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_37] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_37] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_37] at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_37] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.17.jar:na] at com.mysql.jdbc.Util.getInstance(Util.java:386) ~[mysql-connector-java-5.1.17.jar:na] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013) ~[mysql-connector-java-5.1.17.jar:na] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[mysql-connector-java-5.1.17.jar:na] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[mysql-connector-java-5.1.17.jar:na] ... Caused by: java.sql.SQLException: Unexpected exception encountered during query. ... Caused by: java.lang.NullPointerException: null at com.mysql.jdbc.ConnectionImpl.getServerCharacterEncoding(ConnectionImpl.java:3100) ~[mysql-connector-java-5.1.17.jar:na] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2095) ~[mysql-connector-java-5.1.17.jar:na]

Cause

This is a bug in MySQL driver 5.1.16 which causes an error in the connection when the JDBC URL has both "sessionVariables" and "characterEncoding" parameters at the same time (ie. url="jdbc:mysql://localhost:3306/bitbucket131?autoReconnect=true&characterEncoding=utf8&useUnicode=true&sessionVariables=storage_engine%3DInnoDB")

Resolution

Upgrade to the latest MySQL driver version.

Tested to be working with MySQL driver 5.1.22

Updated on April 8, 2025

Still need help?

The Atlassian Community is here for you.