1 [root@ansible roles]# cat /etc/ansible/roles/mysql_install/templates/mysql_install.sh
2 #!/bin/bash3
4 DATADIR='/data/mysql/data'
5 VERSION='{{mysql_version}}'
6 export LANG=zh_CN.UTF-8
7
8 #Source functionlibrary.9 . /etc/init.d/functions10
11 #camke install mysql5.5.X12 install_mysql(){13 #read -p "please input a password for root:"PASSWD14 PASSWD='ly36843'
15 if [ ! -d $DATADIR ];then
16 mkdir -p $DATADIR17 fi
18 yum install cmake make gcc-c++ bison-devel ncurses-devel -y19 id mysql &>/dev/null
20 if [ $? -ne 0 ];then
21 useradd mysql -s /sbin/nologin -M22 fi
23 #useradd mysql -s /sbin/nologin -M24 #change datadir owner to mysql25 chown -R mysql.mysql $DATADIR26 cd27 #wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.38.tar.gz
28 tar xf $VERSION.tar.gz29 cd $VERSION30 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/$VERSION \31 -DMYSQL_DATADIR=$DATADIR \32 -DMYSQL_UNIX_ADDR=$DATADIR/mysql.sock \33 -DDEFAULT_CHARSET=utf8 \34 -DDEFAULT_COLLATION=utf8_general_ci \35 -DENABLED_LOCAL_INFILE=ON \36 -DWITH_INNOBASE_STORAGE_ENGINE=1\37 -DWITH_FEDERATED_STORAGE_ENGINE=1\38 -DWITH_BLACKHOLE_STORAGE_ENGINE=1\39 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1\40 -DWITHOUT_PARTITION_STORAGE_ENGINE=1
41 make && make install
42 if [ $? -ne 0 ];then
43 action "install mysql is failed!" /bin/false
44 exit $?
45 fi
46 sleep 2
47 #link48 ln -s /usr/local/$VERSION/ /usr/local/mysql49 ln -s /usr/local/mysql/bin/*/usr/bin/50 #copy config and start file51 /bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf52 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld53 chmod 700 /etc/init.d/mysqld54 #init mysql55 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=$DATADIR --user=mysql56 if [ $? -ne 0 ];then57 action "install mysql is failed!" /bin/false58 exit $?59 fi60 #check mysql61 /etc/init.d/mysqld start62 if [ $? -ne 0 ];then63 action "mysql start is failed!" /bin/false64 exit $?65 fi66 chkconfig --add mysqld67 chkconfig mysqld on68 /usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';"69 /usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user='root';"70 /usr/local/mysql/bin/mysql -e "delete from mysql.user where password='';"71 /usr/local/mysql/bin/mysql -e "flush privileges;"72 #/usr/local/mysql/bin/mysql -e "select version();" >/dev/null 2>&173 if [ $? -eq 0 ];then74 echo "+---------------------------+"75 echo "+------mysql安装完成--------+"76 echo "+---------------------------+"77 fi78 #/etc/init.d/mysqld stop79 }80
81 install_mysql