######################################################################
####Time:2020/10/19
####Author:baoziqiang
####
####
######################################################################

#useage :  sh install_mysql.sh master/slave username password masterIP slaveIP 
pd=$PWD
role=$1
username=$2
password=$3
mysql_password="`echo ${3}|sed -e 's/&/\\\\\\&/g' -e 's/\//\\\\\\//g'`"
DBport=$4
masterIP=$5
slaveIP=$6
VIP=$7
pd=$PWD
mysql_version='mysql-8.0.26-linux-glibc2.12-x86_64'
#mysql_version='mysql-8.0.26-linux-glibc2.17-x86_64-minimal-rebuild'
mysql_data=$pd/mysql$DBport
set -x
function InstallPrepare
{
	if [ -d $mysql_data ];then
		rm -rf $mysql_data
		rm -f /etc/my.cnf
		mkdir -p $mysql_data/etc
		mkdir -p $mysql_data/data
		mkdir -p $mysql_data/log/relay-log
	else
		rm -f /etc/my.cnf
		mkdir -p $mysql_data/etc
		mkdir -p $mysql_data/data
		mkdir -p $mysql_data/log/relay-log
	fi
	rm -f /usr/local/mysql $pd/mysql/mysql
	tar -xvf mysql.tar.gz 
	tar xvJf $pd/mysql/$mysql_version.tar.xz -C $pd/mysql/
	\cp -rf $pd/mysql/$mysql_version/lib/private/libssl.so.1.1 /lib64/
	\cp -rf $pd/mysql/$mysql_version/lib/private/libcrypto.so.1.1 /lib64/
	ln -sf $pd/mysql/$mysql_version $pd/mysql/mysql
	ln -s $pd/mysql/mysql /usr/local
	\cp -rf /usr/local/mysql/bin/* /bin/
        mkdir $pd/mysql/mysql/data
        mkdir $mysql_data/log/binlog/
        chown mysql:mysql $pd/mysql/mysql/data
	if id -u mysql >/dev/null 2>&1; then
        echo "user exists"
	else
        groupadd mysql
		useradd -r -g mysql mysql
	fi
	chown -R mysql:mysql $pd/mysql  $mysql_data
	chmod -R 755 $pd/mysql
}

function InstallFullDb
{
	$pd/mysql/$mysql_version/bin/mysqld --user=mysql --basedir=$pd/mysql/mysql --datadir=$mysql_data/data/mysqldata --lower-case-table-names=1 --initialize-insecure --innodb_data_file_path=ibdata1:1G:autoextend
	\cp -rf $pd/mysql/mysql/support-files/mysql.server /etc/init.d/mysql_$DBport
	chmod 755 /etc/init.d/mysql_$DBport
	\cp -rf $pd/mysql/my.cnf $mysql_data/etc/
	\cp -f $pd/mysql/error.log $mysql_data/log/
	chmod 755 $mysql_data/log/error.log
	sed -i "s#mysql_dir#$mysql_data#g" $mysql_data/etc/my.cnf
	sed -i "s#mysqlbase_dir#$pd#g" $mysql_data/etc/my.cnf
	sed -i "s#DBport#$DBport#g" $mysql_data/etc/my.cnf
	ln -s $mysql_data/etc/my.cnf /etc/my.cnf
	if [ $1 == 'slave' ];then
		sed -i "s#SERVER_ID#30002#g" $mysql_data/etc/my.cnf
	else
		sed -i "s#SERVER_ID#30001#g" $mysql_data/etc/my.cnf
    fi
	chown -R mysql:mysql $pd
	cat > /usr/lib/systemd/system/mysqld.service <<EOF
	[Unit]
	Description=MySQL Community Server
	After=network.target
	After=syslog.target

	[Service]
	User=mysql
	Group=mysql
	ExecStart=$pd/mysql/$mysql_version/bin/mysqld_safe --defaults-file=$mysql_data/etc/my.cnf --basedir=$pd/mysql/mysql
	PrivateTmp=false
	LimitNOFILE=65536
    LimitNPROC=65536

	[Install]
	WantedBy=multi-user.target
	Alias=mysql.service
EOF
	systemctl daemon-reload
	systemctl restart mysqld
	systemctl enable mysqld
	
}
function InitDb
{
	while(true)
	do
		esps=`netstat -ntpl|grep $DBport|wc -l`
		if [ $esps -ge 1 ];then
			break;
		fi
		sleep 5
	done
	\cp $pd/mysql/slave_rpl.sh $pd
	sed -i "s#mysql_version#$mysql_version#g" $pd/slave_rpl.sh
	sed -i "s#mysql_user#$username#g" $pd/slave_rpl.sh
	sed -i "s#mysql_password#$mysql_password#g" $pd/slave_rpl.sh
	sh $pd/slave_rpl.sh $1 $2 $3 $4 $5
}
InstallPrepare $role
InstallFullDb $role
InitDb $role $masterIP $slaveIP $DBport $VIP
