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 <