MySQL8 一键部署

#!/bin/bash

定义变量

mysql_download_url=https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz

mysql_package_name=mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz

mysql_dec_name=mysql-8.0.33-linux-glibc2.12-x86_64

mysql_download_dir="/usr/local/src"

mysql_base_dir="/usr/local/mysql"

mysql_data_dir="/data/mysql"

mysql_root_password=!@#123QAz

ck_ok()

{

if [ $? -ne 0 ]

then

echo "$1 error."

exit 1

fi

}

dependon_install()

{

if which yum >/dev/null 2>&1

then

for pkg in "$@"

do

if ! rpm -q ${pkg} >/dev/null 2>&1

then

sudo yum install -y ${pkg}

else

echo "${pkg}已安装"

fi

done

fi

if which apt >/dev/mull 2>&1

then

for pkg in "$@"

do

if ! dpkg -l ${pkg} >/dev/null 2>&1

then

sudo apt install -y ${pkg}

else

echo "${pkg}已安装"

fi

done

fi

}

download_mysql()

{

cd ${mysql_download_dir}

if [ -f ${mysql_package_name} ]

then

echo "当前目录已经存在mysql安装包"

echo "检测MD5"

file_md5=`md5sum {mysql_package_name} \| awk '{print 1}'`

if [ ${file_md5} == '0bb9fd978d8b122d7846efc37884c0bb' ]

then

return 0

else

echo "file ${mysql_package_name} check failed"

/bin/mv -f {mysql_package_name} {mysql_package_name}.old

fi

fi

dependon_install wget

ck_ok "wget安装"

sudo wget ${mysql_download_url}

ck_ok "下载mysql"

}

install_mysql()

{

cd /usr/local

if [ -d ${mysql_base_dir} ]

then

echo "${mysql_base_dir}已存在,移走"

sudo /bin/mv -f {mysql_base_dir} {mysql_base_dir}.old

fi

sudo tar Jxf {mysql_download_dir}/{mysql_package_name}

ck_ok "解压mysql压缩包"

sudo /bin/mv -f ${mysql_dec_name} mysql

if id mysql &>/dev/null

then

echo "系统已经创建了mysql用户,跳过创建"

else

echo "创建mysql用户"

sudo useradd -s /sbin/nologin mysql

fi

ck_ok "创建mysql用户"

if [ -d ${mysql_data_dir} ]

then

echo "${mysql_data_dir}已存在,移走"

sudo /bin/mv -f {mysql_data_dir} {mysql_data_dir}.old

fi

echo "创建MySQL data_dir"

sudo mkdir -p ${mysql_data_dir}

sudo chown -R mysql ${mysql_data_dir}

if [ -f ${mysql_base_dir}/my.cnf ]

then

echo "MySQL配置文件已存在,移走"

sudo /bin/mv -f {mysql_base_dir}/my.cnf {mysql_base_dir}/my.cnf.old

fi

echo "创建MySQL配置文件my.cnf"

cat > /tmp/my.cnf <<EOF

mysqld

user = mysql

port = 3306

server_id = 1

basedir = ${mysql_base_dir}

datadir = ${mysql_data_dir}

socket = /tmp/mysql.sock

pid-file= ${mysql_data_dir}/mysqld.pid

log-error = ${mysql_data_dir}/mysql.err

EOF

sudo /bin/mv /tmp/my.cnf ${mysql_base_dir}/my.cnf

dependon_install ncurses-compat-libs libaio-devel

sudo {mysql_base_dir}/bin/mysqld --console --datadir={mysql_data_dir} --initialize-insecure --user=mysql

ck_ok "初始化"

if [ -f /usr/lib/systemd/system/mysqld.service ]

then

echo "mysql服务启动脚本已存在,移走"

sudo /bin/mv -f /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/mysqld.service.old

fi

echo "创建mysql服务启动脚本"

cat > /tmp/mysqld.service <<EOF

Unit

Description=MySQL server

After=network.target

Install

WantedBy=multi-user.target

Service

Type=forking

TimeoutSec=0

PermissionsStartOnly=true

ExecStart={mysql_base_dir}/bin/mysqld --defaults-file={mysql_base_dir}/my.cnf --daemonize $OPTIONS

ExecReload=/bin/kill -HUP -$MAINPID

ExecStop=/bin/kill -QUIT $MAINPID

KillMode=process

LimitNOFILE=65535

Restart=on-failure

RestartSec=10

RestartPreventExitStatus=1

PrivateTmp=false

EOF

sudo /bin/mv /tmp/mysqld.service /usr/lib/systemd/system/mysqld.service

sudo systemctl unmask mysqld

sudo systemctl daemon-reload

sudo systemctl enable mysqld

sudo systemctl start mysqld

ck_ok "启动mysql"

{mysql_base_dir}/bin/mysqladmin -S/tmp/mysql.sock -uroot password "{mysql_root_password}"

ck_ok "设置mysql密码"

unlink /usr/bin/mysql &>/dev/null

ln -s /usr/local/mysql/bin/mysql /usr/bin

}

main()

{

download_mysql

install_mysql

}

main

相关推荐
程序猿零零漆10 分钟前
【金仓数据库征文】金仓数据库:国产化浪潮下的技术突破与行业实践
数据库·金仓数据库 2025 征文·数据库平替用金仓
浩浩测试一下22 分钟前
SQL注入高级绕过手法汇总 重点
数据库·sql·安全·web安全·网络安全·oracle·安全架构
Pasregret1 小时前
缓存与数据库一致性深度解析与解决方案
数据库·缓存·wpf
skywalk81631 小时前
Graph Database Self-Managed Neo4j 知识图谱存储实践2:通过官方新手例子入门(未完成)
数据库·知识图谱·neo4j
Lucky GGBond1 小时前
MySQL 报错解析:SQLSyntaxErrorException caused by extra comma before FROM
数据库·mysql
Claudio1 小时前
【MySQL】联合索引和覆盖索引(索引失效的误区讲解+案例分析)
数据库
纪元A梦2 小时前
Redis最佳实践——性能优化技巧之监控与告警详解
数据库·redis·性能优化
GarfieldFine2 小时前
MySQL索引使用一定有效吗?如何排查索引效果?
数据库·mysql
cypking2 小时前
mysql 安装
数据库·mysql·adb
一个数据大开发2 小时前
解读《数据资产质量评估实施规则》:企业数据资产认证落地的关键指南
大数据·数据库·人工智能