#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2015, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
# 

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

#
#
# Usage: zmmycnf [--innodb-buffer-pool-memory-percent number].  
# 
# We don't error check - if you provide wrong arguments we default the
# buffer pool percent to keep things simple for the installer.
#
bufferPoolPercent=25
if [ "x$1" = "x--innodb-buffer-pool-memory-percent" ];
then
    if echo "$2" | grep -q "^[0-9]*$"
    then
        if [ $2 -gt 1 -a $2 -lt 100 ]; then
            bufferPoolPercent=$2
        fi
    fi
fi

#
# Calculate innodb buffer pool size
#
memKB=$(zmsysmemkb)
((bufferPoolSize=memKB * 1024 * bufferPoolPercent / 100))

#
# Tell the JDBC driver how many mysql active connections to use.
#
/opt/zimbra/bin/zmlocalconfig -e zimbra_mysql_connector_maxActive=100

#
# Write config to stdout
#
cat<<EOF

[mysqld]

basedir        = /opt/zimbra/common
datadir        = ${mysql_data_directory}
socket         = ${mysql_socket}
pid-file       = ${mysql_pidfile}
bind-address   = ${mysql_bind_address}
port           = ${mysql_port}
user           = ${zimbra_mysql_user}
tmpdir         = ${zimbra_tmp_directory}

external-locking
slow_query_log = 1
slow_query_log_file = ${zimbra_log_directory}/myslow.log

general_log_file = ${zimbra_log_directory}/mysql-mailboxd.log

long_query_time  = 1
log_queries_not_using_indexes

thread_cache_size = 110
max_connections   = 110

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

sort_buffer_size = 1048576
read_buffer_size = 1048576

# (Num mailbox groups * Num tables in each group) + padding
table_open_cache = 1200

innodb_data_file_path          = ibdata1:10M:autoextend
innodb_buffer_pool_size        = ${bufferPoolSize}
innodb_log_file_size           = 524288000
innodb_log_buffer_size         = 8388608
innodb_file_per_table

# Value is: 200 + max_connections + 2 * table_open_cache
innodb_open_files              = 2710

innodb_max_dirty_pages_pct     = 30
innodb_flush_method            = O_DIRECT
innodb_flush_log_at_trx_commit = 0
max_allowed_packet             = 16777216

[mysqld_safe]

log-error      = ${zimbra_log_directory}/mysqld.log
pid-file     = ${mysql_pidfile}


EOF
