/ansible/roles/mysql/files/mysql_install.sh
#!/bin/bash
####定义mysql数据库路径,和mysql登录密码
DBDIR='/application/mysql/data'PASSWD='bingoclo123'####判断数据目录是否存在如果不存在递归创建目录
[-d $DBDIR ] || mkdir $DBDIR -p
####安装mysql组件
yum install cmake make gcc-c++ bison-devel ncurses-devel -y
id mysql&> /dev/null####如果执行id mysql输出为0 那么就是执行正确创建mysql用户if [ $? -ne 0];then
useradd mysql-s /sbin/nologin -M
fi
####赋予数据目录权限
chown-R mysql.mysql $DBDIR
####切换到tmp目录,解压mysql,编译安装mysql到/application/mysql
cd/tmp/tar xf mysql-5.6.35.tar.gz
cd mysql-5.6.35cmake .-DCMAKE_INSTALL_PREFIX=/application/mysql \-DMYSQL_DATADIR=$DBDIR \-DMYSQL_UNIX_ADDR=$DBDIR/mysql.sock \-DDEFAULT_CHARSET=utf8 \-DEXTRA_CHARSETS=all \-DENABLED_LOCAL_INFILE=1\-DWITH_READLINE=1\-DDEFAULT_COLLATION=utf8_general_ci \-DWITH_EMBEDDED_SERVER=1####判断如果以上执行结果不等于0,说明执行失败,退出!if [ $? != 0];then
echo"cmake error!"exit1fi
####判断执行成功,继续往下走
make&&make installif [ $? -ne 0];then
echo"install mysql is failed!" && /bin/falsefi
sleep2####做软连接
ln-s /application/mysql/bin/*/usr/bin/
####复制mysql配置文件
cp -f /application/mysql/support-files/my-default.cnf /etc/my.cnf
####复制mysql启停脚本到init.d
cp -f /application/mysql/support-files/mysql.server /etc/init.d/mysqld
####执行权
chmod 700 /etc/init.d/mysqld
####初始化mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=$DBDIR --user=mysql
####如果mysql初始化失败提示install mysql is failed! 否则/etc/init.d/mysqld start
if [ $? -ne 0 ];then
echo "install mysql is failed!" && /bin/false
fi
/etc/init.d/mysqld start
####如果mysql开启失败提示install mysql is failed! 否则继续往下走
if [ $? -ne 0 ];then
echo "install mysql is failed!" && /bin/false
fi
####开机自启
chkconfig --add mysqld
chkconfig mysqld on
/application/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user=
'root';"
/application/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user=
'root';"
/application/mysql/bin/mysql -e "delete from mysql.user where password='';"
/application/mysql/bin/mysql -e "flush privileges;"
if [ $? -eq 0 ];then
echo "ins_done"
fi