1. 准备工作

现在 MySQL 虽说可以通过 Docker 一键安装,但是还是有很多公司的项目依旧在使用传统的方式进行安装。

如下方式可以通过脚本一键安装 MySQL 5.7.35

准备环境:

  • 操作系统:CentOS 7.6 64位
  • 实例配置:1核 1GB 5Mbps
  • MySQL:mysql 5.7.35

准备脚本(网盘自取):

2. 步骤

  1. 将脚本上传至 /tmp 目录下
  2. 给脚本添加权限添加权限
  3. 执行脚本
  4. 开放防火墙端口,并刷新配置
  5. 配置 MySQL
  6. 测试链接
  1. 将脚本上传至 /tmp 目录下

    image-20230511133516733

  2. 给脚本添加权限添加权限

    1
    [root@VM-24-4-centos tmp]# chmod +x mysqlinstall_auto.sh 
  3. 执行脚本

    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
    [root@VM-24-4-centos tmp]# ./mysqlinstall_auto.sh 
    #################################################
    MySQL自动部署脚本执行
    #################################################
    发现MySQL-server安装文件 mysql-community-server-5.7.35-1.el7.x86_64.rpm
    发现MySQL-client安装文件 mysql-community-client-5.7.35-1.el7.x86_64.rpm
    --------------------------------------------------------------------
    正在安装MySQL...
    正在卸载MariaDB
    已卸载MariaDB
    正在安装MySQL-server...
    warning: mysql-community-server-5.7.35-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
    ################################# [100%]
    Updating / installing...
    ################################# [100%]
    server安装完成
    正在安装MySQL-client...
    warning: mysql-community-client-5.7.35-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
    ################################# [100%]
    Updating / installing...
    ################################# [100%]
    client安装完成
    --------------------------------------------------------------------
    正在启动MySQL...
    启动成功
    --------------------------------------------------------------------
    正在获取mysql初始密码
    mysql初始密码: )tLp(LCD<6*S
    -------------------------------------------------
    部署脚本执行完成
  4. 开放防火墙端口,并刷新配置

    1
    2
    3
    4
    5
    [root@VM-24-4-centos tmp]# firewall-cmd --zone=public --add-port=3306/tcp --permanent # 开放3306端口
    success
    [root@VM-24-4-centos tmp]# firewall-cmd --reload # 刷新防火墙配置
    success
    [root@VM-24-4-centos tmp]#
  5. 配置 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
    [root@VM-24-4-centos tmp]# mysql -u root -p 
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.35

    Copyright (c) 2000, 2021, Oracle and/or its affiliates.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    # 首次操作要求重置密码,必须大小写特殊字符组成, 我设置的密码为 P@ssw0rd
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'P@ssw0rd';
    Query OK, 0 rows affected (0.00 sec)

    # 授权远程访问 % 表示所有主机都可以访问
    mysql> grant all privileges on *.* to 'root'@'%' identified by 'P@ssw0rd' with grant option;
    Query OK, 0 rows affected, 1 warning (0.00 sec)

    mysql> flush privileges; # 刷新权限信息
    Query OK, 0 rows affected (0.00 sec)

    mysql> exit
    Bye
    [root@VM-24-4-centos tmp]#
  6. 测试链接

    image-20230511140119459

3. 脚本解析

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash

# 指定安装文件夹
package_dir="/tmp"

echo "#################################################"
echo "MySQL自动部署脚本执行"
echo "#################################################"

cd "${package_dir}"

# 查找MySQL-server和MySQL-client的安装包
server=$(find -maxdepth 1 -name '*mysql*' | grep server | awk -F "/" '{print $2}')
client=$(find -maxdepth 1 -name '*mysql*' | grep client | awk -F "/" '{print $2}')

# 检查MySQL-server安装包是否存在,不存在则退出
if [[ -z "${server}" ]]; then
echo "未找到MySQL-server安装文件"
exit 0
else
echo "发现MySQL-server安装文件 ${server}"
fi

# 检查MySQL-client安装包是否存在,不存在则退出
if [[ -z "${client}" ]]; then
echo "未找到MySQL-client安装文件"
exit 0
else
echo "发现MySQL-client安装文件 ${client}"
fi

# 安装MySQL
function install_mysql() {
echo "--------------------------------------------------------------------"
echo "正在安装MySQL..."

# 检查系统自带数据库MariaDB是否存在,如果存在则卸载
mariadb=$(rpm -qa | grep mariadb)
if [[ -n "${mariadb}" ]]; then
echo "正在卸载MariaDB"
rpm -e "${mariadb}" --nodeps
echo "已卸载MariaDB"
else
echo "未找到MariaDB软件包"
fi

# 检查服务器上是否已安装MySQL,如果已经安装则退出
ismysql=$(rpm -qa | grep mysql)
if [[ -n "${ismysql}" ]]; then
echo "mysql已存在,请确认"
exit 0
else
# 安装MySQL-server和MySQL-client
echo "正在安装MySQL-server..."
rpm -ih "${server}" --force --nodeps
if [ $? -eq 0 ];then
echo "server安装完成"
else
echo "server安装失败,请检查报错信息!"
exit 1
fi

echo "正在安装MySQL-client..."
rpm -ih "${client}" --force --nodeps
if [ $? -eq 0 ];then
echo "client安装完成"
else
echo "client安装失败,请检查报错信息!"
exit 1
fi
fi
}

# 启动MySQL
function startup(){
echo "--------------------------------------------------------------------"
echo "正在启动MySQL..."
systemctl restart mysqld
if [ $? -eq 0 ];then
if netstat -ntulp | grep -q 3306
then
echo "启动成功"
else
echo "启动失败,请查看日志!"
exit 1
fi
else
echo "启动失败,请查看日志!"
exit 1
fi
}

# 获取MySQL的初始密码
function get_password() {
echo "--------------------------------------------------------------------"
echo "正在获取mysql初始密码"
init_password=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}')
echo "mysql初始密码: $init_password"
}

install_mysql
startup
get_password

echo "-------------------------------------------------"
echo "部署脚本执行完成"