(资料图)
EL6, EL7, EL8, and EL9-based platforms (for example, the corresponding versions of Oracle Linux, Red Hat Enterprise Linux, and CentOS),本脚本使用的是CentOS7。
此脚本安装的是MySQL57,如果需要使用8.X版本的,只需在配置yum源时将57改为80即可
此脚本的好处便是能够使用一条命令即可安装完成MySQL
脚本直接使用bash 脚本名称或者sh 脚本名称即可运行,如果安装成功则等待设置MySQL密码即可。
#!/bin/bash # 配置mysql yum源 wget https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm rpm -ivh mysql57-community-release-el7-7.noarch.rpm # 更新GPG验证密钥,并安装mysql,如果不更新密钥,安装则会失败 # 如果不更新最后两行可能报以下错误 # Failing package is: mysql-community-client-5.7.38-1.el7.x86_64 # GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 yum install mysql-server -y # 启动mysql echo "正在启动MySQL,请稍等......" systemctl start mysqld.service if [ $? -ne 0 ];then echo "MySQL启动失败!!!" exit else echo "MySQL启动成功!!!" echo "====================================" fi # 获取初始密码 initpasswd=`cat /var/log/mysqld.log | grep password | awk "{print $NF}"` echo "初始密码为:${initpasswd}" # 用户设置密码 flag=1 while [ $flag -eq 1 ] do read -p "请输入新密码:" newpasswd read -p "请确认密码:" secondpasswd if [ $newpasswd -eq $secondpasswd ] then flag=2 else echo "两次密码不一致,请重新输入" echo "====================================" fi done echo "设置的新密码为:${newpasswd}" mysql --connect-expired-password -uroot -p"${initpasswd}" -e "set global validate_password_policy=0;set global validate_password_length=1;alter user "root"@"localhost" identified by "${newpasswd}";" if [ $? -ne 0 ];then echo "新密码设置失败!!!" exit else echo "====================================" echo "新密码设置成功!,新密码为:${newpasswd}" echo "====================================" fi echo "正在开启远程登录......" mysql --connect-expired-password -uroot -p"${newpasswd}" -e "update mysql.user set Host = "%" where Host = "localhost" and User="root";flush privileges;" if [ $? -ne 0 ];then echo "远程登录开启失败!!!" exit else echo "====================================" echo "远程登录开启成功!" echo "====================================" fi
到此这篇关于MySQL一键安装Shell脚本的实现的文章就介绍到这了,更多相关MySQL安装Shell脚本内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
关键词:
Copyright 2000-2021 by www.jiaoyu.rexun.cn all rights reserved
备案号:豫ICP备20005723号-6
邮箱 : 29 59 11 57 8@qq.com