Accessing Bitbucket Server's H2 embedded 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
Summary
While the H2 database is not supported for production environments, sometimes admins will need access for testing purposes or you may need to run some diagnostics on a Bitbucket mirror's H2 database.
Solution
Before accessing or modifying the H2 embedded database, it's always recommended to first make a backup of $BITBUCKET_HOME/shared/data/db.h2.db
Stop Bitbucket
Login to the server as the user that runs Bitbucket
Run the following command which will open a console connection to the database:
1
java -cp <BITBUCKET_INSTALLATION>/app/WEB-INF/lib/h2-1.3.176.jar org.h2.tools.Shell
Update the path above to reflect where Bitbucket Server is installed
When prompted for the URL enter:
1
jdbc:h2:file://<BITBUCKET_HOME>/shared/data/db;DB_CLOSE_ON_EXIT=TRUE
Update the path above to reflect the location of the Bitbucket Home directory
Press enter to select default (org.h2.Driver) when asked which driver to use
Enter sa for the user
Press enter twice when prompted for the password (no password is required)
If successful, you'll be prompted with sql>
and can enter any SQL commands required.
Command Line Options
It is also possible to combine the steps to connect to the database in a single command:
1
java -cp <BITBUCKET_INSTALLATION>/app/WEB-INF/lib/h2-1.3.176.jar org.h2.tools.Shell -url "jdbc:h2:file://<BITBUCKET_HOME>/shared/data/db;DB_CLOSE_ON_EXIT=TRUE" -driver org.h2.Driver -user sa
To connect and then run a query, pass the -sql
option and the SQL query, for example:
1
java -cp <BITBUCKET_INSTALLATION>/app/WEB-INF/lib/h2-1.3.176.jar org.h2.tools.Shell -url "jdbc:h2:file://<BITBUCKET_HOME>/shared/data/db;DB_CLOSE_ON_EXIT=TRUE" -driver org.h2.Driver -user sa -sql "SELECT * FROM app_property;"
Update the paths above to reflect the location of the Bitbucket installation and home directories
Accessing the H2 database from a browser
For easier navigation of the H2 database, use the Server mode using the command below.
1
java -cp <BITBUCKET_INSTALLATION>/app/WEB-INF/lib/h2*.jar org.h2.tools.Server
The H2 console can be accessible using http://localhost:8082. Enter the JDBC URL and click on the Connect button.
Was this helpful?