一键编译安装mysql数据库

说明:请把所需要的软件包放在文件夹中,把文件夹重命名为packages,然后压缩成packages.zip的包,上传到linux系统上任何目录都可以,然后在上传packages.zip包的当前目录(例如:/media )运行mysql_install.sh脚本即可,若mysql_install.sh脚本和packages.zip包不在同一目录,需使用绝对该脚本所在绝对路径来运行。
[root@localhost media]# vim /root/mysql_install.sh
#!/bin/bash
#Write_Date: 2012.07.02
date > /root/install_mysql_time.txt

#--------------------------------------------------------------------------------
cat << EOF
+--------------------------------------------------------------+
|           === Welcome to install mysql database ===          |
+--------------------------------------------------------------+
EOF
#---------------------------------------------------------------------------------

# MySQL install directory and configuration files...
echo "Do you want to install the MySQL database in what directory?"
read -p "Please input a diretory,For Example:/opt :" directory
#directory=/opt
mysql_base_dir=$directory/app/mysql
data_dir=$directory/data/mysql
packages=$directory/packages
mysql_password=p@ssw0rd
IN_PATH=`pwd`
# Check the mysqld rpm packages installed, if installed on uninstall...
rpm -qa | grep mysql
if [ $? -eq 0 ]
 then
  echo "The mysql rpm packages already installed!!"
  echo "# yum -y remove mysql"
  service mysqld stop
  sleep 3
  yum -y remove mysql
 else
  echo "The mysql rpm packages not installed!!"
fi

# Install the mysql database need development tools and libraries!
echo "Install the mysql database need development tools and libraries.Please wating..."
sleep 3
yum -y install gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libmcrypt* ibtool*

unzip $IN_PATH/packages.zip -d $directory
# Mysql database version choice...
echo "show $packages mysql install package."
ls -lh $packages | awk '{ print $9}' | grep mysql
echo "Please input you will install mysql database version number:"
read -p "Mysql database version number is:" version
echo "You will install mysql database package is mysql-$version.tar.gz!"
sleep 3

if [ -e "$packages/mysql-$version.tar.gz" ]
 then
  echo "Please wait..."
# Create a system user runing the mysql database.
  echo "Create a system user runing the mysql database."
  groupadd mysql
  useradd -M -g mysql -s /sbin/nologin mysql
# Create the installation directory and data store directory
   if [ -d "$mysql_base_dir" ]
    then
     echo "This $mysql_base_dir is already existed!"
    else
     mkdir -p $mysql_base_dir
   fi
   if [ -d "$data_dir" ]
    then
     echo "This $data_dir is already existed!"
    else
     mkdir -p $data_dir
   fi
# Modify the installation directory and data storage directory permissions.
  chown -R root:mysql $mysql_base_dir
  chown -R mysql:mysql $data_dir
  chmod -R 755 $data_dir
# Unpack, configure, compile, install mysql
  echo "Unpack, configure, compile, install mysql"
  tar -zxf $packages/mysql-$version.tar.gz -C $packages
  cd $packages/mysql-$version
  echo "Configure, compile and install, please wait... Mabey take 20 minutes!"
  sleep 3
  sh configure \
  --prefix=$mysql_base_dir \
  --localstatedir=$data_dir \
  --enable-assembler \
  --with-unix-socket-path=/tmp/mysql.sock \
  --with-plugins=partition,innobase \
  --with-charset=utf8 \
  --with-collation=utf8_general_ci \
  --with-extra-charsets=all \
  --with-big-tables \
  --without-debug \
  --with-client-ldflags=-all-static \
  --with-mysqld-ldflags=-all-static \
  --enable-thread-safe-client
  make && make install
  cd

# Mysql master configuration file.
  cp -p $packages/mysql-$version/support-files/my-medium.cnf /etc/my.cnf
# Initialize the database using the mysql user.
  $mysql_base_dir/bin/mysql_install_db --user=mysql --basedir=$mysql_base_dir --datadir=$data_dir
# Adjust the lib library path.
  echo "$mysql_base_dir/lib/mysql" >> /etc/ld.so.conf
  ldconfig
# Start mysqld_safe script safe to start the service!
  $mysql_base_dir/bin/mysqld_safe --user=mysql &
  echo "Show Mysql used port!!"
  netstat -antupl | grep mysqld
  export PATH=$PATH:$mysql_base_dir/bin
  echo "PATH=$PATH:$mysql_base_dir/bin" >> /etc/profile
  source /etc/profile
# Mysql add as a system service, start with the system and start
  cp -p $packages/mysql-$version/support-files/mysql.server /etc/init.d/mysqld
  chmod 755 /etc/init.d/mysqld
  chkconfig mysqld on
  mysqladmin -u root password ${mysql_password}
  echo "Mysql install Success!!!"
  sleep 3
  source /etc/profile
  date >> /root/install_mysql_time.txt
  cat << EOF
   +--------------------------------------------------------------+
|           === The mysql database install Success!!! ===            |
   +--------------------------------------------------------------+
EOF
  echo "Show mysql used port!!"
  netstat -antupl | grep mysqld
  # Show the /tmp directory is mysql.sock this file?
  echo "# ls -lh /tmp!! Show the /tmp directory is mysql.sock this file?"
  ls -lh /tmp
  echo "Show time to install mysql!!"
  cat /root/install_mysql_time.txt | awk '{ print $4 }'
 else
  echo "Mysql install Failed!!!"
  rm -fr $mysql_base_dir
  rm -fr $data_dir
  rm -fr $packages
  userdel  mysql
  groupdel mysql
  rm -fr /etc/init.d/mysqld
  rm -f /root/install_mysql_time.txt
  exit 1
fi
source /etc/profile

[root@localhost media]# chmod 755 /root/mysql_install.sh
[root@localhost media]# /root/mysql_install.sh