#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root to install MySQL"
exit 1
fi
clear
echo "========================================================================="
echo "A tool to auto-compile & install MySQL on Linux "
echo "========================================================================="
cur_dir=$(pwd)
#set mysql root password
echo "==========================="
mysqlrootpwd="root"
echo "Please input the root password of mysql:"
read -p "(Default password: root):" mysqlrootpwd
if [ "$mysqlrootpwd" = "" ]; then
mysqlrootpwd="root"
fi
echo "==========================="
echo "MySQL root password:$mysqlrootpwd"
echo "==========================="
#do you want to install the InnoDB Storage Engine?
echo "==========================="
installinnodb="n"
echo "Do you want to install the InnoDB Storage Engine?"
read -p "(Default no,if you want please input: y ,if not please press the enter button):" installinnodb
case "$installinnodb" in
y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
echo "You will install the InnoDB Storage Engine"
installinnodb="y"
;;
n|N|No|NO|no|nO)
echo "You will NOT install the InnoDB Storage Engine!"
installinnodb="n"
;;
*)
echo "INPUT error,The InnoDB Storage Engine will NOT install!"
installinnodb="n"
esac
#which MySQL Version do you want to install?
echo "==========================="
isinstallmysql55="y"
get_char()
{
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo ""
echo "Press any key to start...or Press Ctrl+c to cancel"
char=`get_char`
function InitInstall()
{
cat /etc/issue
uname -a
MemTotal=`free -m | grep Mem | awk '{print $2}'`
echo -e "\n Memory is: ${MemTotal} MB "
#Set timezone
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install -y ntp
ntpdate -u pool.ntp.org
date
rpm -qa|grep mysql
rpm -e mysql
yum -y remove mysql-server mysql mysql-libs
yum -y remove php-mysql
yum -y install yum-fastestmirror
#Disable SeLinux
if [ -s /etc/selinux/config ]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi
cp /etc/yum.conf /etc/yum.conf.lnmp
sed -i 's:exclude=.*:exclude=:g' /etc/yum.conf
for packages in patch make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap diffutils;
do yum -y install $packages; done
mv -f /etc/yum.conf.lnmp /etc/yum.conf
}
function CheckAndDownloadFiles()
{
echo "============================check files=================================="
if [ -s mysql-5.5.37.tar.gz ]; then
echo "mysql-5.5.37.tar.gz [found]"
else
echo "Error: mysql-5.5.37.tar.gz not found!!!download now......"
wget -c http://soft.vpser.net/datebase/mysql/mysql-5.5.37.tar.gz
fi
if [ -s mysql-openssl.patch ]; then
echo "mysql-openssl.patch [found]"
else
echo "Error: mysql-openssl.patch not found!!!download now......"
wget -c http://soft.vpser.net/lnmp/ext/mysql-openssl.patch
fi
echo "============================check files=================================="
}
function InstallMySQL55()
{
echo "============================Install MySQL 5.5.26=================================="
cd $cur_dir
rm -f /etc/my.cnf
tar zxf mysql-5.5.37.tar.gz
cd mysql-5.5.37/
patch -p1 < $cur_dir/mysql-openssl.patch
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1
make && make install
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
cp support-files/my-medium.cnf /etc/my.cnf
sed '/skip-external-locking/i\datadir = /usr/local/mysql/var' -i /etc/my.cnf
if [ $installinnodb = "y" ]; then
sed -i 's:#innodb:innodb:g' /etc/my.cnf
sed -i 's:/usr/local/mysql/data:/usr/local/mysql/var:g' /etc/my.cnf
else
sed '/skip-external-locking/i\default-storage-engine=MyISAM\nloose-skip-innodb' -i /etc/my.cnf
fi
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib
/usr/local/lib
EOF
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
if [ -d "/proc/vz" ];then
ulimit -s unlimited
fi
/etc/init.d/mysql start
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
/usr/local/mysql/bin/mysqladmin -u root password $mysqlrootpwd
cat > /tmp/mysql_sec_script<<EOF
use mysql;
update user set password=password('$mysqlrootpwd') where user='root';
delete from user where not (user='root') ;
delete from user where user='root' and password='';
drop database test;
DROP USER ''@'%';
flush privileges;
EOF
/usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
rm -f /tmp/mysql_sec_script
/etc/init.d/mysql restart
/etc/init.d/mysql stop
echo "============================MySQL 5.5.26 install completed========================="
}
function AddAndStartup()
{
/etc/init.d/mysql start
#add iptables firewall rules
if [ -s /sbin/iptables ]; then
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 3306 -j DROP
/sbin/iptables-save
fi
}
function CheckInstall()
{
echo "===================================== Check install ==================================="
clear
ismysql=""
echo "Checking..."
if [ -s /usr/local/mysql/bin/mysql ] && [ -s /usr/local/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then
echo "MySQL: OK"
ismysql="ok"
else
echo "Error: /usr/local/mysql not found!!!MySQL install failed."
fi
if [ "$ismysql" = "ok" ] ; then
echo "Install MySQL completed! enjoy it."
echo "default mysql root password:$mysqlrootpwd"
echo "The path of some dirs:"
echo "mysql dir: /usr/local/mysql"
echo ""
echo "========================================================================="
netstat -ntl
else
echo "Sorry,Failed to install mysql!"
fi
}
InitInstall 2>&1 | tee /root/lnmp-install.log
CheckAndDownloadFiles 2>&1 | tee -a /root/lnmp-install.log
InstallMySQL55 2>&1 | tee -a /root/lnmp-install.log
AddAndStartup 2>&1 | tee -a /root/lnmp-install.log
CheckInstall 2>&1 | tee -a /root/lnmp-install.log