配合文档看,挺简单的。
#!/bin/bash
###2016-3-23
###version 1.0
####tishi yonghu
cat<< AA
you must mv the mysql_package to /root/tools/ .
AA
sleep 2
###define var
tools_dir="/root/tools"
package_name="`cd $tools_dir && ls mysql-*-linux*`"
version_num="`cd $tools_dir && ls mysql*linux* -d | awk -F"-" ‘{print $2}‘`"
install_path="/application/mysql$version_num"
####test if have a mysql user
id mysql &>/dev/null
[ $? -ne 0 ] && useradd mysql -s /sbin/login -M && echo "mysql_user ok." || echo "mysql_user ok."
###test if have a install dir.
[ ! -d $install_path ] && mkdir -p $install_path && echo "install_dir ok." || ( echo "$install_path is exsit.";exit 1 )
###tar and mv mysql_package
cd $tools_dir && tar xf $package_name
dir_name="`ls mysql*x86_64 -d`"
mv ${dir_name}/* $install_path && sleep 7 && rm -rf ${dir_name} && echo "tar and mv ok." || exit 1
###ln dir and dir
ln -s ${install_path}/ /application/mysql && echo "ln dir ok." || exit 1
###chown dir
chown -R mysql.mysql /application/mysql/ && echo "chwon dir ok." || exit 1
###init mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql &>/dev/null
[ $? -eq 0 ] && echo "init ok." || exit 1
###cp my-cnf
[ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak
cp /application/mysql/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
[ $? -eq 0 ] && echo "cp my.cnf ok." || exit 1
###mysql
cp /application/mysql/bin/mysqld_safe /application/mysql/bin/mysqld_safe.bak
[ $? -ne 0 ] && exit 1
sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/bin/mysqld_safe
[ $? -ne 0 ] && exit 1
grep ‘PATH="/application/mysql/bin:$PATH"‘ /etc/profile &>/dev/null
[ $? -ne 0 ] && echo ‘PATH="/application/mysql/bin:$PATH"‘ >> /etc/profile && source /etc/profile || echo "path ok."
[ $? -ne 0 ] && exit 1
#/application/mysql/bin/mysqld_safe & >/dev/null 2>&1
#[ $? -ne 0 ] && exit 1
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[ $? -ne 0 ] && exit 1
sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /etc/init.d/mysqld
[ $? -ne 0 ] && exit 1
chmod +x /etc/init.d/mysqld
[ $? -ne 0 ] && exit 1
chkconfig --add mysqld
[ $? -ne 0 ] && exit 1
grep ‘/etc/init.d/mysqld start‘ /etc/rc.local &>/dev/null
[ $? -ne 0 ] && echo ‘/etc/init.d/mysqld start‘ >> /etc/rc.local
[ $? -ne 0 ] && exit 1
#/application/mysql/bin/mysqladmin -uroot password "123456"
#[ $? -ne 0 ] && exit 1
#/application/mysql/binmysql -uroot -p123456
#[ $? -ne 0 ] && exit 1
############done
echo "now you can use mysql,thanks for your wait."