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
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!