学过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