使用shell脚本一键安装MySQL

学过MySQL数据库的安装之后,会发现不论是哪种方式安装都会比较麻烦,因此,为了以后在安装部署MySQL的时候能稍微方便一些,这里把MySQL的二进制安装写入shell脚本,之后便能直接一件执行安装。

[root@localhost ~]# vim mysql_install.sh
#!/bin/bash
. /etc/init.d/functions
install() {
if [ -a /application ] &> /dev/null;then
action "application目录已存在" false
else
mkdir /application
action "application目录创建成功"
fi
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz &> /dev/null && action "mysql二进制包下载成功" || { action "mysql二进制包下载失败,请检测包或网络配置" false;exit; }
tar xzvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz &> /dev/null && action "文件解压成功"
mv mysql-5.6.40-linux-glibc2.12-x86_64 /application/mysql-5.6.40
ln -s /application/mysql-5.6.40 /application/mysql
cd /application/mysql/support-files
cp my-default.cnf /etc/my.cnf &>/dev/null
cp mysql.server /etc/init.d/mysqld &>/dev/null
cd /application/mysql/scripts
if id mysql &> /dev/null;then
     action "mysql用户已经存在" false
else
     useradd mysql -s /sbin/nologin -M
     action "mysql用户创建成功"
fi
yum -y install autoconf &> /dev/null
/application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --data=/application/mysql/data  &>/dev/null && action "初始化成功" || { action "初始化失败,请检测初始化配置" false;exit;}
echo 'export PATH="/application/mysql/bin:$PATH"' > /etc/profile.d/mysql.sh
source /etc/profile && action "环境变量添加成功" || { action "环境变量添加失败" false;exit; }
sed -i 's#/usr/local#/application#g' /etc/init.d/mysqld /application/mysql/bin/mysqld_safe
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
cat >> /etc/my.cnf <<EOF
basedir = /application/mysql/
datadir = /application/mysql/data
EOF
systemctl daemon-reload
/etc/init.d/mysqld start  &> /dev/null && action "mysql服务启动成功" || { action "mysql服务启动失败" false;exit; }
systemctl enable  mysqld
/application/mysql/bin/mysqladmin -uroot password '123456' && action "mysql密码设置成功" || { action "mysql密码设置失败" false;exit; }
/application/mysql/bin/mysql -uroot -p123456
}
install
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值