#/bin/bash
#Title:mysql_install.sh
#Description:Auto install mysql
#System:Use Linux
#Author:zy
#Version:5.1
#==========================================
. jjconfig
#:Use Mysqldir
#:Use Mysqlconf
#:Use Variable
#:Use Source
#==========================================
#Set PWD
PWDDIR=`pwd`
Check_error()
{
if [ $? -eq 0 ];then
echo "SUCCESS"
else
echo "FAIL" >>$PWDDIR/mysql.log
fi
}
Check_exist()
{
if [ -d $Mysqldir -a -f $Mysqlconf -a -f $Variable -a $Source ];then
echo "OK"
tar xvf $Source &>/dev/null
Check_error
else
echo "准备文件不足,请用户先创建好"
exit
fi
}
Install()
{
yum -y install gcc gcc-c++ ncurses-devel openssl openssl-devel libtool* &>/dev/null
Check_error
cd mysql-5.1.50
./configure '--prefix=/usr/local/mysql-5.1' '--localstatedir=/data' '--with-unix-socket-path=/usr/local/mysql-5.1/mysql.sock' '--with-charsets=utf8' '--with-extra-charsets=complex' '--with-pthread' '--enable-thread-safe-client' '--with-ssl' '--with-client-ldflags=-all-static' '--with-mysqld-ldflags=-all-static' '--with-plugins=partition,federated,ndbcluster,innobase,csv,blackhole,myisam,innodb_plugin,heap,archive' '--enable-shared' '--enable-assembler'
Check_error
make
Check_error
make install
Check_error
}
Initial()
{
groupadd mysql
useradd -g mysql -s /sbin/nologin -M mysql
if [ -f /data ];then
rm -rf /data/*
else
mkdir /data
fi
if [ -f /usr/local/mysql-5.1 ];then
rm -rf /usr/local/mysql-5.1/*
else
mkdir /usr/local/mysql-5.1
fi
chown -R mysql:mysql /usr/local/mysql-5.1
chown -R mysql:mysql /data
/usr/local/mysql-5.1/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql-5.1 --datadir=/data
Check_error
cp /usr/local/mysql-5.1/share/mysql/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
sed -i "/^basedir=$/s/.*/basedir=${Base_dir}/g" /etc/init.d/mysqld
sed -i "/^datadir=$/s/.*/datadir=${Data_dir}/g" /etc/init.d/mysqld
cp ./my.cnf /etc
Check_error
#设置环境变量
sed -i "/^MYSQL=/s/.*/MYSQL=${Mysql}/g" /etc/profile
echo 'PATH=$PATH:$MYSQL' >>/etc/profile
echo "export PATH" >>/etc/profile
source /etc/profile
}
Restart()
{
/etc/init.d/mysqld restart
Check_error
}
main()
{
echo "************************************************"
echo " 1.解压 2.编译安装 3.初始化 4.启动服务 5.退出 "
echo "************************************************"
read -p "请选择您的操作(1 2 3 4 5)" i
case $i in
1)
Check_exist;;
2)
Install;;
3)
Initial;;
4)
Restart;;
5)
exit;;
esac
main
}
main;
工作目录是dconfig,里面有三个文件:jjconfig,my.cnf和mysql-5.1.50.tar.gz
jjconfig的内容是:Mysqldir="\/scripts\/dconfig"
Mysqlconf="\/scripts\/dconfig\/my.cnf"
Variable="\/scripts\/dconfig\/jjconfig"
Source="\/scripts\/dconfig\/mysql-5.1.50.tar.gz "
Base_dir="\/usr\/local\/mysql-5.1"
Data_dir="\/data"
Mysql="\/usr\/local\/services\/mysql\/bin"
~
my.cnf的内容:
[ mysqld ]
datadir = /data
basedir = /usr/local/mysql-5.1
socket = /usr/local/mysql-5.1/mysql.sock
~