MySQL 5.7/8.0 二进制一键安装脚本.md

脚本支持 5.7 与 8.0 的二进制包的安装

脚本执行方式:

./installMysql.sh /opt/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz 3306

1.运行脚本 installMysql.sh
2.传入mysql安装包路径
3.传入mysql准备设置的端口 3306/3307等

默认安装路径

安装路径可以根据需求更改,只需要更改如下:
mysql_basedir="/usr/local"
mysql_datadir="/data/mysql$mysql_port"
mysql_tmpdir="/data/tmpdir"
my_cnf_dir="/data"

  • mysql 解压文件路径,与mysql程序路径
    /usr/local/mysql
    /usr/local/mysql-version-linux-glibc2.12-x86_64

  • 数据文件存放路径
    /data/mysql+指定的端口

  • 临时文件存放路径
    /data/tmpdir

installMysql.sh

#!/bin/bash


###################################################################################################
## filename      : installMysql.sh                                                            ##
## Author        : 杨清  https://www.jianshu.com/u/9f9bbfe7e267                                  ##
## attention     :my.cnf中参数在脚本中仅是建议参数,实际使用需要根据实际业务与机器进行修改。    ##
###################################################################################################

# 传参1:myqsl 二进制安装包全路径
# 传参2:预装端口

mysql_pkg=$1
mysql_port=$2

# mysql安装路径、数据存放路径、临时表空间路径
mysql_basedir="/usr/local"
mysql_datadir="/data/mysql$mysql_port"
mysql_tmpdir="/data/tmpdir"
my_cnf_dir="/data"

mysql_basename=`basename $mysql_pkg`
unzip_result_name=`basename $mysql_pkg|awk -F ".tar" '{print $1}'`

#echo "$mysql_pkg"
#echo "$mysql_basedir"
#echo "$mysql_tmpdir"

# 检查传入参数是否正确
check_parameter(){
  if [ ! -f $mysql_pkg ];then
    echo "【Error】:参数错误,请确认传入文件是否存在!"
    echo "【Message】:例如:./installMysql /opt/mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz 3306"
    exit
  fi

  if [ $mysql_port -gt 0 ];then
    echo "设置MySQL端口为: $mysql_port"
  else
    echo "输入错误,请检查端口是否正确!"
    exit
  fi
}

# 检查端口
check_port(){
  echo "检查端口是否存在..."
  port_cnt=`ss -tanl | grep $mysql_port |wc -l`
  if [ $port_cnt -eq 1 ]; then
    echo "  [Error]:端口已存在,请确认!"
    exit
  fi
}

# 检查或创建mysql数据文件路径
check_and_create_install_path(){
  echo "检查所需目录..."
  if [ ! -d "${mysql_basedir}" ];then
    echo "  创建mysql安装目录..."
    mkdir -p ${mysql_basedir}
  else
    echo "  【忽略】mysql安装目录已存在..."
  fi

  if [ ! -d "${mysql_datadir}" ];then
    echo "  创建数据目录并授权..."
    mkdir -p ${mysql_datadir}
    chown -R mysql.mysql ${mysql_datadir}
  else
    is_null=`ls ${mysql_datadir}|wc -l`
    if [ $is_null -gt 0 ];then
      echo "  【Error】:${mysql_pkg_path} 已存在,但目录不为空,请确认!【MySQL 初始化时数据目录必须为空】"
      exit
    else
      echo "  【忽略】数据目录已存在..."
    fi
  fi

  if [ ! -d "${mysql_tmpdir}" ];then
    echo "  创建mysql临时文件目录..."
    mkdir -p ${mysql_tmpdir}
    chown -R mysql.mysql ${mysql_tmpdir}
  else
    echo "  【忽略】临时文件目录已存在..."
  fi
}

# 检查目录权限,防止目录已存在,但权限不正确
check_dir_privileges(){
  data_dirname=`dirname $mysql_datadir`
  tmp_dirname=`dirname $mysql_tmpdir`
  
  data_basename=`basename $mysql_datadir`
  tmp_basename=`basename $mysql_tmpdir`

  data_priv=`ls -l $data_dirname|grep $data_basename|awk -F " " '{print $3$4}'`
  tmp_priv=`ls -l $tmp_dirname|grep $tmp_basename|awk -F " " '{print $3$4}'`
  
  if [ $data_priv != 'mysqlmysql' ];then
    echo "  【Error】:目录 $mysql_datadir 权限错误,请检查!"
    exit
  fi

  if [ $tmp_priv != 'mysqlmysql' ];then
    echo "  【Error】:目录 $mysql_tmpdir 权限错误,请检查!"
    exit
  fi
}

# 创建组与用户
check_and_create_group_user(){
  echo "创建mysql组与用户..."
  group_cnt=`grep "mysql" /etc/group|wc -l`
  user_cnt=`id mysql|wc -l`
  if [ $group_cnt -eq 0 ] && [ $group_cnt -eq 0 ];then
    echo "  mysql组与用户已创建..."
    groupadd mysql
    useradd  -g mysql -s /bin/nologin mysql
  elif [ $group_cnt -eq 1 ] && [ $group_cnt -eq 1 ];then
    echo "  【忽略】mysql 用户与组已存在..."
  else
    echo "  【Error】:请确认mysql组与mysql用户是否均存在!"
  fi
}

# 检查安装包版本
check_mysql_install_version(){
  mysql_version=`basename $mysql_pkg|awk -F "-" '{print $2}'|awk -F "." '{print $1"."$2}'`
  minor_versions=`basename $mysql_pkg|awk -F "-" '{print $2}'`
  if [ $mysql_version = '5.6' ];then
    echo "【Error】:此脚本不支持 5.6 版本安装!"
  else
    echo "即将安装版本为 $minor_versions"
  fi
}

# 解压缩二进制包
unzip_mysql_pkg(){
  echo "解压安装包..."
  unzip_result_name=`basename $mysql_pkg|awk -F ".tar" '{print $1}'`
  if [ -d "${mysql_basedir}/${unzip_result_name}" ];then
    echo "  【忽略】$unzip_result_name 已存在..."
  else
    echo "  开始解压缩,可能需要花费几分钟,请耐心等待..."
    
    tar xf $mysql_pkg -C $mysql_basedir
    
    # 8.0 可以下载 xz格式的包与包含router等包的tar格式的包,tar格式的需要再次解压
    if [ $mysql_version = '8.0' ];then
      tar_or_xz=`basename $mysql_pkg|awk -F "." '{print $(NF)}'`
      if [ $tar_or_xz = 'tar' ];then
        tar xf $mysql_basedir/$mysql_basename.xz -C $mysql_basedir
      fi
    fi
  fi
}

# 创建软链
create_mysql_link(){
  echo "创建软链..."
  if [ -d $mysql_basedir/mysql ];then
    now_mysql_link=`ls -l /usr/local/mysql|awk -F "/" '{print $(NF)}'`
    if [ $now_mysql_link = $unzip_result_name ];then
      echo "  【忽略】$mysql_basedir/mysql 软链已存在..."
    else
      # 若已存在,可考虑创建link为mysql56 这样的值,不过涉及的相关路径都需要改一下
      echo "  【Error:】当前mysql已与 $now_mysql_link 建立软链,请确认!"
      exit
    fi
  else
    ln -s $mysql_basedir/$unzip_result_name $mysql_basedir/mysql
  fi
}

# 设置环境变量
set_mysql_environment(){
  echo "设置环境变量..."
  is_env=`cat /etc/profile|grep $mysql_basedir/mysql/bin|wc -l`
  if [ $is_env -eq 0 ];then
    echo "export PATH=$PATH:$mysql_basedir/mysql/bin">>/ect/profile
    source /etc/profile
  else
    echo "  【忽略】环境变量已存在..."
  fi
}

# 设置配置文件
set_mysql_config(){
  echo "设置mysql参数文件..."
  mem_total=`free | awk '/Mem/ {print $2}'`
  end_of_ip=`ip a | grep 'inet '| grep -v '127.0.0.1'|awk -F "." '{ print $4}'|awk -F "/" '{ print $1}'`
  
  # 设置参数值
  if [ $mem_total -le 4194304 ];then
    join_buffer_size="1M"
    sort_buffer_size="1M"
    read_buffer_size="2M"
    table_open_cache=1024
    table_definition_cache=1024
    table_open_cache_instances=8
    innodb_buffer_pool_instances=4
  elif [ $mem_total -le 8388608 ];then
    join_buffer_size="4M"
    sort_buffer_size="4M"
    read_buffer_size="8M"
    table_open_cache=1024
    table_definition_cache=1024
    table_open_cache_instances=8
    innodb_buffer_pool_instances=4  
  elif [ $mem_total -le 16777216 ];then
    join_buffer_size="4M"
    sort_buffer_size="4M"
    read_buffer_size="8M"
    table_open_cache=1024
    table_definition_cache=1024
    table_open_cache_instances=8
    innodb_buffer_pool_instances=4
  elif [ $mem_total -gt 16777216 ];then
    join_buffer_size="4M"
    sort_buffer_size="4M"
    read_buffer_size="8M"
    table_open_cache=2048
    table_definition_cache=2048
    table_open_cache_instances=16
    innodb_buffer_pool_instances=8
  else
    echo "  [Error]:出现这一行就不对了,理论这一行是永远都不会出现的..."
  fi
  
  # 按照 75% 方式计算,向下取小,其中 128 是 innodb_buffer_pool_chunk_size 大小
  multiple_value=`expr $mem_total \* 3 / 4 / 128 / 1024 / $innodb_buffer_pool_instances`
  innodb_buffer_pool_size=`expr 128 \* 1024 \* $innodb_buffer_pool_instances \* $multiple_value` 
  #echo "multiple_value: $multiple_value |  innodb_buffer_pool_size: $innodb_buffer_pool_size"

  # 设置需求变量
  port="$mysql_port"
  basedir="$mysql_basedir/mysql"
  datadir="$mysql_datadir"
  tmpdir="$mysql_tmpdir"
  socket="/tmp/mysql$mysql_port.sock"
  log_error="$mysql_datadir/error.log"
  log_bin="$mysql_datadir/mybinlog"
  slow_query_log_file="$mysql_datadir/slow.log"
  server_id=$end_of_ip$mysql_port

# 配置my.cnf参数
cat > $my_cnf_dir/my$port.cnf << EOF

[client]
port = $port
socket = $socket

[mysql]
prompt="\u [\d]> "
no-auto-rehash

[mysqld]
# 路径配置
user                                                    = mysql                                         
port                                                    = $port                                  
basedir                                                 = $basedir                
datadir                                                 = $datadir                 
socket                                                  = $socket
log-error                                               = $log_error
log-bin                                                 = $log_bin
tmpdir                                                  = $tmpdir
slow_query_log_file                                     = $slow_query_log_file

# 基础配置                                              设置值                             默认值
max_connections                                         = 1024                              # 151
pid-file                                                = initnode.pid                      # host名
interactive_timeout                                     = 1200                              # 28800
wait_timeout                                            = 1200                              # 28800
transaction_isolation                                   = READ-COMMITTED                    # REPEATABLE-READ
innodb_buffer_pool_size                                 = $innodb_buffer_pool_size                             # 134217728 = 128M
innodb_buffer_pool_instances                            = $innodb_buffer_pool_instances                                 # 8 ,内存<1G时为1
default_time_zone                                       = "+8:00"                           # SYSTEM
character-set-server                                    = utf8mb4                           # 5.6/5.7 --> latin1 | 8.0 --> utf8mb4
skip_name_resolve                                       = 1                                 # OFF
open_files_limit                                        = 65535                             # 5000
back_log                                                = 1024                              # 151
max_connect_errors                                      = 1000000                           # 100
table_open_cache                                        = $table_open_cache                              # -1 自动调整大小
table_definition_cache                                  = $table_definition_cache                              # -1 自动调整大小
table_open_cache_instances                              = 8                                 # 16,通常使用16个或更多内核的系统上,建议将值设置为8或16。
thread_stack                                            = 512K                              # 286720
max_allowed_packet                                      = 32M                               # 67108864
thread_cache_size                                       = 768                               # -1 自动调整大小,大于 max_connections 一些
tmp_table_size                                          = 32M                               # 16777216
max_heap_table_size                                     = 32M                               # 16777216
lock_wait_timeout                                       = 3600                              # 31536000
explicit_defaults_for_timestamp                         = 1                                 # 5.6/5.7 --> OFF | 8.0 --> ON
lower_case_table_names                                  = 1                                 # 0
log_bin_trust_function_creators                         = 1                                 # OFF
event_scheduler                                         = 1                                 # 5.6/5.7 --> OFF | 8.0 --> ON
#看情况设置,设置了可使用 load 和 output                                                    # 
secure_file_priv                                        = ''                                #                                                                                          # 
sort_buffer_size                                        = $sort_buffer_size                                # 262144
join_buffer_size                                        = $join_buffer_size                                # 262144
log_timestamps                                          = SYSTEM                            #  UTC

# 慢查询                                                                                    # 
slow_query_log                                          = 1                                 # OFF
long_query_time                                         = 1                                 # 10
log_slow_admin_statements                               = 1                                 # OFF
#log_slow_slave_statements                              = 1                                 # 
                                                                                            # 
# 复制                                                                                      # 
server-id                                               = $server_id                        # 
sync_binlog                                             = 1                                 # 
binlog_cache_size                                       = 4M                                # 32768
max_binlog_cache_size                                   = 2G                                # 18446744073709551615
max_binlog_size                                         = 1G                                # 1073741824
expire_logs_days                                        = 7                                 # 0 | 8.0 --> binlog_expire_logs_seconds
master_info_repository                                  = TABLE                             # 5.6/5.7 --> FILE | 8.0 --> TABLE
relay_log_info_repository                               = TABLE                             # 5.6/5.7 --> FILE | 8.0 --> TABLE
gtid_mode                                               = on                                # OFF
enforce_gtid_consistency                                = 1                                 # OFF
log_slave_updates                                       = 1                                 # 5.6/5.7 --> OFF | 8.0 --> ON
slave-rows-search-algorithms                            = 'INDEX_SCAN,HASH_SCAN'            # 5.6/5.7 --> TABLE_SCAN,INDEX_SCAN | 8.0 --> INDEX_SCAN,HASH_SCAN
binlog_format                                           = row                               # ROW
binlog_checksum                                         = 1                                 # CRC32
relay_log_recovery                                      = 1                                 # OFF
relay-log-purge                                         = 1                                 # ON

## replication
#plugin_load                                            = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
#plugin_load                                            = "validate_password.so"            # 
#loose_rpl_semi_sync_master_enabled                     = 1                                 # 
#loose_rpl_semi_sync_slave_enabled                      = 1                                 # 
#loose_rpl_semi_sync_master_timeout                     = 3000                              # 
#slave-parallel-type                                    = LOGICAL_CLOCK                     # 
#slave-parallel-workers                                 = 4                                 # 
#slave_preserve_commit_order                            = 1                                 # 
#binlog_gtid_simple_recovery                            = 1                                 # 
                                                                                            # 
# MyISAM,在8.0环境能进一步调小                                                                                   # 
key_buffer_size                                         = 32M                               # 8388608
read_buffer_size                                        = $read_buffer_size                                # 131072
read_rnd_buffer_size                                    = 4M                                # 262144
bulk_insert_buffer_size                                 = 64M                               # 8388608
myisam_sort_buffer_size                                 = 128M                              # 8388608
myisam_max_sort_file_size                               = 5G                                # 9223372036854775807
myisam_repair_threads                                   = 1                                 # 1

# innodb                                                                                    # 
innodb_buffer_pool_load_at_startup                      = 1                                 # ON
innodb_buffer_pool_dump_at_shutdown                     = 1                                 # ON
innodb_data_file_path                                   = ibdata1:200M:autoextend           # ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit                          = 1                                 # 1
innodb_log_buffer_size                                  = 32M                               # 16777216
innodb_log_file_size                                    = 2G                                # 50331648
innodb_log_files_in_group                               = 3                                 # 2
# 根据服务器IOPS能力适当调整                                                               # 
innodb_io_capacity                                      = 2000                              # 200
innodb_io_capacity_max                                  = 4000                              # 2000
# innodb_flush_neighbors参数,SSD 禁用,非SSD请启用                                         # 
innodb_flush_neighbors                                  = 1                                 # 0
innodb_write_io_threads                                 = 8                                 # 4
innodb_read_io_threads                                  = 8                                 # 4
innodb_purge_threads                                    = 4                                 # 4
innodb_open_files                                       = 65535                             # -1
innodb_flush_method                                     = O_DIRECT                          # NULL
innodb_checksum_algorithm                               = crc32                             # crc32
innodb_lock_wait_timeout                                = 10                                # 50
innodb_rollback_on_timeout                              = 1                                 # OFF
innodb_file_per_table                                   = 1                                 # ON
innodb_online_alter_log_max_size                        = 4G                                # 134217728
## undo log                                                                                 # 
innodb_max_undo_log_size                                = 2G                                # 1073741824
## innodb_undo_directory、innodb_undo_tablespaces 在5.6环境不建议开启                       # 
innodb_undo_tablespaces                                 = 3                                 #  2
# 遵守innodb_io_capacity设置定义的I / O速率                                                 # 
innodb_flush_sync                                       = 0                                 #  1
innodb_page_cleaners                                    = 4                                 #  

# performance_schema                                                                        # 
performance_schema                                      = 1                                 # ON
performance_schema_instrument                           = '%lock%=on'                       # 
                                                                                            # 
#innodb monitor                                                                             # 
innodb_monitor_enable                                   = "module_innodb"                   # 
innodb_monitor_enable                                   = "module_server"                   # 
innodb_monitor_enable                                   = "module_dml"                      # 
innodb_monitor_enable                                   = "module_ddl"                      # 
innodb_monitor_enable                                   = "module_trx"                      # 
innodb_monitor_enable                                   = "module_os"                       # 
innodb_monitor_enable                                   = "module_purge"                    # 
innodb_monitor_enable                                   = "module_log"                      # 
innodb_monitor_enable                                   = "module_lock"                     # 
innodb_monitor_enable                                   = "module_buffer"                   # 
innodb_monitor_enable                                   = "module_index"                    # 
innodb_monitor_enable                                   = "module_ibuf_system"              # 
innodb_monitor_enable                                   = "module_buffer_page"              # 
innodb_monitor_enable                                   = "module_adaptive_hash"            # 
                                                                                            # 

[mysqld-5.7]                                                                                # 
query_cache_size                                        = 0                                 # 1048576
query_cache_type                                        = 0                                 # 0
#索引767限制                                                                                # 
innodb_large_prefix                                     = ON                                # 5.6/5.7 --> OFF | 8.0.0 已删除
                                                                                            # 
[mysqld-8.0]                                                                                # 
log_error_verbosity                                     = 3                                 # 
innodb_print_ddl_logs                                   = 1                                 # 
binlog_expire_logs_seconds                              = 604800                            # 
                                                                                            # 
[mysqldump]                                                                                 # 
quick                                                                                       # 
max_allowed_packet                                      = 32M                               # 
EOF
}

# 初始化
mysql_initialize(){
  echo "初始化MySQL..."
  cd $mysql_basedir/mysql 
  bin/mysqld --defaults-file=$my_cnf_dir/my$port.cnf --initialize  --user=mysql
}

# 启动
start_mysql(){
  echo "启动MySQL..."
  $mysql_basedir/mysql/bin/mysqld_safe --defaults-file=$my_cnf_dir/my$port.cnf --user=mysql > /dev/null &
}

# 检查启动状态
check_mysql_status(){
  mysql_status=`ps -ef|grep $my_cnf_dir/my$port.cnf|grep -v grep|wc -l`
  echo "mysql_status:   $mysql_status"
  if [ $mysql_status -gt 0 ];then
    echo "安装完成,MySQL已启动..."
    init_pasword=`cat $mysql_datadir/error.log |grep 'A temporary password'|awk -F " " '{print $(NF)}'`
    echo "初始化密码为: $init_pasword"
    echo "请使用 mysql -uroot -S $socket -p'$init_pasword' 进行登陆。"
    echo "请使用 alter user user() identified by '新密码';  修改root密码。"
  else
    echo "启动异常,请查看错误日志!【详细请使用命令:tail -100 $mysql_datadir/error.log 查看。】"
    cat $log_error|grep -i error
  fi
}

check_parameter
check_port
check_and_create_group_user
check_and_create_install_path
check_dir_privileges
check_mysql_install_version
unzip_mysql_pkg
set_mysql_environment
create_mysql_link
set_mysql_config
mysql_initialize
start_mysql
check_mysql_status

你可能感兴趣的:(MySQL 5.7/8.0 二进制一键安装脚本.md)