Linux shell脚本超详细安装mysql

shell脚本自动安装mysql

#!/bin/bash

# 自动化脚本安装mysql

# 本文使用的是wget 下载mysql包

# 官方mysql下载的地址:https://downloads.mysql.com/archives/community/

# 我这里使用 https://dev.mysql.com/get/Downloads/MySQL-5.7/

# 本文下载的包 mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

# mysql安装的主目录 /usr/local/mysql

# mysql 设置默认密码为:123456

#安装mysql需要管理员权限
[ $UID -ne 0 ] && echo "need to be root so that" && exit 1

echo  "安装中, 请稍等 ... "
# 接收mysql 版本参数
mysqlVersion="5.7.33"
# 下载wget
isInstallWget=`yum list installed | grep wget`

if [ $? -ne 0 ];then
   yum -y install wget &>/dev/null
fi

# 准备创建 mysql 安装目录
if [ -d /usr/local/mysql ];then
   echo "已存在mysql 目录,自行确认是否需要删除该目录";
   exit 1
fi

# 使用wget下载mysql包
echo "下载mysql包..."
dirName=mysql-5.7.33-linux-glibc2.12-x86_64
[ ! -e /opt ] && mkdir /opt
isExistMysqlPackage=`ls /opt/$dirName.tar.gz`;
echo "下载中... 请稍等"
if [ $? -ne 0 ];then
  wget -P /opt/ "https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz" &>/dev/null
  if [ $? -ne 0 ];then
    echo "下载失败,该版本: $mysqlVersion 不存在"
    exit 1;
  fi
fi
sleep 1
echo "mysql 下载已完成"


if [ ! -e $dirName ];then
  # --no-same-owner 新增此参数 将解压出来的包 归属于当前用户
  tar --no-same-owner -xf "/opt/$dirName.tar.gz"  -C /opt/
fi

if [ ! -d /usr/local/mysql ];then
  # 移动并修改成mysql 目录
  cd /usr/local/
  mv /opt/$dirName mysql
  # 创建日志文件
  [ ! -d /usr/local/mysql/logs ] && mkdir /usr/local/mysql/logs
  [ ! -e /usr/local/mysql/logs/mysqld.log ] && touch  /usr/local/mysql/logs/mysqld.log
  chmod -R 777 /usr/local/mysql/logs
  [ ! -d /usr/local/mysql/run ] && mkdir /usr/local/mysql/run
  [ ! -e /usr/local/mysql/run/mysqld.pid ] && touch  /usr/local/mysql/run/mysqld.pid
  chmod -R 777 /usr/local/mysql/run
  #初始化安装mysql
  echo "初始化安装mysql...请稍等"
  /usr/local/mysql/bin/mysqld --initialize --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data  --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US 
  #给/usr/local/mysql/data 添加写入权限
  chmod -R 755 /usr/local/mysql/data  
fi
echo "mysql已安装完成"

# 启动mysql
/usr/local/mysql/support-files/mysql.server start

# 启动可能会出现如下错误:
# Starting MySQL... ERROR! The server quit without updating PID file

# 需要在/usr/local/mysql/support-files/mysql.server 文件中266行 $bindir/mysqld_safe 此处新增 --user=root

# 设置新密码 旧密码在 日志文件中 mysqld.log 中该行 【A temporary password is generated for root@localhost: Fa_Rbp2Tad3=】 密码:Fa_Rbp2Tad3=
# 其他权限设置
<<mysqlText
 # 设置密码
 mysql> set password=password('新密码');
 #即时刷新
 mysql> flush privileges;
 #更新权限
 mysql> UPDATE `mysql`.`user` SET `Host` = '%',  `User` = 'root'  WHERE (`Host` = 'localhost') AND (`User` = 'root');
mysqlText

# 设置开机自动重启
<<mysqlOpen
  cd /usr/local/mysql/support-files
  cp mysql.server /etc/init.d/mysqld
  chkconfig --add mysqld

mysqlOpen

# 重启 service mysqld start/stop/restart

<<myCnf
# 如下是/etc/my.cnf 文件的配置
[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/run/mysqld.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

myCnf

你可能感兴趣的:(Shell,linux,mysql,数据库)