##################################################
#mysql_install.sh #
#initialize the Linux system to deploy lnmp #
#2016/10/31 16:41 #
#kangjie #
##################################################
#!/bin/sh
#定义文件的位置
LOG_PATH=/server/initlog
LOG_FILE=$LOG_PATH/mysql_install.log
#MySQL安装路径
MYSQL_BASEDIR=/data/mysqlbasedir
#数据目录
MYSQL_DATA=/data/mysql
MYSQL_POS=/home/package
i=0
判断目录是否存在,没有则创建
[ ! -d $LOG_PATH ] && mkdir -p $LOG_PATH
[ ! -d $MYSQL_BASEDIR ] && mkdir -p $MYSQL_BASEDIR
[ ! -d $MYSQL_DATA ] && mkdir -p ${MYSQL_DATA}/data
[ ! -d $MYSQL_POS ] && mkdir -p $MYSQL_POS
#mysql5.5.32
#check
#检查执行状态,将状态写入日志文件
function check(){
if [ $? != 0 ]
then
echo "××××-Install $1 Failed!!!-×××" >> $LOG_FILE
else
echo "√√√-Install $1 Success-√√√" >> $LOG_FILE
fi
}
#安装依赖包
function prepare(){
yum install cmake gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make -y
check libs
#useradd
#添加用户和组, mysql用户不能登录
groupadd mysql
useradd mysql -s /bin/nologin -M -g mysql
check groupadd-useradd
#mkdir
#mysql文件目录更改组用户为mysql
chown -R mysql.mysql /data/mysql
check chown-mysql
#解压文件
cd $MYSQL_POS && tar zxf mysql-5.5.32.tar.gz
check MySQL-tar-zxf
}
#compile
#编译源码--指定安装位置--编码方式--支持的引擎--数据目录--用户--端口
function compile(){
cmake -DCMAKE_INSTALL_PREFIX=$MYSQL_BASEDIR -DMYSQL_UNIX_ADDR=$MYSQL_DATA/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=$MYSQL_DATA/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
}
#my.cnf
#将配置信息写入配置文件
function mycnf(){
cat >>/etc/my.cnf<<EOF
[client]
port = 3306
socket = $MYSQL_DATA/mysql.sock
[mysqld]
port = 3306
socket = $MYSQL_DATA/mysql.sock
basedir = $MYSQL_BASEDIR
datadir = $MYSQL_DATA/data
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
skip-name-resolve
log-bin=mysql-bin
binlog_format=mixed
max_binlog_size = 500M
server-id = 1
[mysqld_safe]
log-error=$MYSQL_DATA/mysql.err
pid-file=$MYSQL_DATA/mysql.pid
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF
}
#init
#初始化数据库 , 添加开机启动
function init(){
$MYSQL_BASEDIR/scripts/mysql_install_db --basedir=$MYSQL_BASEDIR --datadir=$MYSQL_DATA/data --user=mysql
cp $MYSQL_BASEDIR/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
}
#mail
#发送邮件(邮件配置参考上一篇文章)
function Mail(){
mail -s "***Linux Deploy Report***" 88888888@qq.com < $LOG_FILE
}
#main
#调用上边的函数
#首先是安装依赖包,然后编译源码:cmake , make, make install, 修改MySQL配置文件,设置环境变量,初始化MySQL,启动,发邮件
function main(){
ip=`awk '/IPADDR/' /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F "=" '{print $2}' `
echo "Deploy MySQL ****$ip****`date +"%Y-%m-%d_%H-%M-%S"`*****">>$LOG_FILE
prepare
cd $MYSQL_POS/mysql-5.5.32 && compile && make && make install
check make_install
ln -sv $MYSQL_BASEDIR /usr/local/mysql
mycnf
check mycnf
echo "export PATH=$MYSQL_BASEDIR/bin:$PATH" >> /etc/profile.d/mysql.sh
source /etc/profile
init
/etc/init.d/mysqld start
check mysql_start
mysql -uroot -e "select version();"
if [ $? == 0 ]
then
mysql -uroot -e "show databases;" >> $LOG_FILE
mysqladmin -uroot password '888888'
check mysql_change_password
else
echo "MySQL Install Failed" >> $LOG_FILE
fi
Mail
}
main