概述
shell脚本部署mysql
文章目录
- 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
最后
以上就是懦弱水池为你收集整理的shell脚本部署mysqlshell脚本部署mysql的全部内容,希望文章能够帮你解决shell脚本部署mysqlshell脚本部署mysql所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复