栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

shell脚本多实例部署MySQL

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

shell脚本多实例部署MySQL

shell脚本多实例部署MySQL
  • 1. 创建一个目录,用来存放脚本和安装包
  • 2. 下载好对应的安装包
  • 3. 编写脚本
  • 4. 验证效果

1. 创建一个目录,用来存放脚本和安装包
[root@localhost opt]# mkdir mysql
[root@localhost opt]# cd mysql/
[root@localhost mysql]# mkdir packages
[root@localhost mysql]# touch install.sh
[root@localhost mysql]# ls
install.sh  packages
[root@localhost mysql]# 

2. 下载好对应的安装包
[root@localhost mysql]# ls packages/
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

3. 编写脚本
[root@localhost mysql]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "Please execute this script uses root user."
    exit 250
fi

install_dir=/usr/local
data_dir=/opt/data

read -p "请输入要创建的实例的个数: " instance_count
if [ -z $instance_count ];then
    instance_count=1
fi
echo $instance_count | grep -E "^[0-9]+$" &>/dev/null
if [ $? -ne 0 ];then
    instance_count=1
fi

read -p "请输入您要使用的数据库密码: " password
if [ -z $password ];then
    password=1
fi

yum -y install ncurses-compat-libs perl

id mysql &>/dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

if [ ! -d $install_dir/mysql-5.7.34-linux-glibc2.12-x86_64 ] && [ ! -d $install_dir/mysql ];then
    tar xf packages/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C $install_dir
    ln -s $install_dir/mysql-5.7.34-linux-glibc2.12-x86_64 $install_dir/mysql
fi

chown -R mysql.mysql $install_dir/mysql*

echo "export PATH=$install_dir/mysql/bin:$PATH" > /etc/profile.d/mysql.sh


function single(){
    if [ ! -d $data_dir ];then
        mkdir -p $data_dir
    fi
    chown -R mysql.mysql $data_dir
    content=$(ls $data_dir | wc -l)
    if [ $content -eq 0 ];then
        $install_dir/mysql/bin/mysqld --initialize-insecure --user mysql --datadir=$data_dir
    fi
    cat > /etc/my.cnf < /usr/lib/systemd/system/mysqld.service < /etc/my.cnf <> /etc/my.cnf < /usr/lib/systemd/system/mysqld.service < 
4. 验证效果 
[root@localhost mysql]# bash -x install.sh 
+ '[' 0 -ne 0 ']'
+ install_dir=/usr/local
+ data_dir=/opt/data
+ read -p '请输入要创建的实例的个数: ' instance_count
请输入要创建的实例的个数: 5
+ '[' -z 5 ']'
+ echo 5
+ grep -E '^[0-9]+$'
+ '[' 0 -ne 0 ']'
+ read -p '请输入您要使用的数据库密码: ' password
请输入您要使用的数据库密码: 
+ '[' -z ']'
+ password=1
+ yum -y install ncurses-compat-libs perl
过程略……

[root@localhost mysql]# ss -antl
State        Recv-Q       Send-Q             Local Address:Port             Peer Address:Port       
LISTEN       0            128                      0.0.0.0:22                    0.0.0.0:*          
LISTEN       0            128                         [::]:22                       [::]:*          
LISTEN       0            80                             *:3306                        *:*          
LISTEN       0            80                             *:3307                        *:*          
LISTEN       0            80                             *:3308                        *:*          
LISTEN       0            80                             *:3309                        *:*          
LISTEN       0            80                             *:3310                        *:*          
[root@localhost mysql]# 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/334511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号