#!/bin/bash
##########################################################################
# Function: #
# automatic configure mysql from source code since mysql 5.6.20 #
# Author: #
# Create: #
# 2014/08/20 #
# Update: #
# 2014/12/24 #
# Notes: Please edit my.cnf file and db_version,camke_version args #
# before running the script and put mysql source file and camke file #
# into the current directory #
##########################################################################
base_dir=/usr/local/mysql
data_root=/opt
data_dir=/opt/mysql/data
logs_dir=/opt/mysql/logs
db_user=root
db_pwd=123456
db_version=mysql-5.6.20
cmake_version=cmake-3.0.2
function JudgeMysqld(){
#If Judging mysqld and mysql directory exists
pid=`ps -ef | grep mysql |grep -v grep |awk '{print $2}'`
if [ "$pid" != "" ];then
kill -9 $pid
echo "mysql has been killed"
fi
# uninstall mysql package
for i in MySQL MySQL-server MySQL-test MySQL-bench MySQL-connector-java MySQL-connector-odbc MySQL-devel MySQL-shared MySQL-client MySQL-shared-compat
do
rpm -q $i &> /dev/null
if [ $? == 0 ]; then
rpm -e $i --nodeps &> /dev/null
fi
done
}
function AddUserGroup(){
grep mysql /etc/group &> /dev/null
if [ $? != 0 ];then
groupadd mysql
fi
grep mysql /etc/passwd &> /dev/null
if [ $? != 0 ];then
useradd -g mysql mysql
fi
echo "check relative user and group done! "
}
function JudgeMysqlDir(){
check=`find / -name mysql | wc -l`
if [ "$check" != 0 ];then
find / -name mysql -print0| xargs -0 rm -fr
fi
# define and create relative directory
if [ ! -d "$base_dir" ];then
mkdir -p "$base_dir"
else
rm -rf "$base_dir"
fi
if [ ! -d "$data_dir" ];then
mkdir -p "$data_dir"
else
rm -rf "$data_dir"
fi
if [ ! -d "$logs_dir" ];then
mkdir -p "$logs_dir"
else
rm -rf "$logs_dir"
fi
cd $data_root
chown -R mysql:mysql *
cd -
echo "check relative directory done!"
}
function DependencyPackage(){
#check and install relative packages"
if [ -d "$cmake_version" ];then
rm -rf "$cmake_version"
fi
cmake --version &> /dev/null
if [ $? == 0 ]; then
echo "cmake already exists"
else
tar -zxvf "$cmake_version".tar.gz
cd "$cmake_version"
./configure
make && make install
fi
yum -y install gcc flex bison autoconf automake ncurses-devel curl-devel make lynx zlib libxml openssl
echo "check relative install packages done! "
}
function SourceInsatll(){
# unzip $db_version"
if [ -d "$db_version" ];then
rm -rf "$db_version"
fi
tar -zxvf "$db_version".tar.gz
cd "$db_version"
if [ -d "project" ];then
rm -rf project;mkdir project
else
mkdir project
fi
cd project
CFLAGS="-O3 -g -fno-exceptions -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
CXX=g++
CXXFLAGS="-O3 -g -fno-exceptions -fno-rtti -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
export CFLAGS CXX CXXFLAGS
sleep 2
cmake ..
-DCMAKE_INSTALL_PREFIX:PATH=$base_dir \
-DMYSQL_DATADIR=$data_dir \
-DSYSCONFDIR:PATH=/etc \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DENABLE_PROFILING:BOOL=ON \
-DWITH_DEBUG:BOOL=OFF \
-DWITH_VALGRIND:BOOL=OFF \
-DENABLE_DEBUG_SYNC:BOOL=OFF \
-DWITH_EXTRA_CHARSETS:STRING=all \
-DWITH_SSL:STRING=bundled \
-DWITH_UNIT_TESTS:BOOL=OFF \
-DWITH_ZLIB:STRING=bundled \
-DWITH_PARTITION_STORAGE_ENGINE:BOOL=ON \
-DWITH_INNOBASE_STORAGE_ENGINE:BOOL=ON \
-DWITH_ARCHIVE_STORAGE_ENGINE:BOOL=ON \
-DWITH_BLACKHOLE_STORAGE_ENGINE:BOOL=ON \
-DWITH_PERFSCHEMA_STORAGE_ENGINE:BOOL=ON \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE:BOOL=ON \
-DWITH_EMBEDDED_SERVER=0 \
-DINSTALL_LAYOUT:STRING=STANDALONE \
-DCOMMUNITY_BUILD:BOOL=ON;
echo
echo "cmake compilation done! "
# make && make install"
make -j `cat /proc/cpuinfo | grep processor| wc -l`
make install
echo "mysql installation is over"
# Now,begin to change and set environment variables!!"
echo
cd $base_dir
cd ..
chown -R mysql:mysql mysql
cd -
grep "$base_dir/bin" ~/.bashrc
if [ $? == 1 ];then
echo "export PATH=$PATH:$base_dir/bin" >> ~/.bashrc
else
echo "environment variables has setted"
fi
sleep 1
source ~/.bashrc
echo
echo "Now,to be going to iniatialize data directory"
# add data directory and remove skip-federated
if [ ! -f "/etc/my.cnf" ];then
cp -f $base_dir/support-files/my-default.cnf /etc/my.cnf
sed -i "/\[mysqld\]/a basedir = $base_dir \ndatadir = $data_dir \nlog-error = $logs_dir/alert.log" /etc/my.cnf
sed -i "s/^skip-federated/#skip-federated/g" /etc/my.cnf
fi
if [ $? != 0 ];then
echo "change my.cnf error."
exit 1
fi
# iniatialize data directory
$base_dir/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --datadir=$data_dir --basedir=$base_dir
# add mysqld service
alias cp='cp'
\cp -f $base_dir/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
echo "change and set environment variables done! "
}
function TestInsatll(){
echo "Now,begin to start mysql server and test it!!"
echo
/etc/init.d/mysqld start
echo
mysqladmin -u$db_user password $db_pwd
mysqladmin -u$db_user -p$db_pwd create rgf
if [ $? == 0 ]; then
echo "mysql server is OK !"
else
echo "mysql server is wrong!Please check errors again!"
exit 2
fi
echo
mysqladmin -u$db_user -p$db_pwd -f drop rgf
echo "set privileges for user"
mysql -u$db_user -p$db_pwd -e "use mysql;delete from user where user!='root' or host!='localhost';GRANT ALL ON *.* TO 'rgf'@'%' identified by '123456' with grant option;flush privileges;"
echo "test and set privileges is over and restart mysql! "
/etc/init.d/mysqld restart
ps -ef |grep -v grep |grep mysqld
if [ $? == 1 ];then
echo "please check mysql!"
else
echo "congratulation!!mysql is running!!"
fi
cd ../../
}
#function main
JudgeMysqld
if [ $? != 0 ];then
exit 3
fi
AddUserGroup
if [ $? != 0 ];then
exit 4
fi
JudgeMysqlDir
if [ $? != 0 ];then
exit 5
fi
DependencyPackage
if [ $? != 0 ];then
exit 6
fi
SourceInsatll
if [ $? != 0 ];then
exit 7
fi
TestInsatll
if [ $? != 0 ];then
exit 8
fi
echo "install mysql OK!!"