!/bin/sh

######################################################
# Author: yanglin05 wangjianying
# Email: yanglin05@baidu.com wangjianying@baidu.com

disable gcc 4.8.2 and bison 2.3+
#export PATH=”${PATH/\/opt\/compiler\/gcc-4.8.2\/bin/}”
#export TERMINFO_DIRS=

################################################################################
function log()
{
local working_path=$PWD
local timestamp=$(date +%Y%m%d-%H:%M:%S)
echo “[$timestamp][$working_path]$1”
}

function do_cmd()
{
log “[exec] $* ……”
$@
if [ $? -ne 0 ];then
log “[fail] $@”
exit 1
else
log “[succ] $@”
fi
}

function generatepwd()
{
local MATRIX=’0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!%^&*()
+=’
local LENGTH=30
local PASSWORD=””
local n=1
while [ ${n} -le $LENGTH ]
do
PASSWORD=”${PASSWORD}${MATRIX:$((RANDOM%${#MATRIX})):1}”
let n+=1
done
echo “$PASSWORD”
}

function generate_sql_cnf()
{
local BASEDIR=$1
local DATADIR=$2
local PORT=$3
local SERVER_ID=$4

  1. local root_pwd=`generate_pwd`<br /> local admin_pwd=`generate_pwd`<br /> local mysqlsync_pwd=`generate_pwd`
  2. cat > init.sql << ==EOF==<br />SET SQL_LOG_BIN=0;

— clear mysql-bin.00000* create by mysql_install_db
— RESET MASTER;

— clear dirty users
DELETE FROM mysql.user WHERE user=’’;
DELETE FROM mysql.db WHERE user=’’;
DELETE FROM mysql.user WHERE host LIKE ‘%-%’;
DELETE FROM mysql.db WHERE host LIKE ‘%-%’;

— change password for root
UPDATE mysql.user SET password=PASSWORD(“${root_pwd}”) WHERE user=’root’;
UPDATE mysql.user SET password=PASSWORD(“${admin_pwd}”) WHERE user=’admin’;

— create admin users
GRANT SELECT,RELOAD,PROCESS,SHOW DATABASES,SUPER,LOCK TABLES,REPLICATION CLIENT ON . TO ‘admin’@’localhost’ IDENTIFIED BY “${admin_pwd}” WITH GRANT OPTION;
GRANT SELECT,RELOAD,PROCESS,SHOW DATABASES,SUPER,LOCK TABLES,REPLICATION CLIENT ON . TO ‘admin’@’127.0.0.1’ IDENTIFIED BY “${admin_pwd}” WITH GRANT OPTION;
GRANT REPLICATION CLIENT,REPLICATION SLAVE ON . TO ‘mysqlsync’@’%’ IDENTIFIED BY “${mysqlsync_pwd}”;
GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY “${root_pwd}”;

— reset privileges and replication status;
flush privileges;
reset master;
reset slave;
==EOF==

cat > $BASEDIR/etc/mysqld.cnf << ==EOF==<br />[mysqld]<br />server-id   = ${SERVER_ID}<br />read-only    = $is_slave<br />==EOF==

cat > $BASEDIR/etc/user.root.cnf << ==EOF==<br />[client]<br />user=root<br />password=$root_pwd<br />socket=${BASEDIR}/tmp/mysql.sock<br />==EOF==

cat > $BASEDIR/etc/user.admin.cnf << ==EOF==<br />[client]<br />user=admin<br />password=$admin_pwd<br />socket=${BASEDIR}/tmp/mysql.sock<br />==EOF==

cat > $BASEDIR/etc/user.mysqlsync.cnf << ==EOF==<br />[client]<br />user=mysqlsync<br />password=$mysqlsync_pwd<br />socket=${BASEDIR}/tmp/mysql.sock<br />==EOF==

cat > $BASEDIR/etc/my.cnf << ==EOF==<br />[client]<br />port                     = $PORT<br />socket                   = $BASEDIR/tmp/mysql.sock

[mysqld]
core-file

!include $BASEDIR/etc/mysqld.cnf

port = $PORT
socket = $BASEDIR/tmp/mysql.sock
pid-file = $DATADIR/mysql.pid
basedir = $BASEDIR
datadir = $DATADIR

tmp dir settings
tmpdir = $BASEDIR/tmp
slave-load-tmpdir = $BASEDIR/tmp


language = $BASEDIR/share/mysql/english
character-sets-dir = $BASEDIR/share/mysql/charsets

skip options
skip-name-resolve
skip-symbolic-links
skip-external-locking
skip-slave-start

sysdate-is-now

res settings
back_log = 50
max_connections = 1000
max_connect_errors = 10000
open_files_limit = 10240

connect-timeout = 5
wait-timeout = 28800
interactive-timeout = 28800
slave-net-timeout = 600
net_read_timeout = 30
net_write_timeout = 60
net_retry_count = 10
net_buffer_length = 16384
max_allowed_packet = 64M


table_cache = 512
thread_stack = 192K
thread_cache_size = 20
thread_concurrency = 8

qcache settings
query_cache_type = 1
query_cache_size = 32M
#query_cache_size = 256M
#query_cache_limit = 2M
#query_cache_min_res_unit = 2K

default settings
# time zone
default-time-zone = system
character-set-server = utf8
default-storage-engine = InnoDB

tmp & heap
tmp_table_size = 512M
max_heap_table_size = 512M

rollback-xa-trans-when-disconnect = 0

log-bin = mysql-bin
log-bin-index = mysql-bin.index
sync_binlog = 1
binlog-format = STATEMENT
binlog_cache_size = 1M
max_binlog_size = 1G
expire_logs_days = 7
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%

log-slave-updates = 1

relay-log = relay-log
relay_log_index = relay-log.index
max_relay_log_size = 1G
relay-log-purge = 1

gtid and semi-sync
rpl_group_id
==EOF==

if [ $is_slave -eq 1 ]
then
cat >> $BASEDIR/etc/my.cnf << ==EOF==
rpl_group_id_act_as_root = 0
rpl_semi_sync_slave_enabled = 1
innodb_committed_read_view_mode = 0
==EOF==
else
cat >> $BASEDIR/etc/my.cnf << ==EOF==
rpl_group_id_act_as_root = 1
rpl_semi_sync_master_enabled = 1
innodb_committed_read_view_mode = 1
==EOF==
fi

cat >> $BASEDIR/etc/my.cnf << ==EOF==

warning & error log
log-warnings = 2
log-error = $BASEDIR/log/mysql.err

slow query log
long-query-time = 1
slow_query_log = 1
slow_query_log_file = $BASEDIR/log/slow.log
log-queries-not-using-indexes

general query log
general-log = 1
general_log_file = $BASEDIR/log/mysql.log

key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
join_buffer_size = 8M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover

transaction_isolation = REPEATABLE-READ

skip-innodb

innodb_file_per_table

innodb_status_file = 1
innodb_open_files = 2048
# committed read views are mallocted from additional mem pool
innodb_additional_mem_pool_size = 512M
innodb_buffer_pool_size = 2G
innodb_data_home_dir = $DATADIR
innodb_data_file_path = ibdata1:100M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1

innodb_log_buffer_size = 8M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_log_group_home_dir = $DATADIR

innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 50
#innodb_flush_method = O_DSYNC

[mysqldump]
quick
max_allowed_packet = 64M

[mysql]
auto-rehash
default-character-set = utf8
connect-timeout = 3

[isamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

==EOF==
}

################################################################################
if [ $# -ne 6 ]
then
echo “Usage: $0 mysql_src_dir deployment_dir data_dir mysqld_port is_slave(0 or 1) release_version(0 or 1)”
echo “ all path name should use absolute name.”
exit 1
else
mysql_src_dir=$1
deployment_dir=$2
data_dir=$3
mysqld_port=$4
is_slave=$5
release_version=$6
if [ $release_version -ne 0 ]<br />    then<br />        export CFLAGS='-g -O2'<br />        export CXXFLAGS='-g -O2'<br />    else<br />        export CFLAGS='-g'<br />        export CXXFLAGS='-g'<br />    fi

if [ -d $deployment_dir ]<br />    then<br />         echo "[fail] deployment directory $deployment_dir already exists!"<br />         exit 1<br />     fi<br />     if [ -d $data_dir ]<br />     then<br />         echo "[fail] data directory $data_dir already exists!"<br />         exit 1<br />     fi<br />fi

sec=$(date +%S)
server_id=$(ping -c 1 hostname | head -n 1 | awk ‘{print $3}’ | sed -r ‘s/[().\s]//g’)
server_id=”${server_id}${sec}”
server_id=$((server_id%4294967295))

cur_dir=$PWD

do_cmd cd $mysql_src_dir

do_cmd ./configure \
—prefix=${deployment_dir} \
—with-plugins=myisam,innobase \
—enable-profiling \
—enable-assembler \
—with-charset=latin1 \
—with-extra-charsets=all \
—enable-local-infile \
—enable-thread-safe-client \
—without-readline \
—without-libedit \
—with-federated-storage-engine

do_cmd make -j 8
do_cmd make install
do_cmd make clean

do_cmd cd ${deployment_dir}
if [ ! -d etc ]
then
do_cmd mkdir etc
fi
if [ ! -d log ]
then
do_cmd mkdir log
fi
if [ ! -d tmp ]
then
do_cmd mkdir tmp
fi
if [ ! -d ${data_dir} ]
then
do_cmd mkdir -p ${data_dir}
fi

do_cmd generate_sql_cnf $deployment_dir $data_dir $mysqld_port $server_id
do_cmd ./bin/mysql_install_db —defaults-file=etc/my.cnf
do_cmd ./share/mysql/mysql.server start
do_cmd ./bin/mysql -uroot -S tmp/mysql.sock < init.sql

make group_id reset to 0
do_cmd ./share/mysql/mysql.server stop
do_cmd rm ${data_dir}/mysql-bin*
do_cmd ./share/mysql/mysql.server start

do_cmd rm -f init.sql
log “Bingo!”