侧边栏壁纸
博主头像
高大北博主等级

所有的再见中,我最喜欢明天见

  • 累计撰写 208 篇文章
  • 累计创建 151 个标签
  • 累计收到 20 条评论
标签搜索

目 录CONTENT

文章目录

centos7一键离线安装脚本-mysql5.7

高大北
2022-04-18 / 0 评论 / 1 点赞 / 314 阅读 / 3,392 字 / 正在检测是否收录...

使用方法

source install_mysql.sh

install_mysql.sh接收$1参数,设置mysql密码,默认密码为mysql初始密码,会打印出来。

$2为安装路径,默认安装路径为/usr/local.

$3为数据存储路径,默认安装路径为/usr/local/mysql/data.

install_mysql.sh

#!/bin/bash

. /etc/init.d/functions
password=$1
directory=$2
data_dir=$3
current_dir=`pwd`

function select_installation_directory(){
  if [[ -z ${directory} ]];
  then
    directory='/usr/local'
  fi
  if [[ -z ${data_dir} ]];
  then
    data_dir=${directory}/mysql/data
  fi
}

function delete_mariadb(){
  for i in $(rpm -qa | grep mariadb | grep -v grep)
  do
    echo "卸载mariadb -> "$i
    rpm -e --nodeps $i
  done
}

function check_gcc(){
  if [[ -z $(rpm -qa | grep ^gcc | grep -v grep) ]];
  then
    cd gcc && rpm -Uvh *.rpm --nodeps --force && cd ..
  fi
}

function create_mysql_config(){
  [[ -f /etc/my.cnf ]] && mv /etc/my.cnf /etc/my.cnf-$(date +%F-%H:%H:%S)
  cd "${current_dir}" && cp my.cnf /etc/
  system_mem_size=$(grep "^MemTotal" /proc/meminfo |awk '{print $2}')
  innodb_buffer_pool_size="$(echo "${system_mem_size}"/1024/10*6|bc)M"
  sed -i "s#^socket.*#socket = ${data_dir}/mysql.sock#g" /etc/my.cnf
  sed -i "s#^slow_query_log_file.*#slow_query_log_file = ${data_dir}/slow.log#g" /etc/my.cnf
  sed -i "s#^log-error.*#log-error = ${data_dir}/error.log#g" /etc/my.cnf
  sed -i "s#^log-bin.*#log-bin = ${data_dir}/mybinlog#g" /etc/my.cnf
  sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = ${innodb_buffer_pool_size}M#g" /etc/my.cnf
  sed -i "s#^innodb_undo_directory.*#innodb_undo_directory = ${data_dir}/undolog#g" /etc/my.cnf
  sed -i "s#^basedir.*#basedir = ${directory}/mysql#g" /etc/my.cnf
  sed -i "s#^datadir.*#datadir = ${data_dir}#g" /etc/my.cnf
  echo -e "\n"
}

function add_mysql_user(){
  id mysql >& /dev/null
  if [[ $? -ne 0 ]];
  then
    echo "用户不存在,开始创建mysql用户"
    groupadd mysql
    useradd -g mysql mysql
  fi
}

function install_mysql(){
  tar_name=$(ls .|grep mysql-.*.tar.gz)
  tar_dir=${tar_name%.tar.gz*}
  tar -zxvf "${current_dir}"/"${tar_name}"
  mv "${current_dir}"/"${tar_dir}" ${directory}/mysql
  if [ ! -d ${data_dir} ];then
    mkdir ${data_dir}
  fi
  echo -e "开始初始化数据库"
  cd ${directory}/mysql && bin/mysqld --initialize --user=mysql --basedir=${directory}/mysql --datadir=${data_dir}
  if [[ $? -eq 0 ]];then
      action "MySQL初始化完毕" /bin/true
      mysql_init_pwd=$(grep "temporary password" ${data_dir}/error.log  |awk -F "[: ]" '{print $NF}')
      echo "MySQL初始化密码为:${mysql_init_pwd}"
      chown -R mysql:mysql ${directory}/mysql
      echo -e "\n"
  else
      action "MySQL初始化失败请查看${data_dir}/error.log日志"  /bin/false
      exit
  fi
}

function powered_up_mysql(){
  cd ${directory}/mysql/support-files && cp mysql.server /etc/init.d/mysql
  sed -i "s#^basedir=.*#basedir=${directory}/mysql#g" /etc/init.d/mysql
  sed -i "s#^datadir=.*#datadir=${data_dir}#g"  /etc/init.d/mysql
  service mysql start
  chkconfig mysql on
}

function login_mysql(){
  if [[ -z ${password} ]];
  then
    password=${mysql_init_pwd}
  fi
  login_mysql="${directory}/mysql/bin/mysql -uroot -p${mysql_init_pwd} --connect-expired-password"
  ${login_mysql} <<EOF
  alter user 'root'@'localhost' identified by '${password}';
  use mysql;
  update user set host='%' where user='root' and host='localhost';
  flush privileges;
EOF
}

function Main(){
  select_installation_directory;
  delete_mariadb;
  check_gcc;
  create_mysql_config;
  add_mysql_user;
  install_mysql;
  powered_up_mysql;
  login_mysql;
}
Main;

相关依赖包下载地址

https://www.sky12580.cn/upload/2022/04/mysql.zip

1

评论区