!/bin/bash
BOOSTURL="https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz"
PERCONAURL="https://www.percona.com/downloads/Percona-Server-LATEST/Percona-Server-5.7.18-15/source/debian/percona-server-5.7_5.7.18-15.orig.tar.gz"
install_filepath=$(dirname $0)
基础配置
port=3308
basedir=/data/app/mysql5.7
rundir=$basedir/run
databasedir=/data/dbdata/mysql${port}
datadir=${databasedir}/data
log/tmp/redo/ibdata 文件目录{日志文件、临时文件、共享表空间文件可独立其他目录存储如存储到ssd区域}
otherdir=/opt/dbdata/mysql${port}
logdir=${otherdir}/log
tmpdir=${otherdir}/tmp
redodir=${otherdir}/redo
ibdatadir=${otherdir}
基础配置选项文件my.cnf
mycnf=$install_filepath/my.cnf
主机基础信息并产生server_id参数【IP尾号+端口】
LOCALGW=$(route -n|awk '{if( $1 == "0.0.0.0"){print $2}}')
LOCALIP=$( ifconfig -a|awk '{print $2}'|grep $(echo $LOCALGW |awk -F"." '{print $1"."$2"."$3}') )
SERVERID=$(echo $LOCALIP|awk -F"." '{print $4}')$port
对临时目录判断[安装时也作为临时目录使用]
if [ -d $tmpdir ] ;then
install_filepath=$tmpdir
else
install_filepath=$(dirname $0)
fi
mysql用户与组判断,不存在则创建
gmysql=$(grep mysql /etc/group)
if [ -z $gmysql ] ;then
groupadd mysql
fi
umysql=$(grep mysql /etc/passwd)
if [ -z $umysql ] ;then
useradd -g mysql -s /bin/bash mysql
fi
function object_is_null ()
{
if [ -z "$1" ] ; then
echo "$1 is null"
return 1
else
echo "$1 is exist"
return 0
fi
}
function permission_exist()
{
if [ "mysql.mysql" = $(ll $1 |awk '{print $3"."$4}') ] ;then
echo "permission successed"
else
echo "Permission denied: May be you can chown -R mysql.mysql $1 "
fi
}
function dir_is_exist ()
{
if [ -d $1 ] ; then
echo "$1 is exist"
if [ $(ls -A $1 |wc -l) -gt 0 ] || [ -z "$(ls -A $1 |wc -l)" ];then
echo -e "$1 is empty , check Success "
else
echo -e " \033[41;33m $1 is not empty ,check Failed \033[0m "
exit 1
fi
return 1
else
echo "$1 is not exist"
echo "creating $1"
echo -e " \033[41;33m mkdir -p $1 \033[0m "
mkdir -p $1
return 0
fi
}
####目录验证
dir_is_exist $basedir
dir_is_exist $rundir
dir_is_exist $databasedir
dir_is_exist $logdir
dir_is_exist $datadir
dir_is_exist $tmpdir
dir_is_exist $redodir
dir_is_exist $ibdatadir
# cp $mycnf $basedir
权限验证
chown -R mysql.mysql $basedir
chown -R mysql.mysql $rundir
chown -R mysql.mysql $databasedir
chown -R mysql.mysql $logdir
chown -R mysql.mysql $datadir
chown -R mysql.mysql $tmpdir
chown -R mysql.mysql $redodir
chown -R mysql.mysql $ibdatadir
yum -y install perl-DBD-MySQL.x86_64 perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN perl-Time-HiRes
yum install cmake make gcc gcc-c++ bison ncurses ncurses-devel zlib zlib-devel -y
yum install perl readline readline-devel -y
安装包检查,检查是否有安装包,不存在,则下载
if [ -z $( ls $install_filepath |grep boost ) ] ;then
wget -c -P $install_filepath $BOOSTURL
if [ $? -eq 0 ];then
echo -e "get Success :$BOOSTURL "
else
echo -e " \033[41;33m get Failed :$BOOSTURL \033[0m "
exit 1
fi
fi
if [ -z $( ls $install_filepath |grep percona-server ) ] ;then
wget -c -P $install_filepath $PERCONAURL
if [ $? -eq 0 ];then
echo -e "get Success :$PERCONAURL "
else
echo -e " \033[41;33m get Failed :$PERCONAURL \033[0m "
exit 1
fi
fi
进入下载后目录
cd $install_filepath
查看本地是否存在boost或percona-server压缩包,不存在则解压刚下载的包
for codefile in $( ls *.tar.gz |grep -e boost -e percona-server)
do
if [ -f $codefile ] ;then
codedir=$(echo $codefile|awk -F "." '{print $1}')
codedirs=$( ls |grep $codedir|grep -v tar.gz )
echo $codedir
echo $codedirs
if [ $codedirs ] ;then
echo -e " \033[41;33m $codedirs is exist \033[0m "
exit 1
else
tar zxvf $codefile
fi
else
echo $codefile is not exist
fi
done
截取对应包名字(去除.tar.gz)
boost=$( ls |grep boost|grep -v tar.gz )
percona=$( ls |grep percona-server|grep -v tar.gz )
首先生成my.cnf文件
cat /dev/null > $basedir/my.cnf
基础配置信息写入 my.cnf
echo -e '
[mysql]
prompt="\u@\h:\d \\r:\m:\s> "
'>>$basedir/my.cnf
目录相关配置地址
echo -e "
no_auto_rehash
[mysqld]
basedir=$basedir
datadir=$datadir
tmpdir=$tmpdir
pid-file=$rundir/mysqld.pid
socket=$rundir/mysql.sock
port=$port
log_error=$logdir/alert.log
slow_query_log_file=$logdir/slow.log
log_bin=$logdir/mysql_bin
innodb_data_home_dir=$ibdatadir
innodb_log_group_home_dir=$redodir
relay_log=$logdir/mysql_relay
relay_log_index=$logdir/mysql_relay.index
slave_load_tmpdir=$tmpdir
server_id=$SERVERID
" >>$basedir/my.cnf
其他需要配置项可写入当前目录下的default-my.cnf并写入my.cnf文件
if [ -f "$install_filepath/default-my.cnf" ];then
cat $install_filepath/default-my.cnf >>$basedir/my.cnf
echo -e " \033[41;33m copy $install_filepath/default-my.cnf into $basedir/my.cnf \033[0m "
else
echo -e " \033[41;33m $basedir/my.cnf . No other configuration options . \033[0m "
fi
echo -e '
[client]
socket=$rundir/mysql.sock
'>>$basedir/my.cnf
####开始编译
cd $percona
cmake . -DWITH_BOOST=../$boost \
-DCMAKE_INSTALL_PREFIX=$basedir \
-DMYSQL_DATADIR=$datadir \
-DSYSCONFDIR=$basedir \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1
验证编译是够正常
if [ $? -ne 0 ] ;then
echo "cmake error"
else
编译正常,则进入make
make -j16
if [ $? -ne 0 ] ; then
echo "make error"
exit 1
fi
make install
if [ $? -ne 0 ] ; then
echo "make install error"
exit 1
else
###编译安装均正常,则copy 相关文件到系统环境变量目录。并初始化,加入开机启动项
cd $basedir
ln -s $basedir/bin/* /usr/bin/
mysqld --defaults-file=$basedir/my.cnf --initialize --user=mysql
ln -s $basedir/support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
service mysql start
fi
fi
因编译时my.cnf 选择$basedir/my.cnf,因此取消或替换/etc/my.cnf配置文件
if [ -f /etc/my.cnf ];then
mv /etc/my.cnf /etc/my.cnf.bak
fi