Mysql
主页 > 数据库 > Mysql >

MySQL一键安装Shell脚本的实现

2024-02-16 | 佚名 | 点击:

一、脚本说明

1、linux系统版本

EL6, EL7, EL8, and EL9-based platforms (for example, the corresponding versions of Oracle Linux, Red Hat Enterprise Linux, and CentOS),本脚本使用的是CentOS7。

2、MySQL版本

此脚本安装的是MySQL57,如果需要使用8.X版本的,只需在配置yum源时将57改为80即可

3、运行方式

此脚本的好处便是能够使用一条命令即可安装完成MySQL
脚本直接使用bash 脚本名称或者sh 脚本名称即可运行,如果安装成功则等待设置MySQL密码即可。

二、脚本内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

#!/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

原文链接:
相关文章
最新更新