Mysql Shell 脚本一键部署

###### 二进制自动安装数据库脚本将脚本和安装包放在/home目录即可########### 用户root密码####
#mysql 安装包的绝对路径,去掉.tar.gz
tarGzPath=/home/mysql-5.7.24-linux-glibc2.12-x86_64
#mysql 安装路径
installPath=/home
#mysql 密码(不可擅自修改)
defaultPwd=
#mysql 端口
mysqlPort=3306
# 校验是否为ROOT用户
CheckRoot()
{
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi
clear
}
CheckRoot

#优化文件最大打开数
InstallDependsAndOpt()
{
cat >>/etc/security/limits.conf<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
echo "fs.file-max=65535" >> /etc/sysctl.conf
}
InstallDependsAndOpt

# install the basie lib
InstallBasieLib(){
lib=("vim*" "cmake" "gcc" "gcc-c++" "ncurses-devel.x86_64" "cmake.x86_64" "libaio.x86_64" "bison.x86_64" "gcc-c++.x86_64")
for i in ${lib[@]}
do
echo -e "\e[32m 检查是否安装$i... \e[0m"
sudo yum list installed $i &> /dev/null
if [[ $? -ne 0 ]]; then
	echo -e "\e[35m 自动安装$i... \e[0m"
	sudo yum install $i -y > /dev/null
fi
done
}
#InstallBasieLib

#拷贝tar.gz包
CheckTarGz()
{
if [ ! -e ${tarGzPath}.tar.gz ];then
    echo -e "\e[31m ${tarGzPath} 不存在!请检查后重新执行脚本 \e[0m"
    exit 1
fi
#解压并重命名到安装目录
tar -xvf ${tarGzPath}.tar.gz
mv ${tarGzPath} ${installPath}/mysql

#创建 mkdir /home/mysql/data
if [ ! -d ${installPath}/mysql/data ];then
    mkdir ${installPath}/mysql/data
fi
}
CheckTarGz

#添加组合角色
AddUser()
{
groupadd -f mysql
useradd -r -s /sbin/nologin -g mysql mysql -d ${installPath}/mysql

#赋权限
#chown -R mysql .
#chgrp -R mysql .
#chown -R mysql ${installPath}/mysql/data/
#chmod -R 777 ${installPath}//mysql/
#chmod -R 777 ${installPath}/
}
AddUser

#修改my.cnf/mysql.server
updateCnf()
{
cat >/etc/my.cnf<<EOF
[client]
port = ${mysqlPort}
socket = /tmp/mysql.sock

[mysqld]

###############################基础设置#####################################

#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id = 1

#服务端口号 默认3306
port = 3306

#mysql安装根目录
basedir = /home/mysql

#mysql数据文件所在位置
datadir = /home/mysql/data

#临时目录 比如load data infile会用到
tmpdir  = /tmp

#设置socke文件所在目录
socket  = /tmp/mysql.sock

#主要用于MyISAM存储引擎,如果多台服务器连接一个数据库则建议注释下面内容
skip-external-locking

#只能用IP地址检查客户端的登录,不用主机名
skip_name_resolve = 1

#事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
transaction_isolation = READ-COMMITTED

#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4

#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci

#设置client连接mysql时的字符集,防止乱码
init_connect=‘SET NAMES utf8mb4‘

#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1

#最大连接数
max_connections = 400

#最大错误连接数
max_connect_errors = 1000

#TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
explicit_defaults_for_timestamp = true

#SQL数据包发送的大小,如果有BLOB对象建议修改成1G
max_allowed_packet = 128M

#MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
#MySQL默认的wait_timeout  值为8个小时, interactive_timeout参数需要同时配置才能生效
interactive_timeout = 1800
wait_timeout = 1800

#内部内存临时表的最大值 ,设置成128M。
#比如大数据量的group by ,order by时可能用到临时表,
#超过了这个值将写入磁盘,系统IO压力增大
tmp_table_size = 134217728
max_heap_table_size = 134217728

#禁用mysql的缓存查询结果集功能
#后期根据业务情况测试决定是否开启
#大部分情况下关闭下面两项
query_cache_size = 0
query_cache_type = 0

#####################用户进程分配到的内存设置BEGIN#############################

##每个session将会分配参数设置的内存大小
#用于表的顺序扫描,读出的数据暂存于read_buffer_size中,当buff满时或读完,将数据返回上层调用者
#一般在128kb ~ 256kb,用于MyISAM
#read_buffer_size = 131072
#用于表的随机读取,当按照一个非索引字段排序读取时会用到,
#一般在128kb ~ 256kb,用于MyISAM
#read_rnd_buffer_size = 262144
#order by或group by时用到

#建议先调整为2M,后期观察调整
sort_buffer_size = 2097152

#一般数据库中没什么大的事务,设成1~2M,默认32kb
binlog_cache_size = 524288

########################用户进程分配到的内存设置END############################

#在MySQL暂时停止响应新请求之前的短时间内多少个请求可以被存在堆栈中
#官方建议back_log = 50 + (max_connections / 5),封顶数为900
back_log = 130

############################日志设置##########################################

#数据库错误日志文件
log_error = error.log

#慢查询sql日志设置
slow_query_log = 1
slow_query_log_file = slow.log

#检查未使用到索引的sql
log_queries_not_using_indexes = 1

#针对log_queries_not_using_indexes开启后,记录慢sql的频次、每分钟记录的条数
log_throttle_queries_not_using_indexes = 5

#作为从库时生效,从库复制中如何有慢sql也将被记录
log_slow_slave_statements = 1

#慢查询执行的秒数,必须达到此值可被记录
long_query_time = 8

#检索的行数必须达到此值才可被记为慢查询
min_examined_row_limit = 100

#mysql binlog日志文件保存的过期时间,过期后自动删除
expire_logs_days = 5

############################主从复制设置#####################################

#开启mysql binlog功能
log-bin=mysql-bin

#binlog记录内容的方式,记录被操作的每一行
binlog_format = ROW

#对于binlog_format = ROW模式时,减少记录日志的内容,只记录受影响的列
binlog_row_image = minimal

#master status and connection information输出到表mysql.slave_master_info中
master_info_repository = TABLE

#the slave‘s position in the relay logs输出到表mysql.slave_relay_log_info中
relay_log_info_repository = TABLE

#作为从库时生效,想进行级联复制,则需要此参数
log_slave_updates

#作为从库时生效,中继日志relay-log可以自我修复
relay_log_recovery = 1

#作为从库时生效,主从复制时忽略的错误
slave_skip_errors = ddl_exist_errors

#####################redo log和binlog的关系设置BEGIN#########################

#(步骤1) prepare dml相关的SQL操作,然后将redo log buff中的缓存持久化到磁盘
#(步骤2)如果前面prepare成功,那么再继续将事务日志持久化到binlog
#(步骤3)如果前面成功,那么在redo log里面写上一个commit记录
#当innodb_flush_log_at_trx_commit和sync_binlog都为1时是最安全的,
#在mysqld服务崩溃或者服务器主机crash的情况下,binary log只有可能丢失最多一个语句或者一个事务。
#但是都设置为1时会导致频繁的io操作,因此该模式也是最慢的一种方式。
#当innodb_flush_log_at_trx_commit设置为0,mysqld进程的崩溃会导致上一秒钟所有事务数据的丢失。
#当innodb_flush_log_at_trx_commit设置为2,只有在操作系统崩溃或者系统掉电的情况下,上一秒钟所有事务数据才可能丢失。

#commit事务时,控制redo log buff持久化磁盘的模式 默认为1
innodb_flush_log_at_trx_commit = 2

#commit事务时,控制写入mysql binlog日志的模式 默认为0
#innodb_flush_log_at_trx_commit和sync_binlog都为1时,mysql最为安全但性能上压力也是最大
sync_binlog = 1

####################redo log和binlog的关系设置END############################

############################Innodb设置#####################################

#数据块的单位8k,默认是16k,16kCPU压力稍小,8k对select的吞吐量大
#innodb_page_size的参数值也影响最大索引长度,8k比16k的最大索引长度小
#innodb_page_size = 8192

#一般设置物理存储的60% ~ 70%
innodb_buffer_pool_size = 1G

#5.7.6之后默认16M
#innodb_log_buffer_size = 16777216

#该参数针对unix、linux,window上直接注释该参数.默认值为NULL
#O_DIRECT减少操作系统级别VFS的缓存和Innodb本身的buffer缓存之间的冲突
innodb_flush_method = O_DIRECT

#此格式支持压缩, 5.7.7之后为默认值
innodb_file_format = Barracuda

#CPU多核处理能力设置,假设CPU是2颗4核的,设置如下
#读多,写少可以设成2:6的比例
innodb_write_io_threads = 1
innodb_read_io_threads = 1

#提高刷新脏页数量和合并插入数量,改善磁盘I/O处理能力
#默认值200(单位:页)
#可根据磁盘近期的IOPS确定该值
innodb_io_capacity = 500

#为了获取被锁定的资源最大等待时间,默认50秒,超过该时间会报如下错误:
# ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
innodb_lock_wait_timeout = 30

#调整buffer pool中最近使用的页读取并dump的百分比,通过设置该参数可以减少转储的page数
innodb_buffer_pool_dump_pct = 40

#设置redoLog文件所在目录, redoLog记录事务具体操作内容
innodb_log_group_home_dir = /home/mysql/redolog/

#设置undoLog文件所在目录, undoLog用于事务回滚操作
innodb_undo_directory = /home/mysql/undolog/

#在innodb_log_group_home_dir中的redoLog文件数, redoLog文件内容是循环覆盖写入。
innodb_log_files_in_group = 3

#MySql5.7官方建议尽量设置的大些,可以接近innodb_buffer_pool_size的大小
#之前设置该值较大时可能导致mysql宕机恢复时间过长,现在恢复已经加快很多了
#该值减少脏数据刷新到磁盘的频次
#最大值innodb_log_file_size * innodb_log_files_in_group <= 512GB,单文件<=256GB
innodb_log_file_size = 1024M

#设置undoLog文件所占空间可以回收
#5.7之前的MySql的undoLog文件一直增大无法回收
innodb_undo_log_truncate = 1
innodb_undo_tablespaces = 0
innodb_undo_logs = 128

#5.7.7默认开启该参数 控制单列索引长度最大达到3072
#innodb_large_prefix = 1

#5.7.8默认为4个, Inodb后台清理工作的线程数
#innodb_purge_threads = 4

#通过设置配置参数innodb_thread_concurrency来限制并发线程的数量,
#一旦执行线程的数量达到这个限制,额外的线程在被放置到对队列中之前,会睡眠数微秒,
#可以通过设定参数innodb_thread_sleep_delay来配置睡眠时间
#该值默认为0,在官方doc上,对于innodb_thread_concurrency的使用,也给出了一些建议:
#(1)如果一个工作负载中,并发用户线程的数量小于64,建议设置innodb_thread_concurrency=0;
#(2)如果工作负载一直较为严重甚至偶尔达到顶峰,建议先设置innodb_thread_concurrency=128,
###并通过不断的降低这个参数,96, 80, 64等等,直到发现能够提供最佳性能的线程数
#innodb_thread_concurrency = 0

#强所有发生的死锁错误信息记录到error.log中,之前通过命令行只能查看最近一次死锁信息
innodb_print_all_deadlocks = 1

############################其他设置########################################

[mysqldump]
quick
max_allowed_packet = 128M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 256k
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
#增加每个进程的可打开文件数量
open-files-limit = 28192
#
#skip-grant-tables
EOF

cat >${installPath}/mysql/support-files/mysql.server<<EOF
#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: \$local_fs \$network \$remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: \$local_fs \$network \$remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO

# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=${installPath}/mysql
datadir=${installPath}/mysql/data

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900

# Lock directory for RedHat / SuSE.
lockdir='/var/lock/subsys'
lock_file_path="\$lockdir/mysql"

# The following variables are only set for letting mysql.server find things.

# Set some defaults
mysqld_pid_file_path=
if test -z "\$basedir"
then
  basedir=/usr/local/mysql
  bindir=/usr/local/mysql/bin
  if test -z "\$datadir"
  then
    datadir=/usr/local/mysql/data
  fi
  sbindir=/usr/local/mysql/bin
  libexecdir=/usr/local/mysql/bin
else
  bindir="\$basedir/bin"
  if test -z "\$datadir"
  then
    datadir="\$basedir/data"
  fi
  sbindir="\$basedir/sbin"
  libexecdir="\$basedir/libexec"
fi

# datadir_set is used to determine if datadir was set (and so should be
# *not* set inside of the --basedir= handler.)
datadir_set=

#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f \$lsb_functions ; then
  . \$lsb_functions
else
  log_success_msg()
  {
    echo " SUCCESS! \$@"
  }
  log_failure_msg()
  {
    echo " ERROR! \$@"
  }
fi

PATH="/sbin:/usr/sbin:/bin:/usr/bin:\$basedir/bin"
export PATH

mode=\$1    # start or stop

[ \$# -ge 1 ] && shift


other_args="\$*"   # uncommon, but needed when called from an RPM upgrade action
           # Expected: "--skip-networking --skip-grant-tables"
           # They are not checked here, intentionally, as it is the resposibility
           # of the "spec" file author to give correct arguments only.

case \`echo "testing\c"\`,\`echo -n testing\` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac

parse_server_arguments() {
  for arg do
    case "\$arg" in
      --basedir=*)  basedir=\`echo "\$arg" | sed -e 's/^[^=]*=//'\`
                    bindir="\$basedir/bin"
		    if test -z "\$datadir_set"; then
		      datadir="\$basedir/data"
		    fi
		    sbindir="\$basedir/sbin"
		    libexecdir="\$basedir/libexec"
        ;;
      --datadir=*)  datadir=\`echo "\$arg" | sed -e 's/^[^=]*=//'\`
		    datadir_set=1
	;;
      --pid-file=*) mysqld_pid_file_path=\`echo "\$arg" | sed -e 's/^[^=]*=//'\` ;;
      --service-startup-timeout=*) service_startup_timeout=\`echo "\$arg" | sed -e 's/^[^=]*=//'\` ;;
    esac
  done
}

wait_for_pid () {
  verb="\$1"           # created | removed
  pid="\$2"            # process ID of the program operating on the pid-file
  pid_file_path="\$3" # path to the PID file.

  i=0
  avoid_race_condition="by checking again"

  while test \$i -ne \$service_startup_timeout ; do

    case "\$verb" in
      'created')
        # wait for a PID-file to pop into existence.
        test -s "\$pid_file_path" && i='' && break
        ;;
      'removed')
        # wait for this PID-file to disappear
        test ! -s "\$pid_file_path" && i='' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
        exit 1
        ;;
    esac

    # if server isn't running, then pid-file will never be updated
    if test -n "\$pid"; then
      if kill -0 "\$pid" 2>/dev/null; then
        :  # the server still runs
      else
        # The server may have exited between the last pid-file check and now.
        if test -n "\$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there's nothing that will affect the file.
        log_failure_msg "The server quit without updating PID file (\$pid_file_path)."
        return 1  # not waiting any more.
      fi
    fi

    echo \$echo_n ".\$echo_c"
    i=\`expr \$i + 1\`
    sleep 1

  done

  if test -z "\$i" ; then
    log_success_msg
    return 0
  else
    log_failure_msg
    return 1
  fi
}

# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x "\$bindir/my_print_defaults";  then
  print_defaults="\$bindir/my_print_defaults"
else
  # Try to find basedir in /etc/my.cnf
  conf=/etc/my.cnf
  print_defaults=
  if test -r \$conf
  then
    subpat='^[^=]*basedir[^=]*=\(.*\)\$'
    dirs=\`sed -e "/\$subpat/!d" -e 's//\1/' \$conf\`
    for d in \$dirs
    do
      d=\`echo \$d | sed -e 's/[ 	]//g'\`
      if test -x "\$d/bin/my_print_defaults"
      then
        print_defaults="\$d/bin/my_print_defaults"
        break
      fi
    done
  fi

  # Hope it's in the PATH ... but I doubt it
  test -z "\$print_defaults" && print_defaults="my_print_defaults"
fi

#
# Read defaults file from 'basedir'.   If there is no defaults file there
# check if it's in the old (depricated) place (datadir) and read it from there
#

extra_args=""
if test -r "\$basedir/my.cnf"
then
  extra_args="-e \$basedir/my.cnf"
fi

parse_server_arguments \`\$print_defaults \$extra_args mysqld server mysql_server mysql.server\`

#
# Set pid file if not given
#
if test -z "\$mysqld_pid_file_path"
then
  mysqld_pid_file_path=\$datadir/\`hostname\`.pid
else
  case "\$mysqld_pid_file_path" in
    /* ) ;;
    * )  mysqld_pid_file_path="\$datadir/\$mysqld_pid_file_path" ;;
  esac
fi

case "\$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd \$basedir

    echo \$echo_n "Starting MySQL"
    if test -x \$bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      \$bindir/mysqld_safe --datadir="\$datadir" --pid-file="\$mysqld_pid_file_path" \$other_args >/dev/null &
      wait_for_pid created "\$!" "\$mysqld_pid_file_path"; return_value=\$?

      # Make lock for RedHat / SuSE
      if test -w "\$lockdir"
      then
        touch "\$lock_file_path"
      fi

      exit \$return_value
    else
      log_failure_msg "Couldn't find MySQL server (\$bindir/mysqld_safe)"
    fi
    ;;

  'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.

    if test -s "\$mysqld_pid_file_path"
    then
      # signal mysqld_safe that it needs to stop
      touch "\$mysqld_pid_file_path.shutdown"

      mysqld_pid=\`cat "\$mysqld_pid_file_path"\`

      if (kill -0 \$mysqld_pid 2>/dev/null)
      then
        echo \$echo_n "Shutting down MySQL"
        kill \$mysqld_pid
        # mysqld should remove the pid file when it exits, so wait for it.
        wait_for_pid removed "\$mysqld_pid" "\$mysqld_pid_file_path"; return_value=\$?
      else
        log_failure_msg "MySQL server process #\$mysqld_pid is not running!"
        rm "\$mysqld_pid_file_path"
      fi

      # Delete lock for RedHat / SuSE
      if test -f "\$lock_file_path"
      then
        rm -f "\$lock_file_path"
      fi
      exit \$return_value
    else
      log_failure_msg "MySQL server PID file could not be found!"
    fi
    ;;

  'restart')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    if \$0 stop  \$other_args; then
      \$0 start \$other_args
    else
      log_failure_msg "Failed to stop running server, so refusing to try to start."
      exit 1
    fi
    ;;

  'reload'|'force-reload')
    if test -s "\$mysqld_pid_file_path" ; then
      read mysqld_pid <  "\$mysqld_pid_file_path"
      kill -HUP \$mysqld_pid && log_success_msg "Reloading service MySQL"
      touch "\$mysqld_pid_file_path"
    else
      log_failure_msg "MySQL PID file could not be found!"
      exit 1
    fi
    ;;
  'status')
    # First, check to see if pid file exists
    if test -s "\$mysqld_pid_file_path" ; then
      read mysqld_pid < "\$mysqld_pid_file_path"
      if kill -0 \$mysqld_pid 2>/dev/null ; then
        log_success_msg "MySQL running (\$mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL is not running, but PID file exists"
        exit 1
      fi
    else
      # Try to find appropriate mysqld process
      mysqld_pid=\`pidof \$libexecdir/mysqld\`

      # test if multiple pids exist
      pid_count=\`echo \$mysqld_pid | wc -w\`
      if test \$pid_count -gt 1 ; then
        log_failure_msg "Multiple MySQL running but PID file could not be found (\$mysqld_pid)"
        exit 5
      elif test -z \$mysqld_pid ; then
        if test -f "\$lock_file_path" ; then
          log_failure_msg "MySQL is not running, but lock file (\$lock_file_path) exists"
          exit 2
        fi
        log_failure_msg "MySQL is not running"
        exit 3
      else
        log_failure_msg "MySQL is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      # usage
      basename=\`basename "\$0"\`
      echo "Usage: \$basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
      exit 1
    ;;
esac

exit 0

EOF

cp ${installPath}/mysql/support-files/mysql.server /etc/init.d/mysql
}
updateCnf

createFolder()
{
cd ${installPath}/mysql/
mkdir undolog redolog
}
createFolder

#初始化数据库
InitDataBase()
{
#加权
chown -R mysql .
chgrp -R mysql .
chown -R mysql ${installPath}/mysql/data/
chmod -R 777 ${installPath}//mysql/
chmod -R 777 ${installPath}/
${installPath}/mysql/bin/mysqld --initialize --user=mysql --basedir=${installPath}/mysql --datadir=${installPath}/mysql/data
${installPath}/mysql/bin/mysql_ssl_rsa_setup  --datadir=${installPath}/mysql/data/
#创建软连接
/etc/init.d/mysql start
ln -s /home/mysql/bin/mysql /usr/bin
}
InitDataBase

#重置密码
ResetPwd()
{
#从日志中获取mysql初始密码
pwd=`grep "A temporary password is generated for root@localhost: " ${installPath}/mysql/data/error.log`
pwd=${pwd##*root@localhost:}
#防止因为初始密码中有特殊字符出错 拼接单引号
pwd=${pwd// /}
echo ${pwd}
mysql -uroot -p${pwd} --connect-expired-password  -e "alter user 'root'@'localhost' identified by   '${defaultPwd}';"
}
ResetPwd

#设置开机自启动
AutomaticStartup(){
#添加系统路径/etc/profile
export PATH=/home/mysql/bin:$PATH
#使环境变量生效
source /etc/profile
#配置mysql自动启动
 chmod 755 /etc/init.d/mysql
 chkconfig --add mysql
 chkconfig --level 345 mysql on
#查看mysql状态
service mysql status
#开放mysql端口
firewall-cmd --zone=public --add-port=${mysqlPort}/tcp --permanent
}
AutomaticStartup

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值