参考文档:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html 、https://dev.mysql.com/doc/refman/5.6/en/binary-installation.html
环境介绍:
Red Hat Enterprise Linux Server release 7.2 (Maipo),脚本可实现 MysQL 5.6 ,5.7 自动化安装
ins_dir=/usr/local //mysql 家目录
datadir=/data/mysql/mysql_$version // mysql 数据目录
src_dir=/usr/local/src/$version // mysql 二进制压缩包目录
/etc/my.cnf 为初始参数文件可根据自己需要进行修改
1 #!/bin/bash 2 echo "********add group,user **************" 3 if id -g mysql > /dev/null 2>&1;then 4 echo "mysql group is exits;" 5 else 6 groupadd mysql >/dev/null 2>&1 && echo "creating mysqlgroup success;" 7 fi 8 9 if id -u mysql > /dev/null 2>&1;then 10 echo "mysql user is exits;" 11 else 12 useradd -r -g mysql -s /bin/false mysql > /dev/null 2>&1 && echo "creating mysqluser success;" 13 fi 14 15 rpm -qa libaio || yum install libaio -y > /dev/null 2>&1 16 read -p "enter mysql version eg:5.7 :" version 17 datadir=/data/mysql/mysql_$version 18 src_dir=/usr/local/src/$version 19 ins_dir=/usr/local/ 20 [ -d $datadir ] || mkdir -p $datadir 21 cd $src_dir 22 tar -xf $(ls mysql-*) -C $ins_dir 23 cd $ins_dir 24 ln -s $ins_dir/$(ls |grep mysql-$version*) /usr/local/mysql$version 25 echo "export PATH=$PATH:/usr/local/mysql$version/bin" >> /etc/profile 26 27 cat << EOF >/etc/my.cnf 28 [mysqld] 29 port = 3306 30 user = mysql 31 datadir = $datadir 32 log_error = error.log 33 [mysql] 34 prompt = (u@h) [d]>\_ 35 EOF 36 37 case $version in 38 "5.7") 39 cd /usr/local/mysql$version 40 mkdir mysql-files 41 chmod 755 mysql-files 42 chown mysql.mysql mysql-files 43 bin/mysqld --initialize --user=mysql #./mysqld --defaults-file=/etc/my_57.cnf --initialize-insecure --user=mysql 44 [ $? -eq 0 ] && echo " installing mysql-server success" 45 chown -R mysql.mysql /usr/local/mysql$version 46 cp support-files/mysql.server /etc/init.d/mysqld 47 bin/mysqld_safe --user=mysql & > /dev/null 2>&1 48 pass=`grep "temporary password" $datadir/error.log |awk -F " " '{print $11}'` 49 mysqladmin -uroot -p'$pass' -S /tmp/mysql.sock password '123' 50 ;; 51 "5.6") 52 cd /usr/local/mysql$version 53 scripts/mysql_install_db --user=mysql >/dev/null 54 bin/mysqld_safe --user=mysql & > /dev/null 2&>1 55 cp support-files/mysql.server /etc/init.d/mysqld 56 ;; 57 esac