编译安装mariadb-10.2.23的shell脚本

 
#!/bin/bash
 
#定义变量
let cpu_num=lscpu|awk -F ' +' '/^CPU\(s\):/{print $2}'-1
install_dir=/app/mysql
data_dir=/data/mysql
user_name=mysql
passwd=123456

 
#安装编译相关的包
yum -y install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel &>/dev/null
 
#创建系统用户mysql
id mysql &>/dev/null
if [ echo $? -ne 0 ];then
userdel -r mysql &>/dev/null
useradd -r -u 336 -s /sbin/nologin -d /data/mysql mysql &>/dev/null
else
useradd -r -u 336 -s /sbin/nologin -d /data/mysql mysql &>/dev/null
fi
 
#安装expect,用于安全加固
rpm -q expect &>/dev/null
[ echo $? -ne 0 ] && yum -y install expect
 
#源码包存在的情况下才进行编译安装
if [ -e mariadb-10.2.23.tar.gz ];then
#此文件可能会造成影响,所以先清空

/etc/my.cnf &>/dev/null
mkdir -p $install_dir
tar xf mariadb-10.2.23.tar.gz
cd mariadb-10.2.23
sleep 1
cmake . \
-DCMAKE_INSTALL_PREFIX=${install_dir} \
-DMYSQL_DATADIR=${data_dir} \
-DSYSCONFDIR=/etc/mysql \
-DMYSQL_USER=${user_name} \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make -j $cpu_num && make install
#配置环境变量
echo 'PATH=/app/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
#创建数据库目录并安装数据库
mkdir $data_dir -p
chown mysql.mysql $data_dir
cd $install_dir
./scripts/mysql_install_db --datadir=$data_dir --user=$user_name
#创建主配置文件
mkdir -p /etc/mysql
cp ${install_dir}/support-files/my-huge.cnf /etc/mysql/my.cnf
#禁止主机名解析,建议使用
sed -ri '/^[mysqld]/askip_name_resolve = on' /etc/mysql/my.cnf
sed -ri '/^[mysqld]/adatadir='$data_dir'' /etc/mysql/my.cnf
#创建服务脚本
cp ${install_dir}/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
#安全加固
expect <<EOF
spawn mysql_secure_installation
expect {
"Enter current password for root" { send "\n";exp_continue }
"Set root password" { send "\n";exp_continue }
"New password" { send "${passwd}\n";exp_continue }
"Re-enter new password" { send "${passwd}\n";exp_continue }
"Remove anonymous users" { send "\n";exp_continue }
"Disallow root login remotely" { send "\n";exp_continue }
"Remove test database and access to it" { send "\n";exp_continue }
"Reload privilege tables now" { send "\n" }
}
#expect "]#" { send "exit\n" }
expect eof
EOF
#测试mysql是否正常运行
mysqladmin -uroot -p123456 ping &>/dev/null
[ echo $? -eq 0 ] && echo 'mysql is running !' || echo 'mysql is stopped'

else
echo -e "mariadb-10.2.23.tar.gz is not exist"
exit
fi

 

注意:

1、源码包要与脚本在同一目录中。

2、服务器或虚拟机要能连接外网。

3、对于需要添加的功能,可以在cmake下面添加,此脚本仅做测试用。