# Database Installation Guide

## Introduction

Openfire stores all data in a back-end database. If you choose to not use the embedded database, you must setup and manage an external database for the application. This document provides setup instructions for all the currently supported databases.

JDBC drivers are required for Openfire to communicate with your database. Suggested drivers for particular databases are noted below where applicable. Openfire bundles JDBC drivers for MySQL, Postgres, Microsoft SQL Server, and HSQLDB.

All supported database schemas can be found in the `resources/database` directory of the Openfire installation.

Note that additional databases besides the ones in this list may be supported. Please check the distribution for a database schema for your database or consider creating a schema yourself if one isn't provided.

Choose your database from the list below for setup details:

## MySQL

### JDBC Drivers

The JDBC driver for MySQL is bundled with Openfire, so you do not need to download and install a separate driver.

In the Openfire setup tool, use the following values:

- driver: `com.mysql.jdbc.Driver`
- server: `jdbc:mysql://HOSTNAME/DATABASENAME`

where HOSTNAME and DATABASENAME are the actual values for you server. In many cases `localhost` is a suitable value for HOSTNAME when your database is running on the same server as your webserver.

### Setup Instructions

1. Make sure that you are using recent and supported by Oracle MySQL version.
2. Create a database for the Openfire tables:
   - `mysqladmin create DATABASENAME`
   (note: "DATABASENAME" can be something like 'openfire')
3. Import the schema file from the `resources/database` directory of the installation folder:
   - Unix/Linux: `cat openfire_mysql.sql | mysql DATABASENAME;`
   - Windows: `type openfire_mysql.sql | mysql DATABASENAME;`
4. Start the Openfire setup tool, and use the appropriate JDBC connection settings.

### Character Encoding Issues

MySQL does not have proper Unicode support, which makes supporting data in non-Western languages difficult. However, the MySQL JDBC driver has a workaround which can be enabled by adding this value to the URL of the JDBC driver:

- `?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8`

You can edit the conf/openfire.xml file to add this value.

Note: If the mechanism you use to configure a JDBC URL is XML-based, you will need to use the XML character literal `&amp;` to separate configuration parameters, as the ampersand is a reserved character for XML.

### Further Help

If you need help setting up MySQL, refer to the following site:

- MySQL Connector/J Configuration Properties

## Oracle

### JDBC Drivers

The Oracle JDBC drivers cannot readily be distributed with Openfire, so must be manually downloaded from Oracle's website. Various versions of the drivers are available, but the most recent driver compatible with your version of Oracle is recommended for use with Openfire. Copy the JDBC driver to the `lib/` directory of your Openfire installation.

In the Openfire web-based setup tool, use the following values:

- driver: `oracle.jdbc.driver.OracleDriver`
- server: `jdbc:oracle:thin:@HOST:PORT/SERVICE`

where HOST, PORT and SERVICE are the actual values for you server. In many cases `localhost` and `1521` are suitable values for HOST and PORT when your database is running on the same server as your webserver. Ask your DBA for the service name.

### Setup Instructions

1. Create an "Openfire" user or some other user that will "own" the Openfire tables. This isn't necessary, but doing so will allow your tables to exist in a separate tablespace.
2. Import the schema from the `resources/database` directory of the installation using sqlplus (or your favorite Oracle tool such as Toad). For sqlplus: copy the `openfire_oracle.sql` file to the same directory where sqlplus is installed (should be something like `/Oracle/Ora81/bin/`). Next, log into sqlplus and then execute the command:
   - `@ openfire_oracle`
   That will import the schema file. Your database is now setup.

## Microsoft SQL Server

### JDBC Drivers

Openfire bundles two SQL Server compatible JDBC drivers.

1. The Free TDS JDBC driver
   - driver: `net.sourceforge.jtds.jdbc.Driver`
   - server: `jdbc:jtds:sqlserver://HOSTNAME:PORTNUMBER/DATABASENAME;appName=Openfire`
2. The Microsoft JDBC driver
   - driver: `com.microsoft.sqlserver.jdbc.SQLServerDriver`
   - server: `jdbc:sqlserver://HOSTNAME:PORTNUMBER;databaseName=DATABASENAME;applicationName=Openfire`

You can use either of these, but the Microsoft driver is more up to date.

For the above server settings, HOSTNAME is the host name or IP address of your database server, PORTNUMBER is the port that SQL Server is listening on (normally 1433) and DATABASENAME is the name of the database (this parameter is optional).

### Setup Instructions

1. Create a new database using Enterprise Manager if you do not already have a database you want to use. You may wish to name the database "openfire".
2. Create a user to access the database if you do not already have one you'd like to use. Consider making the default database for the user be the one you created in step 1.
3. Proceed to the Openfire setup tool and use the appropriate JDBC settings when prompted.

## PostgreSQL

### JDBC Drivers

The JDBC driver for PostgreSQL is bundled with Openfire, so you do not need to download and install a separate driver.

In the Openfire web-based setup tool, use the following values:

- driver: `org.postgresql.Driver`
- server: `jdbc:postgresql://HOSTNAME:PORTNUMBER/DATABASENAME`

If left out, host defaults to localhost (not 127.0.0.1) for HOSTNAME and 5432 for PORT-NUMBER.

### Setup Instructions

A general best practice is to create a PostgreSQL user dedicated for Openfire's use. This user need not be a super-user, but should own the dedicated database used by Openfire.

1. Create a user for Openfire to connect as:
   - `createuser -P openfireuser`
2. Create the database owned by this openfire user. For example, using the PostgreSQL `"createdb"` utility:
   - `createdb -E UNICODE -O openfireuser openfire`
3. Proceed with Openfire install setup via the admin console, the setup process with install the schema.

### Further Help

If you need help setting up Postgres, refer to the following site:

- Postgres Configuration and Connection documentation

## IBM DB2 7

### JDBC Drivers

Use the JDBC 1.2 compliant driver, db2java.zip (1293KB), located in the "%DB2PATH%\java2" directory. Copy the JDBC driver to the `lib/` directory of your Openfire installation.

Values for the config file are:

- driver: `COM.ibm.db2.jdbc.app.DB2Driver`
- server: `jdbc:db2:[DB NAME, e.g. OPENFIRE]`

### Setup Instructions

1. You have to create a new database (e.g. OPENFIRE) or use an existing one. Connect to the database using any valid username allowed to create tables and indexes. Keep in mind that DB2 UDB applies the username as the table schema. Use the same user when you configure Openfire with the administration interface. Commands are as follows:

       DB2 CREATE DB OPENFIRE ALIAS OPENFIRE
       DB2 CONNECT TO OPENFIRE USER [DBUSER] USING [DBPASSWORD]

   (or you can use the Command Center GUI)
2. Start the DB2 command window (in Windows), or the user's shell in Unix and import the schema in the `resources/database` directory of the installation with:

       db2 -tvf openfire_db2.sql

   Or, use the DB2 Command Center and run the script through the "Replication Sources" folder in the Database tree.

## HSQLDB

Special Note

Openfire bundles hsqldb as its embedded database. If you choose to use the embedded database, it can be configured via the Openfire installer. If you have a stand-alone installation of HSQLDB, follow the instructions below to connect to it.

### JDBC Drivers

Because hsqldb is embedded in Openfire, there is no need to download the JDBC driver separately. Values for the config file are:

- driver: `org.hsqldb.jdbcDriver`
- server: `dbc:hsqldb:PATH_TO_DB_FILES/DB_NAME`

For example, the server URL might be `jdbc:hsqldb:../database/defaultdb`

### Setup Instructions

1. Create a database for the Openfire tables. This assumes you have HSQLDB installed in its own directory and working correctly.
2. Run the HSQLDB Database Manager by navigating to the folder where hsqldb.jar is located and type the following:
   - `java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing`
   then hit enter. You will now see a GUI application. Note, you will not see a hsqldb.jar in the Openfire distribution - this is referring to the hsqldb.jar from the HSQLDB distribution.
3. When prompted with the connect window, enter `org.hsqldb.jdbcDriver` as the Driver, and specify the URL of your database in the URL field. The default password is already entered.
4. Now copy and paste the contents of `openfire_hsql.sql` from the `resources/database` directory of the installation into the command window, and hit execute. If no errors are reported, then your database is setup properly.
5. Finally, you should change the username and password login for your database: just open the `.script` file in your database folder and change the appropriate values. Make sure that you don't use a blank password, as Openfire will not accept one.

### Further Reading

- Database clustering guide
- Database schema guide