安装包:mysql-5.6.31.tar.gz
已有配置文件:my.cnf
*注意:本次Mysql的配置文件是在my.cnf的基础上更改得到的,my.cnf存放路径为/opt/rh/my.cnf
脚本中的num变量用来生成 mysql$num 用户,生成的配置文件命名也是以my$num.cnf命名的
每个实例必须有不同的端口号,不然会冲突,在my.cnf中初始端口号设置为3306,为了避免端口冲突,我在脚本中将端口号改为3306+$num
不同实例的目录命名也进行了微调,具体见脚本
为每个实例生成了一个日志目录,为log$num
my.cnf内容解析:
http://www.cnblogs.com/xiami-xm/p/5735805.html
#!/bin/bash
num=3
nowwhere=/usr/local/mysql$num/etc
#==============================
#判断每个重要步骤是否执行成功
#test()
#==============================
test()
{
if [ $? -eq 0 ]
then
echo "$1 $2 $3" >/opt/rh/log_ok 2>&1
else
echo "$1 $4 $5" >/opt/rh/log_error 2>&1
# exit 1
fi
}
#==============================
#判断环境
#test1()
#==============================
test1()
{
if [ $1 -eq 0 ]
then
echo "$2已经安装过了"
else
echo "$2未安装,下面安装$2"
yum -y install $2 && make && make install >/opt/rh/log$num/yum_install_$2 2>&1
test "[$2是否安装成功]" $2 [OK] $2 [Error]
fi
}
#==============================
#判断系统编译环境是否正常
#envir()
#==============================
envir()
{
gcc=`gcc --help|echo $?`
test1 $gcc gcc
gjj=`g++ --help|echo $?`
test1 $gjj gcc-c++
perl=`perl --help|echo $?`
test1 $perl perl
cmake=`cmake --help|echo $?`
test1 $cmake cmake
bison=`bison --help|echo $?`
test1 $bison bison
ncurses=`ncurses5-config --version|echo $?`
test1 $ncurses ncurses-devel
}
#=====================
#创建用户,创建目录
#adduser()
#========================
adduser()
{
groupadd mysql$num
test "[add mysql$num group]" "添加mysql$num组" [OK] "添加mysql$num组" [Error]
useradd mysql$num -g mysql$num -s /sbin/nologin
test "[add mysql$num user]" "添加mysql$num用户" [OK] "添加mysql$num用户" [Error]
mkdir -p /usr/local/mysql$num
test "mkdir /usr/local/mysql$num" "创建mysql$num安装目录" [OK] "创建mysql$num安装目录" [Error]
mkdir -p /usr/local/mysql$num/etc
test "mkdir /usr/local/mysql$num/etc" "创建mysql$num配置文件目录" [OK] "创建mysql$num配置文件目录" [Error]
mkdir -p /data$num/mysqldb$num
test "mkdir /data$num/mysqldb$num" "创建mysql$num数据目录" [OK] "创建mysql$num数据目录" [Error]
mkdir -p /data$num/mysqldb$num/binlog
test "mkdir /data$num/mysqldb$num/binlog" "创建mysql$num的binlog日志目录" [OK] "创建mysql$num的binlog日志目录" [Error]
mkdir -p /data$num/mysqldb$num/log
test "mkdir /data$num/mysqldb$num/binlog" "创建mysql$num的日常log目录" [OK] "创建mysql$num的日常log目录" [Error]
mkdir -p /opt/mysqltmp$num
test "mkdir /opt/mysqltmp$num" "创建mysql$num的临时表目录" [OK] "创建mysql$num的临时表目录" [Error]
mkdir -p /opt/rh/log$num
test "mkdir /opt/rh/log$num" "创建日志目录" [OK] "创建日志目录" [Error]
}
#=========================================
#安装mysql前奏,若未安装过,则解压压缩包
#安装过,则删除CMakeCache.txt文件
#pre_install_mysql()
#=============================================
pre_install_mysql()
{
if [ ! -d mysql-5.6.31 ]
then
tar -zxvf /opt/rh/mysql-5.6.31.tar.gz >/dev/null 2>&1
test "解压压缩包" "解压" [OK] "解压" [Error]
else
if [ -f /opt/rh/mysql-5.6.31/CMakeCache.txt ]
then
mv /opt/rh/mysql-5.6.31/CMakeCache.txt /opt/rh/mysql-5.6.31/CMakeCache.txt.bak$num
test "删除CMakeCache.txt" "删除" [OK] "删除" [Error]
fi
fi
}
#============================
#安装mysql
#install_mysql()
#============================
install_mysql()
{
cd /opt/rh/mysql-5.6.31
echo "下面编译安装mysql,可能需要几分钟,请稍等。。。"
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql$num -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DSYSCONFDIR=/usr/local/mysql$num/etc -DDEFAULT_CHARSET=gbk -DDEFAULT_COLLATION=gbk_chinese_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data$num/mysqldb$num -DMYSQL_TCP_PORT=`expr 3306 + $num` >/opt/rh/log$num/cmake_mysql$num 2>&1
test "cmake mysql" cmake [OK] cmake [Error]
make && make install >/opt/rh/log$num/make_mkinstall$num 2>&1
test "make && make install" "make && make install" [OK] "make && make install" [Error]
}
#==================================
#初始化授权
#init_auth()
#=====================================
init_auth()
{
chown mysql$num:mysql$num /usr/local/mysql$num -R
test "更改/usr/local/mysql$num属主组" "更改" [OK] "更改" [Error]
chown mysql$num:mysql$num /data$num/mysqldb$num -R
test "更改/data$num/mysqldb$num属主组" "更改" [OK] "更改" [Error]
chown mysql$num:mysql$num /data$num/mysqldb$num/binlog
test "更改/data$num/mysqldb$num/binlog属主组" "更改" [OK] "更改" [Error]
chown mysql$num:mysql$num /data$num/mysqldb$num/log
test "更改/data$num/mysqldb$num/log属主组" "更改" [OK] "更改" [Error]
chown mysql$num:mysql$num /opt/mysqltmp$num
test "更改/opt/mysqltmp$num属主组" "更改" [OK] "更改" [Error]
chown mysql$num:mysql$num /opt/rh/log$num
test "更改/opt/rh/log$num属主组" "更改" [OK] "更改" [Error]
}
#=========================
#初始化
#init()
#==============================
init()
{
/usr/local/mysql/scripts/mysql_install_db --user=mysql$num --basedir=/usr/local/mysql$num --datadir=/data$num/mysqldb$num >/opt/rh/log$num/start_init 2>&1
test "[初始化是否成功]" "初始化" [OK] "初始化" [Error]
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld$num
test "[复制服务启动脚本mysql.server]" "复制mysql.server" [OK] "复制mysql.server" [Error]
chmod 755 /etc/init.d/mysqld$num
test "[更改mysqld$num权限为755]" "改mysqld$num权限" [OK] "改mysqld$num权限" [Error]
}
#====================
#更改配置文件
#cnf()
#======================
cnf()
{
cat /opt/rh/my.cnf |sed -e 's/mysql/mysql'$num'/g' -e 's/mysql'$num'd/mysqld'$num'/g' -e 's/3306/'`expr $num + 3306`'/g' -e 's/mysqld'$num'b/mysqldb'$num'/g' -e 's/mysql'$num'tmp/mysqltmp'$num'/g' -e 's/mysql'$num'.sock/mysql.sock/g' -e 's/mysql'$num'.pid/mysql.pid/g' -e 's/\/data/\/data'$num'/g' -e 's/mysqltmp/mysqltmp'$num'/g'> $nowwhere/my$num.cnf
test "[生成配置文件my$num.cnf]" "生成文件" [OK] "生成文件" [Error]
echo -e "MYSQL=/usr/local/mysql$num/bin \nPATH=$PATH:$MYSQL\nexport PATH" >> /etc/profile
test "[修改/etc/profile]" "修改" [Ok] "修改" [Error]
#生效
source /etc/profile
/etc/init.d/mysqld$num start >/opt/rh/log$num/mysql$num.start 2>&1
test "[启动服务mysqld$num]" "启动服务" [OK] "启动服务" [Error] }
#==================
#main
#====================
main()
{
envir
adduser
pre_install_mysql
install_mysql
init_auth
init
cnf
}
main;