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

相关推荐
恒辉信达5 分钟前
hhdb客户端介绍(53)
数据库·mysql·hhdb·数据库可视化界面客户端
指尖上跳动的旋律2 小时前
shell脚本定义特殊字符导致执行mysql文件错误的问题
数据库·mysql
一勺菠萝丶2 小时前
MongoDB 常用操作指南(Docker 环境下)
数据库·mongodb·docker
m0_748244833 小时前
StarRocks 排查单副本表
大数据·数据库·python
C++忠实粉丝3 小时前
Redis 介绍和安装
数据库·redis·缓存
wmd131643067123 小时前
将微信配置信息存到数据库并进行调用
数据库·微信
是阿建吖!3 小时前
【Linux】基础IO(磁盘文件)
linux·服务器·数据库
凡人的AI工具箱4 小时前
每天40分玩转Django:Django国际化
数据库·人工智能·后端·python·django·sqlite
ClouGence4 小时前
Redis 到 Redis 数据迁移同步
数据库·redis·缓存
m0_748236584 小时前
《Web 应用项目开发:从构思到上线的全过程》
服务器·前端·数据库