Linux 使用shell脚本部署mysql

Linux 使用shell脚本部署mysql

编写脚本

[root@100 ~]# vim mysql.sh
#/bin/bash

#关闭防火墙和selinux
echo "正在关闭防火墙和selinux"
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
echo "防火墙关闭"
setenforce 0
echo "selinux关闭"

#配置yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &>/dev/null
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
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源配置完毕"
yum clean all &>/dev/null
yum makecache &>/dev/null
echo "缓存已清理"

#安装依赖
yum -y install ncurses-devel openssl-devel openssl cmake ncurses-compat-libs  &>/dev/null
echo "依赖安装完毕"

#将mysql二进制包上传后解压
echo "解压mysql二进制包"
cd /opt
tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz &>/dev/null
echo "解压完成"

#创建mysql用户
echo "创建mysql用户中"
id mysql &>/dev/null
if [ $? -eq 0 ];then
        echo "该用户已存在"
else
        useradd -M -r -s /sbin/nologin mysql &>/dev/null
        echo "用户创建完成"
fi

#创建软连接
echo "创建软链接"
ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
echo "创建完成"

#修改属主和属组
chown -R mysql.mysql mysql
chown -R mysql.mysql mysql-5.7.38-linux-glibc2.12-x86_64/

#配置环境变量
echo "配置环境变量中"
echo 'export PATH=$PATH:/opt/mysql/bin' >/etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
ln -s /opt/mysql/include  /usr/include/mysql
echo '/opt/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
cat >> /etc/man_db.conf << EOF
MANDATORY_MANPATH                       /opt/mysql/man
EOF
echo "环境变量配置完成"

#创建数据存放目录
mkdir /opt/mysql_data
chown -R mysql.mysql /opt/mysql_data

#初始化数据库
mysqld --initialize-insecure --user=mysql --datadir=/opt/mysql_data/  &>/dev/null
echo "数据库初始化完成"

#生成配置文件
echo "生成配置文件中"
cat > /etc/my.cnf << EOF
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
log-error=/var/log/mysqld.log
user = mysql
skip-name-resolve
EOF
echo "配置文件生成完毕"

#配置服务启动脚本
cp -a /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
cat >> /etc/init.d/mysqld < /usr/lib/systemd/system/mysqld.service << EOF
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
echo "服务启动脚本配置完成"
systemctl daemon-reload
systemctl start mysqld
systemctl enable mysqld &>/dev/null

#修改数据库登录密码
read -p "请修改数据库登录密码:" sql_passwd
mysql -uroot -e "set password = password('"$sql_passwd"')"
echo "登录密码修改成功"

运行脚本

[root@100 ~]# chmod +x mysql.sh
[root@100 ~]# ./mysql.sh
正在关闭防火墙和selinux
防火墙关闭
selinux关闭
yum源配置完毕
缓存已清理
依赖安装完毕
解压mysql二进制包
解压完成
创建mysql用户中
用户创建完成
创建软链接
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
创建完成
配置环境变量中
环境变量配置完成
数据库初始化完成
生成配置文件中
配置文件生成完毕
服务启动脚本配置完成
请修改数据库登录密码:123456
登录密码修改成功

[root@100 ~]# mysql -uroot -p123456
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 4
Server version: 5.7.38 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>

你可能感兴趣的:(linux,mysql,centos)