一、使用源
#/bin/bash
#date:2020-05-03
systemctl stop firewalld #关闭防火墙
systemctl disable firewalld
systemctl status firewalld &> /dev/null
if [ $? -ne 0 ];then
echo "防火墙已关闭"
fi
setenforce 0 #关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
check_selinux=`getenforce`
if [[ $check_selinux -eq Permissive || $check_selinux -eq Disabled ]];then
echo "selinux已关闭"
fi
ping -c1 -w1 www.baidu.com &> /dev/null #测试网络通不通
if [ $? -ne 0 ];then
echo "网络不通"
exit
fi
echo "安装常用软件"
yum install -y lrzsz sysstat elinks wget net-tools bash-completion vim #此步是安装常用软件,若不需要可以将其注释掉
yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm #在官网找的下载源的
echo -e "\033[5;31m警告,此脚本安装mysql5.7,如要安装其他版本,请^C退出脚本安装并手动更改mysql源的enabled\033[0m"
sleep 10
sed -i '21c enabled=1' /etc/yum.repos.d/mysql-community.repo #此处是打开5.7的enabled
sed -i '28c enabled=0' /etc/yum.repos.d/mysql-community.repo #关闭8.0的enabled
yum clean all
yum makecache fast
yum -y remove mariadb-libs.x86_64 #由于centos7自带mariadb,所以先移除以免安装mysqld时报错
yum -y install mysql-community-server #安装mysql
systemctl start mysqld
if [ $? -eq 0 ];then
mysqladmin -uroot -p`grep 'password' /var/log/mysqld.log | awk -F':' '{print $4}'|awk '{print $1}'` password "QianFeng@123" #初始密码在/var/log/mysqld.log中,使用grep过滤出更改即可
echo "更改密码成功,新密码为QianFeng@123"
else
echo "mysql启动失败,请手动检查"
fi
若想下载其他版本,查看安装mysql的源文件,把打开5.7的enabled处代码的行号改成要安装版本对应的行号即可
二、使用包
#!/usr/n/bash
version=5.7
systemctl stop firewalld #关闭防火墙
systemctl disable firewalld
systemctl status firewalld &> /dev/null
if [ $? -ne 0 ];then
echo "防火墙已关闭"
fi
setenforce 0 #关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
check_selinux=`getenforce`
if [[ $check_selinux -eq Permissive || $check_selinux -eq Disabled ]];then
echo "selinux已关闭"
fi
ping -c1 -w1 www.baidu.com &> /dev/null #测试网络是否通
if [ $? -ne 0 ];then
echo "网络不通"
exit
fi
echo "安装常用软件"
yum install -y lrzsz sysstat elinks wget net-tools bash-completion vim #安装常用软件,若不需要注释掉即可
echo -e "\033[5;31m警告,此脚本安装mysql5.7,如想安装其他版本,请^C退出脚本安装并更改脚本的version即可\033[0m"
sleep 10
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-$version/mysql-community-server-$version.28-1.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-$version/mysql-community-client-$version.28-1.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-$version/mysql-community-common-$version.28-1.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-$version/mysql-community-libs-$version.28-1.el7.x86_64.rpm
yum install -y net-tools.x86_64 libaio.x86_64 perl.x86_64
yum -y remove mariadb-libs.x86_64
yum -y install mysql-community* #安装mysql
systemctl start mysqld
if [ $? -eq 0 ];then
mysqladmin -uroot -p$(grep 'password' /var/log/mysqld.log | awk -F':' '{print $4}'|awk '{print $1}') password "QianFeng@123" #更改密码,新密码自定义,此处为QianFeng@123
echo "更改密码成功,新密码为QianFeng@123"
else
echo "mysql启动失败,请手动检查"
fi
若想安装其他版本,将version变量改成想要安装的版本号即可,前提是改完之后在网上可找到要下载的包