Centos安装MySQL5.7


根据操作系统版本安装MySQL5.7版本的脚本文件

#!/bin/sh
MYSQL_PORT=3306
#根据系统版本配置Mysql 安装源
VER=`cat /etc/*release | egrep ^VERSION= | awk -F '[". ]+' '{print $2}'` 
case $VER in
    7 )
        rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
            ;;
    8 )
        rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
        yum module disable mysql -y
        ;;
    * )
        echo "Unable to determine the OS version"
        exit 1
        ;;
esac

#禁用mysql8.0的repo并启用 5.7的repo
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
#禁用gpg check
sed -i '/gpgcheck=/c\gpgcheck=0' /etc/yum.repos.d/mysql-community.repo
#安装mysql5.7
yum --enablerepo=mysql57-community install mysql-community-server -y
#启动mysql服务
systemctl start mysqld
#设置为自启动
systemctl enable mysqld
#获取mysql的root用户的临时密码
temp_pass=`grep "A temporary password" /var/log/mysqld.log | awk '{ print $NF }'`
#需要设置的正式密码
dbpass='123456'
#登录数据库后执行下面的操作
#修改密码最小长度为6
#设置密码的复杂度为0
#注意生产环境不建议执行以上两步
#修改密码并允许root用户远程连接
#刷新权限
mysql -u root -p$temp_pass -e "
set global validate_password_length=6;
set global validate_password_policy=LOW;
ALTER USER 'root'@'localhost' IDENTIFIED BY '$dbpass';
use mysql;
update user set host='%' where user='root' and host='localhost';
create user 'replicate'@'%' identified by 'replicate';
grant replication slave on *.* to 'replicate'@'%';
ALTER USER 'replicate'@'%' IDENTIFIED WITH mysql_native_password BY 'replicate';
flush privileges;
" --connect-expired-password
#修改mysql参数
sed -i '/^\[mysqld\]/a\sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' /etc/my.cnf
sed -i '/^\[mysqld\]/a\table_open_cache=2048' /etc/my.cnf
sed -i '/^\[mysqld\]/a\max_connections=2048' /etc/my.cnf
sed -i '/^\[mysqld\]/a\max_connect_errors=2048' /etc/my.cnf
sed -i '/^\[mysqld\]/a\default-time-zone='\''+8:00'\' /etc/my.cnf
#改变MySQL监听端口
if  grep -vq '^[1-9][0-9]*$' <<< "$MYSQL_PORT" ;then
  MYSQL_PORT=3306
fi
if [[ "$MYSQL_PORT" != "3306" ]] && [[ "$MYSQL_PORT" -lt "65535" ]]; then
    sed -i '/^\[mysqld\]/a\port='"$MYSQL_PORT"'' /etc/my.cnf
fi
systemctl restart mysqld
#配置防火墙允许连接MySQL端口
PORT_ALLOW=`firewall-cmd --list-all | egrep '^ {1,}ports:' | grep -c ${MYSQL_PORT}/tcp`
if [[ "$PORT_ALLOW" = "0" ]] ; then
  firewall-cmd --zone=public --add-port=${MYSQL_PORT}/tcp --permanent
  #使防火墙更改的配置生效
  firewall-cmd --reload
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值