使用环境:①CentOS6 & Cent OS7
②联网环境下下载mysql软件包
脚本:#!/bin/bash
#Date 2018/9/10
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
#id –u 检测当前用户id 0为root用户
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
which mysqld
if [ $? -eq 0 ];then
#$? 上条命令退出状态,附1
yum remove `yum list installed | grep mysql` -y
exit 1
fi
if [ ! -d /opt ];then
#[ –d file ]检查是否为目录,附2
mkdir /opt && cd /opt
else
cd /opt
fi
echo "# Atuo 编译安装mysql5.5/5.6 #"
echo "1 Install mysql-5.5"
echo "2 Install mysql-5.6"
echo "3 EXIT"
read -p "Please input your choice:" NUM
case $NUM in
1)
echo -e "\033[32m Start Install mysql-5.5 Now \033[0m"
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.24.tar.gz
TDIR=mysql-5.5.24
;;
2)
echo -e "\033[32m Start Install mysql-5.6 Now \033[0m"
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.22.tar.gz
TDIR=mysql-5.6.22
;;
3)
echo "exit install!!!"
exit 1
;;
*)
echo "Input Error! Pease input{1|2|3|4}"
exit 0
;;
esac
if [ $? -eq 0 ];then
tar zxf $TDIR.tar.gz
else
echo -e "\033[32m Download error! Please check your network !!!\033[0m"
exit 1
fi
mysqlu=`awk -F: '$0~/mysql/' /etc/passwd |wc -l`
#’$0~/mysql/’ $0表示匹配所有域,以:分割,第一个域就是$1; /部分是分割; mysql部分是模式;附3
mysqlg=`awk -F: '$0~/mysql/' /etc/group |wc -l`
if [ $mysqlu -ne 0 ]&&[ $mysqlg -ne 0 ];then
echo -e "\033[32m mysql is exists! \033[0m"
else
/usr/sbin/useradd -s /sbin/nologin mysql
fi
[ ! -d /usr/local/mysql ] && mkdir -p /usr/local/mysql
yum install -y gcc gcc-c++ make cmake ncurses-devel bison libaio-devel
cd /opt/$TDIR
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/home/mysql \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306
make && make install
if [ $? -eq 0 ];then
chown -R mysql.mysql /usr/local/mysql
echo "export PATH=$PATH:/usr/local/mysql/bin/ " >> /etc/profile
source /etc/profile
cp support-files/my-*.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add /etc/init.d/mysqld
chkconfig mysqld --level 35 on
fi
/usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
sed -i 's#^basedir=.*$#basedir=/usr/local/mysql#' /etc/init.d/mysqld
# –i表示插入;s表示替换;#号表示分割符;附4
sed -i 's#^datadir=.*$#datadir=/home/mysql#' /etc/init.d/mysqld
/etc/init.d/mysqld start
Start=`echo $?`
Netstat=`netstat -ntap | grep mysql |wc -l`
if [ $Start -eq 0 ]&&[ $Netstat -eq 1 ];then
echo -e "\033[32m $TDIR is install success! \033[0m"
else
echo -e "\033[32m $TDIR is install fail! \033[0m"
fi
附一:退出状态值及其含义
状态值 含义
0 程序运行成功,未遇到问题
1—125 运行失败,脚本命令、系统命令或参数传递错误
126 找到该命令但无法执行
127 未找到要运行的命令
>128 命令被系统强行结束
附二:文件操作符文件运算符文件描述
-d file是否为目录
-e file是否存在
-f file是否为普通文件
-r file是否可读
-w file是否可写
-x file是否可执行
-s file长度是否为0
-L file是否符号化链接
附三、awk
awk调用用法
①shell命令:awk [ –F 域分隔符] ’awk程序段‘ 输入文件
②awk程序插入脚本文件 awk –f awk脚本文件 输入文件
③执行脚本 ./awk脚本文件 输入文件
附四、sed
sed调用用法
①shell命令:sed [ 选项 ] ’sed命令‘ 输入文件
②sed程序插入脚本文件 sed [ 选项 ] –f sed脚本文件 输入文件
③执行脚本 ./sed脚本文件 输入文件