shell脚本部署mysql

本文介绍了如何通过shell脚本在CentOS Linux系统上进行MySQL的部署,包括准备MySQL软件包,编写部署脚本,执行脚本以及最后的部署验证步骤。
摘要由CSDN通过智能技术生成

shell脚本部署mysql

准备工作


下载mysql的软件包

[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
data  mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

编写脚本


[root@localhost ~]# vi yhm.sh
[root@localhost ~]# cat yhm.sh
#!/bin/bash
#关闭防火墙和selinux
echo "正在关闭防火墙和selinux"
systemctl stop firewalld
systemctl disable firewalld &> /dev/null
setenforce 0

#安装yum源
echo "正在安装yum源"
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &> /dev/null
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm &> /dev/null
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
echo "yum源安装完成"

echo "---正在部署mysql---"
#创建mysql用户
echo "正在验证mysql用户..."
id mysql &> /dev/null
if [ $? -eq 0 ];then
	echo "mysql用户已存在"
else
	echo "正在创建mysql用户..."
	useradd -r -M -s /sbin/nologin mysql
	echo "mysql用户创建成功"
fi

#安装依赖包
echo "正在安装依赖包.."
yum -y install libncurses* &> /dev/null
echo "安装完成"

#配置mysql
echo "正在配置mysql..."
tar -xzf /opt/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
ln -sv /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql &> /dev/null
chown -R mysql.mysql /usr/local/mysql &> /dev/null
mkdir -p /opt/data &> /dev/null
chown -R mysql.mysql /opt/data/ &> /dev/null

#数据库初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ &> /dev/null
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql &> /dev/null
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf &> /dev/null
ldconfig &> /dev/null
ln -s /usr/local/mysql/bin/mysql /usr/bin

#编辑mysql配置文件
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /usr/local/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1/opt/data#g" /usr/local/mysql/support-files/mysql.server

#定义mysql服务可以使用systemd来进行管理
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.targe

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && echo "*****重新加载成功*****"
systemctl enable --now mysqld && echo "*****开机自启mysql成功*****"

#设置数据库密码
read -p "请输入您要设置的数据库密码:" yhm
/usr/local/mysql/bin/mysql -uroot -e "set password=password('$yhm')"

#做一个链接,添加mysq命令的变量到环境变量
echo "export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" >> /etc/profile
source /etc/profile
echo "-----mysql部署完成-----"

执行脚本


[root@localhost ~]# chmod +x yhm.sh
[root@localhost ~]# ./yhm.sh 
正在关闭防火墙和selinux
正在安装yum源
yum源安装完成
---正在部署mysql---
正在验证mysql用户...
mysql用户已存在
正在安装依赖包..
安装完成
正在配置mysql...
*****重新加载成功*****
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
*****开机自启mysql成功*****
请输入您要设置的数据库密码:yhm
-----mysql部署完成-----

验证


[root@localhost ~]# mysql -uroot -pyhm
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值