首页 » MySQL » shell 脚本一键安装mysql数据库(已打包成图形化安装)

shell 脚本一键安装mysql数据库(已打包成图形化安装)

原文 http://blog.csdn.net/u013255206/article/details/79176070

2018-01-28 02:01:11阅读(315)

install_scripts.sh

作用:解压mysql安装包并且注册mysql为一个服务(service),并开机自启动,并且弹出dialog窗体提供安装前配置(用户密码端口配置)。

#!/bin/sh
install_packet_name="mysql"
install_packet_tarball_name=$install_packet_name".tar.gz"
current_install_user="root"
#install_user="mysql"
install_user=$current_install_user
mysql_current_status=0
install_path=/usr/local
#mysql_path=/usr/local/${install_packet_name}
mysql_path=/usr/local
#mysql_data_root=~/tdata
mysql_data_root=/tdata
mysql_data_path=$mysql_data_root/data
mysql_sock_path=$mysql_data_root/mysql.sock
mysql_user=sean
mysql_port=3306
mysql_passwd=sean
mysql_admin_passwd=123456
env_check()
{
    current_install_user=$(whoami)
    if [ "$current_install_user" != "root" ]; then
        echo "please switch to root user to run this script"
        exit
    fi
    if [ "$install_user" != "root" ]; then
      egrep "^$install_user" /etc/group >& /dev/null
      if [ $? -ne 0 ]; then
        groupadd mysql
      fi
      egrep "^$install_user" /etc/passwd >& /dev/null
      if [ $? -ne 0 ];then
        useradd -g mysql mysql
      fi
    fi
#    systemctl stop mysql
}
mysql_exist_clear()
{
  systemctl stop mysql
  test ! -f /etc/rc.d/init.d/mysql || rm -rf /etc/rc.d/init.d/mysql
  test ! -d $mysql_data_root || rm -rf $mysql_data_root
  test ! -f /etc/my.cnf || rm -rf /etc/my.cnf
  test ! -d /usr/local/mysql || rm -rf /usr/local/mysql
}
slice_the_files()
{
  sed -n -e '1,/^Do you understand the feeling of missing someone?$/!p' $0 > $install_path/$install_packet_tarball_name 2>/dev/null
}
set_mysql_config()
{
  cat /dev/null > /tmp/TF_Install.config_db
  dialog --title "同方泰德视频平台安装服务(数据库安装)" --form "请输入数据库信息:" 12 40 4  \
  "数据库连接端口:" 1  1 $mysql_port 1  15  15  0  \
  "数据库管理员密码:" 2  1 $mysql_admin_passwd 2  15  15  0  \
  "登录用户名:" 3  1 $mysql_user 3  15  15  0  \
  "登录密码:"    4   1 $mysql_passwd 4  15  15  0 2> /tmp/TF_Install.config_db
  result=$?
  if [ $result -eq 1 ]; then
    exit 1
  elif [ $result -eq 255 ]; then
    exit 255
  fi
  mysql_port=$(cat /tmp/TF_Install.config_db |sed -n "1p")
  mysql_admin_passwd=$(cat /tmp/TF_Install.config_db |sed -n "2p")
  mysql_user=$(cat /tmp/TF_Install.config_db |sed -n "3p")
  mysql_passwd=$(cat /tmp/TF_Install.config_db |sed -n "4p")
  
  mysql_install_proc
}
mysql_install_proc()
{
  slice_the_files
  cd ${install_path}
  tar xzvf $install_packet_tarball_name -C $install_path
  
  if [ $? -ne 0 ]; then
    echo -e "\e[1;31m[extract packet failure...]\e[0m"
    exit 1
  fi
  rm -rf $install_packet_tarball_name
#  chown -R $current_install_user:$current_install_user $install_packet_name
  chown -R $install_user:$install_user $install_packet_name
  cd $install_packet_name && cp support-files/my-default.cnf ./my.cnf
  mkdir -p $mysql_data_path
#  chown -R $current_install_user:$current_install_user $mysql_data_root
  chown -R $install_user:$install_user $mysql_data_root
 
  sed -i "/mysqld/a\datadir = ${mysql_data_path}"  my.cnf
  sed -i "/mysqld/a\basedir = ${install_path}/$install_packet_name" my.cnf
  sed -i "/mysqld/a\user = ${install_user}" my.cnf
  sed -i "/mysqld/a\character-set-server=utf8" my.cnf
  sed -i "/mysqld/a\port = ${mysql_port}" my.cnf
  sed -i "/mysqld/a\socket = ${mysql_sock_path}" my.cnf
  sed -i "/mysqld/i\[mysql]" my.cnf
  sed -i "/mysqld/i\default-character-set=utf8" my.cnf
  sed -i "/mysqld/i\socket = ${mysql_sock_path}" my.cnf
  sed -i "/mysqld/i\ " my.cnf
  test ! -f /etc/my.cnf || rm -rf /etc/my.cnf
  cp my.cnf /etc/my.cnf
  scripts/mysql_install_db --basedir=$install_path/$install_packet_name --user=$install_user --defaults-file=$install_path/$install_packet_name/my.cnf
  if [ $? -ne 0 ]; then
    echo -e "\e[1;31m[MySQL install init failure...]\e[0m"
    exit 1
  fi
  cd $install_path/$install_packet_name
  echo "Set mysql as system service"
  cp support-files/mysql.server /etc/rc.d/init.d/mysql
  chmod +x /etc/rc.d/init.d/mysql
  chkconfig mysql on
  systemctl start mysql
  
  run_status=$(systemctl status $install_packet_name|grep running)
  echo $run_status
  if [ "$run_status" == "" ]; then
    echo -e "\e[1;31m[MySQL start failure...]\e[0m"
    exit 1
  else
    echo -e "\e[1;32m[MySQL start secessful]\e[0m"
  fi
  export PATH=$PATH:$install_path/$install_packet_name/bin 
  mysql -uroot  -e  "CREATE USER 'sean'@'%' IDENTIFIED BY 'sean';grant all on *.* to 'sean'@'%';CREATE USER 'sean'@'localhost' IDENTIFIED BY 'sean';grant all on *.* to 'sean'@'localhost';flush privileges;"
  if [ $? -eq 0 ]; then
    echo "Set mysql user sean password and create default database successfully!"
  fi
  exit 0
}
mysql_exist_check()
{
  run_status=$(systemctl status $install_packet_name|grep running)
  echo $run_status
  if [ "$run_status" != "" ]; then
    #mysql_current_status=0
    dialog --title "检测到数据库已经存在" --backtitle "同方泰德视频平台安装服务(数据库安装)" --yesno "是否重新安装?\n" 16 51
    result=$?
    if [ $result -eq 1 ];then
      exit 1;
    elif [ $result -eq 255 ];then
      exit 255;
    fi
    echo "start to downloading.......\n"
    
    mysql_exist_clear
  fi
    set_mysql_config
}
if [ ! -n "$1" ];then
  gnome-terminal -x bash -c "./同方泰德数据库安装服务\(mysql\).x -r; exec bash;"
else
  env_check
  mysql_exist_check  
fi
build_script.sh

作用:构建.x安装文件,使用到了shc命令(具体参考shc加密的使用)来加密上面install_scripts.sh脚本,并且和mysql压缩包(版本mysql-5.6.38-linux-glibc2.12-x86_64.tar)一块打包成为一个可执行文件,注意还有被打包进去的slice.txt文件里面就写了一串英文句子,目的就是sed分离安装包的时候能够以这句话为分割标记点,从而可以分别sed分离出安装包里的脚本文件和压缩包,因此cat写入安装包的时候也是先写入install_scripts.sh脚本,然后是分割标记文件slice.txt,最后才是mysql二进制压缩包。拼接分割文件具体可以参考sed的使用。

#!/bin/sh
CFLAGS=-static shc -v -r -T -f install_script.sh
cat install_script.sh.x slice.txt mysql.tar.gz > 同方泰德数据库安装服务\(mysql\).x
chmod +x 同方泰德数据库安装服务\(mysql\).x

最后注意的是 .x可执行文件要拷贝到桌面上双击运行,不是桌面下无法弹出窗体。弹出界面后直接输入设定mysql用户名密码和端口,不修改的话就默认下一步就行,一直enter下一步就成功了。 如果提示没有权限就赋读写权限,实在不行用root用户执行。

需要的文件目录

<a href=shell 脚本一键安装mysql数据库(已打包成图形化安装)" src="http://img.blog.csdn.net/20180126201106381?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzI1NTIwNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹