centos7/redhat7 自动化mysql安装shell脚本 数据库shell脚本

#!/bin/bash
# 将mysql压缩包传到/tmp下(根据实际情况修改变量参数mysql_pakg、mysql_datadir、mysql_new_psd)
mysql_pakg=mysql-5.7.38-el7-x86_64.tar.gz
mysql_datadir=/data
mysql_port=3306
mysql_new_psd="db#ER1258"
mysql_server_id=1

if [ -d ${mysql_datadir} ];then
  echo "`date "+%Y-%m-%d %H:%M:%S"` 开始安装Mysql"
else
  echo "`date "+%Y-%m-%d %H:%M:%S"` 开始安装Mysql"
  mkdir -p ${mysql_datadir}
fi

rpm -qa | grep libaio > /dev/null 2>&1
if [ $? -eq 0 ];then
  echo "`date "+%Y-%m-%d %H:%M:%S"` 依赖包libaio已安装"
else
  echo "`date "+%Y-%m-%d %H:%M:%S"` 依赖包libaio未安装,正在安装中,请稍后...."
  yum -y install libaio > /dev/null 2>&1
fi

echo "`date "+%Y-%m-%d %H:%M:%S"` 正在解压Mysql压缩包...."
tar xvf /tmp/${mysql_pakg} -C ${mysql_datadir} > /dev/null 2>&1
find ${mysql_datadir} -name "mysql*x86_64" -type d | while read mysql_dir
do 
  mv ${mysql_dir} ${mysql_datadir}/mysql
done

if [ -d ${mysql_datadir}/mysqldata ];then
  echo "`date "+%Y-%m-%d %H:%M:%S"` ${mysql_datadir}/mysqldata已存在,请先确认mysqldata目录"
  exit 0
else
  mkdir -p ${mysql_datadir}/mysqldata
fi

# 创建mysql组及mysql用户,并赋权
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql ${mysql_datadir}/mysql
chown -R mysql:mysql ${mysql_datadir}/mysqldata

# 备份/etc/my.cnf
cp /etc/my.cnf /etc/my.cnf.bak

# 修改mysql配置文件
cat << EOF > /etc/my.cnf
[mysqld]
basedir=${mysql_datadir}/mysql
datadir=${mysql_datadir}/mysqldata
port = ${mysql_port}
socket=${mysql_datadir}/mysql/mysql.sock

symbolic-links=0
log-error=/var/log/mysql/mysqld.log
pid-file=${mysql_datadir}/mysql/mysqld/mysqld.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
log-bin=mysql-bin
binlog-format=ROW
server_id=${mysql_server_id}
max_connections=1500
lower_case_table_names=1
init_connect='SET collation_connection = utf8mb4_general_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
skip-character-set-client-handshake
EOF

# 创建mysql所需文件
cd ${mysql_datadir}/mysql
touch mysql.sock
chown mysql:mysql mysql.sock
chmod 755 mysql.sock

cd ${mysql_datadir}/mysql
mkdir mysqld
cd mysqld
touch mysqld.pid
cd ..
chown -R mysql:mysql mysqld
cd mysqld
chmod 755 mysqld.pid
 
mkdir /var/log/mysql
touch /var/log/mysql/mysqld.log
chown -R mysql:mysql /var/log/mysql
cd /var/log/mysql
chmod 755 mysqld.log

# 初始化数据库
${mysql_datadir}/mysql/bin/mysqld --initialize --user=mysql --basedir=${mysql_datadir}/mysql --datadir=${mysql_datadir}/mysqldata
nohup ${mysql_datadir}/mysql/bin/mysqld_safe --user=mysql > /dev/null 2>&1 &
sleep 3
ln -s ${mysql_datadir}/mysql/mysql.sock /tmp
# 查询mysql临时密码
mysql_tmp_psd="`cat /var/log/mysql/mysqld.log | grep -i "temporary password" | awk -F ' ' '{print $11}'`"
# 登录mysql并修改密码
${mysql_datadir}/mysql/bin/mysql -uroot -p"${mysql_tmp_psd}" --connect-expired-password -S /tmp/mysql.sock -e "set password=password('${mysql_new_psd}');" > /dev/null 2>&1
cat << EOF > /tmp/change_mysql_psd.sql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${mysql_new_psd}' WITH GRANT OPTION;
ALTER USER 'root'@'%' IDENTIFIED BY '${mysql_new_psd}';
FLUSH PRIVILEGES;
EOF
${mysql_datadir}/mysql/bin/mysql -uroot -p"${mysql_new_psd}" < /tmp/change_mysql_psd.sql > /dev/null 2>&1 &

# 设置开机自启动
cp -a ${mysql_datadir}/mysql/support-files/mysql.server  /etc/init.d/mysql
chkconfig --add mysql
ln -s ${mysql_datadir}/mysql/bin/mysql /usr/bin
echo "`date "+%Y-%m-%d %H:%M:%S"` `service mysql restart`"
if [ $? -eq 0 ];then
  echo "`date "+%Y-%m-%d %H:%M:%S"` Mysql启动成功,当前状态为:`service mysql status`"
else
  echo "`date "+%Y-%m-%d %H:%M:%S"` Mysql安装有误,请检查!"
  exit 1
fi

你可能感兴趣的:(Linux软件服务相关,mysql,数据库,自动化,linux,centos)