#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2004, 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

source /opt/zimbra/bin/zmshutil || exit 1
zmsetvars

#
# Memory for use by MySQL.
#
memkb=$(zmsysmemkb)
((mymem=memkb * 1024 * mysql_memory_percent / 100))

#
# Allocate 75% of memory that mysql can use to buffer pool.
#
((computed_innodb_buffer_pool_size = mymem * 75 / 100))

#
# Calculate the number of connections allowed.  The magic '2097152' is
# stack size per connection.  Ignoring key_buffer_size as we don't use
# MyISAM.
#
((non_buffer_pool_memory = mymem - computed_innodb_buffer_pool_size))
((per_connection_memory = 2097152 + mysql_sort_buffer_size + mysql_read_buffer_size + mysql_innodb_log_buffer_size))
((computed_max_connections = non_buffer_pool_memory / per_connection_memory))

#
# If we have less RAM, fall back on some graceful defaults.
#
if [ ${computed_max_connections} -lt 10 ]; then
    computed_max_connections=10;
    ((mysql_read_buffer_size = non_buffer_pool_memory / computed_max_connections * 1 / 10))
    ((mysql_sort_buffer_size = non_buffer_pool_memory / computed_max_connections * 1 / 10))
    ((mysql_innodb_log_buffer_size = non_buffer_pool_memory / computed_max_connections * 8 / 10))
fi

#
# Tell the JDBC driver how many mysql active connections to use.
#
${zimbra_home}/bin/zmlocalconfig -e zimbra_mysql_connector_maxActive=${computed_max_connections}

#
# Give some slack for command line mysql invocations.
#
((computed_max_connections = computed_max_connections + 4))

#
# Write config to stdout
#
cat<<EOF

[mysqld]

basedir      = ${mysql_directory}
datadir      = ${mysql_data_directory}
socket       = ${mysql_socket}
pid-file     = ${mysql_pidfile}
bind-address = ${mysql_bind_address}
port         = ${mysql_port}
user         = ${zimbra_mysql_user}

skip-external-locking

log-slow-queries = ${zimbra_log_directory}/myslow.log
long-query-time  = 1
log-long-format
log-queries-not-using-indexes

thread_cache      = ${computed_max_connections}
max_connections   = ${computed_max_connections}

# We do a lot of writes, query cache turns out to be not useful.
query_cache_type = 0

sort_buffer_size = ${mysql_sort_buffer_size}
read_buffer_size = ${mysql_read_buffer_size}

# Increase the size of the table cache, since each mailbox has its
# own set of tables
table_cache = ${mysql_table_cache}

innodb_buffer_pool_size   = ${computed_innodb_buffer_pool_size}
innodb_log_file_size      = ${mysql_innodb_log_file_size}
innodb_log_buffer_size    = ${mysql_innodb_log_buffer_size}
innodb_file_per_table
innodb_open_files         = ${mysql_table_cache}

[mysqld_safe]

err-log = ${zimbra_log_directory}/mysqld.log

EOF
