Prepare the MySQL database for the Tigase server

Submitted by kobit (not verified) on Thu, 2009-11-26 01:15

This guide describes how to prepare the MySQL database for connecting the Tigase server to it.

Basic setup

The MySQL database can be prepared in many ways. Most of Linux distributions contain tools which allow you to go through all steps from the shell command line. To make sure, however it works on all platforms the same way I show first how to do it under MySQL command line client.

Configuring from MySQL command line tool

Run the MySQL command line client in either Linux or MS Windows environment and enter following instructions:

  1. Create the database for the Tigase server:
    mysql> create database tigasedb;
    
  2. Add the tigase_user user and grant him access to the tigasedb database. Depending on how you plan to connect to the database (locally or over the network) use one of following commands or all if you are not sure:
    Grant access to tigase_user connecting from any network address.
    mysql> GRANT ALL ON tigasedb.* TO tigase_user@'%'
                    IDENTIFIED BY 'tigase_passwd';
    

    Grant access to tigase_user connecting from localhost.

    mysql> GRANT ALL ON tigasedb.* TO tigase_user@'localhost'
                    IDENTIFIED BY 'tigase_passwd';
    

    Grant access to tigase_user connecting from local machine only.

    mysql> GRANT ALL ON tigasedb.* TO tigase_user
                    IDENTIFIED BY 'tigase_passwd';
    

    For the Tigase server version 4.x additional permissions must be granted for the database user:

    mysql> GRANT SELECT, INSERT, UPDATE ON mysql.proc TO 'tigase_user'@'localhost';
    mysql> GRANT SELECT, INSERT, UPDATE ON mysql.proc TO 'tigase_user'@'%';
    mysql> GRANT SELECT, INSERT, UPDATE ON mysql.proc TO 'tigase_user';
    
    

    And now you can update user permission changes in the database:

    mysql> FLUSH PRIVILEGES;
    
  3. Load database schema to initialize the Tigase server database space. First, switch to the database you have just created:
    mysql> use tigasedb;
    

    Assuming you run the mysql client in Linux from the Tigase installation directory. If you run the Tigase server all versions below 4.0:

    mysql> source database/mysql-schema.sql;
    

    For the Tigase server version 4.x you have to use proper schema version:

    mysql> source database/mysql-schema-4.sql;
    

    On Windows you have probably to enter the full path:

    mysql> source c:/Program Files/Tigase/database/mysql-schema.sql;
    

    The initialization schema file should be also available locally in database/ directory of your Tigase installation.

Configuring from the Linux shell command line

Follow steps below to prepare the MySQL database:

  1. Create the database space for the Tigase server:
    mysqladmin -p create tigasedb
    
  2. Add the tigase_user user and grant him access to the tigasedb database. Depending on how you plan to connect to the database (locally or over the network) use one of following commands or all if you are not sure:
    Grant access to tigase_user connecting from any network address.
    echo "GRANT ALL ON tigasedb.* TO tigase_user@'%' \
                    IDENTIFIED BY 'tigase_passwd'; \
                    FLUSH PRIVILEGES;" | mysql -u root -pdbpass mysql
    

    Grant access to tigase_user connecting from localhost.

    echo "GRANT ALL ON tigasedb.* TO tigase_user@'localhost' \
                    IDENTIFIED BY 'tigase_passwd'; \
                    FLUSH PRIVILEGES;" | mysql -u root -pdbpass mysql
    

    Grant access to tigase_user connecting from local machine only.

    echo "GRANT ALL ON tigasedb.* TO tigase_user \
                    IDENTIFIED BY 'tigase_passwd'; \
                    FLUSH PRIVILEGES;" | mysql -u root -pdbpass mysql
    
  3. Load database schema to initialize the Tigase server (version below 4.0) database space:
    mysql -u dbuser -p tigasedb < mysql-schema.sql
    

    For the Tigase server version 4.0 and later:

    mysql -u dbuser -p tigasedb < mysql-schema-4.sql
    

    The initialization schema file should be also available locally in database/ directory of your Tigase installation.

Configuring MySQL for UTF-8 support

In the my.conf put following lines:

  1. [mysql]
  2. default-character-SET=utf8
  3. [client]
  4. default-character-SET=utf8
  5. [mysqld]
  6. init_connect='SET collation_connection = utf8_general_ci; SET NAMES utf8;'
  7. character-set-server=utf8
  8. default-character-SET=utf8
  9. collation-server=utf8_general_ci
  10. skip-character-set-client-handshake

 

 

Then connect to the database and from the command line shell check settings:

  1. SHOW VARIABLES LIKE 'character_set_database';
  2. SHOW VARIABLES LIKE 'character_set_client';

 

If any of these shows something else then 'utf8' then you have to correct it:

  1. ALTER DATABASE tigasedb DEFAULT CHARACTER SET utf8;

 

You can now also test your database installation if it accepts UTF-8 data. Best way is just to create an account with UTF-8 characters:

call TigAddUserPlainPw('żółw@some.domain.com', 'żółw');

 

And then check of the account has been created:

  1. SELECT * FROM tig_users WHERE user_id = 'żółw@some.domain.com';

 

If the last command gives you no results it means there is still something wrong with settings. You might also check you shell settings to make sure your command line shell supports UTF-8 characters and passes them correctly to MySQL:

  1. export LANG=en_US.UTF-8
  2. export LOCALE=UTF-8
  3. export LESSCHARSET='utf-8'

 

It seems to me that MySQL 5.0.x also needs an extra parameters in the connection string: '&useUnicode=true&characterEncoding=UTF-8' while MySQL 5.1.x seems to not need it but it doesn't hurt to have it for both versions. You have to edit 'etc/init.properties' file and append this to the database connection string.

For MySQL 5.1.x, however, you need also updated code for all database stored procedures and functions used by the Tigase. They are updated for Tigase version 4.4.x and up, for the time being if you use older version of the Tigase server you can reload stored procedures using the file from SVN.

 Other MySQL setting worth considering

There is a number of other options useful, especially for a performance reasons. Please note, you have to review them as some of them may impact data reliability and are useful for performance or load tests installations only.

  1. # InnoDB seems to be a better choice
  2. # so lets make it a default DB engine
  3. default-storage-engine = innodb

 

Some the general MySQL settings which mainly affect performance:

  1. key_buffer = 64M
  2. max_allowed_packet = 32M
  3. sort_buffer_size = 64M
  4. net_buffer_length = 64K
  5. read_buffer_size = 16M
  6. read_rnd_buffer_size = 16M
  7. thread_stack = 192K
  8. thread_cache_size = 8
  9. query_cache_limit = 10M
  10. query_cache_size = 64M

 

InnoDB specific settings:

  1. # Keep data in a separate file for each table
  2. innodb_file_per_table = 1
  3. # Allocate memory for data buffers
  4. innodb_buffer_pool_size = 1000M
  5. innodb_additional_mem_pool_size = 100M
  6. # A location of the MySQL database
  7. innodb_data_home_dir = /home/databases/mysql/
  8. innodb_log_group_home_dir = /home/databases/mysql/
  9. # The main thing here is the 'autoextend' property
  10. # without it your data file may reach maximum size and
  11. # no more records can be added to the table.
  12. innodb_data_file_path = ibdata1:10M:autoextend
  13. innodb_log_file_size = 10M
  14. innodb_log_buffer_size = 32M
  15. # Some other performance affecting settings
  16. innodb_flush_log_at_trx_commit = 2
  17. innodb_lock_wait_timeout = 50
  18. innodb_thread_concurrency = 16

 

I am certainly not a database expert nor MySQL expert and I do not pretend to be one. So any comments or suggestions you may have are very welcome and appreciated.