背景:有时候自己搭建集群或者测试MySQL的时候,需要经常安装MySQL,之前写过离线的安装文档,但还是感觉不太方便,每次都会花费很长时间,现在改成shell脚本自动安装MySQL,直接配置好新密码和远程访问,整个过程只需要几分钟就搞定
百度网盘中也上传了安装包,可以直接提取 2022-02-06添加
链接:https://pan.baidu.com/s/1XhrxmldLGrW9xCVRYlrUEA?pwd=ye4f
提取码:ye4f
1.建立临时文件夹
2.解压zip文件
3.删除centos7自带的MySQL
4.按顺序安装MySQL相关的依赖包
5.修改MySQL的密码(自己用的话可以设置简单的密码,生产上用建议用复杂密码)
6.配置MySQL可以被远程访问(实际开发中也是,有些机器需要有访问这个机器MySQL的权限)
#!/bin/bash
#第一步、创建一个临时的文件夹
mkdir mysql_soft
#第二步、把新上传的MySQL的安装包移动到mysql_soft下
mv mysql57.zip mysql_soft
#进入到mysql_soft目录下,开始执行下面的操作
cd mysql_soft
#解压MySQL的安装包,在当前目录下
unzip mysql57.zip
#查看集群上现有的MySQL,需要删除掉
mysql_old=`rpm -qa | grep mariadb`
echo -e "当前centos中MySQL版本是: ${mysql_old}"
#删除自带的MySQL
rpm -e --nodeps ${mysql_old}
#验证旧版本的MySQL是否被删除掉
mysql_validate=`rpm -qa | grep mariadb`
echo -e "验证旧版的MySQL是否被删除干净: ${mysql_validate}"
#开始安装MySQL,注意:这些包安装是有顺序的
rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
#判断安装的过程是否成功
if [ $? == 0 ];then
echo -e "安装------libaio-0.3.109-13.el7.x86_64.rpm -- 第1个包------- 成功"
else
echo -e "安装------libaio-0.3.109-13.el7.x86_64.rpm -- 第1个包------- 失败"
fi
#第二个包
rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm
if [ $? == 0 ];then
echo -e "安装------perl-Data-Dumper-2.145-3.el7.x86_64.rpm -- 第2个包------- 成功"
else
echo -e "安装------perl-Data-Dumper-2.145-3.el7.x86_64.rpm -- 第2个包------- 失败"
fi
#第三个包:安装rpm -ivh numactl* 这是安装以前缀numactl开始的所有安装包
rpm -ivh numactl*
if [ $? == 0 ];then
echo -e "安装------numactl* -- 第3个包------- 成功"
else
echo -e "安装------numactl* -- 第3个包------- 失败"
fi
#第四个包:解压mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
tar -xf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
if [ $? == 0 ];then
echo -e "解压------mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar ------- 成功"
else
echo -e "解压------mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar ------- 失败"
fi
#第四个包:安装 mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
if [ $? == 0 ];then
echo -e "安装------mysql-community-common-5.7.26-1.el7.x86_64.rpm -- 第4个包------- 成功"
else
echo -e "安装------mysql-community-common-5.7.26-1.el7.x86_64.rpm -- 第4个包------- 失败"
fi
#第五个包:安装mysql-community-libs-*
rpm -ivh mysql-community-libs-*
if [ $? == 0 ];then
echo -e "安装------mysql-community-libs-* -- 第5个包------- 成功"
else
echo -e "安装------mysql-community-libs-* -- 第5个包------- 失败"
fi
#第六个包:安装mysql-community-devel-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.26-1.el7.x86_64.rpm
if [ $? == 0 ];then
echo -e "安装------mysql-community-devel-5.7.26-1.el7.x86_64.rpm -- 第6个包------- 成功"
else
echo -e "安装------mysql-community-devel-5.7.26-1.el7.x86_64.rpm -- 第6个包------- 失败"
fi
#第七个包:安装net-tools-2.0-0.25.20131004git.el7.x86_64.rpm
rpm -ivh net-tools-2.0-0.25.20131004git.el7.x86_64.rpm
if [ $? == 0 ];then
echo -e "安装------net-tools-2.0-0.25.20131004git.el7.x86_64.rpm -- 第7个包------- 成功"
else
echo -e "安装------net-tools-2.0-0.25.20131004git.el7.x86_64.rpm -- 第7个包------- 失败"
fi
#第八个包:安装mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
if [ $? == 0 ];then
echo -e "安装------mysql-community-client-5.7.26-1.el7.x86_64.rpm -- 第8个包------- 成功"
else
echo -e "安装------mysql-community-client-5.7.26-1.el7.x86_64.rpm -- 第8个包------- 失败"
fi
#第九个包:安装mysql-community-server-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
if [ $? == 0 ];then
echo -e "安装------mysql-community-server-5.7.26-1.el7.x86_64.rpm -- 第9个包------- 成功"
else
echo -e "安装------mysql-community-server-5.7.26-1.el7.x86_64.rpm -- 第9个包------- 失败"
fi
#启动MySQL
service mysqld start
#验证是否启动成功可以查看MySQL的启动状态
service mysqld status >start.log
#查看MySQL的初始密码,把携带密码信息加载到下面这个文件中
grep 'temporary password' /var/log/mysqld.log >result.txt
#读取文件中的密码,用于登录MySQL
password_mysql=`cat result.txt | grep 'localhost' | awk -F ': ' '{print $2}'`
echo -e "查看MySQL的原始密码是啥? ${password_mysql}"
#连接MySQL的几个参数
hostname="localhost"
#hostname="192.168.15.12"
username="root"
port="3306"
#进入MySQL的交互模式,修改相应的参数,设置简单的密码为123456,为了以后自己用方便,实际上生产的密码都比较复杂一些
#mysql -h${hostname} -u${username} -P${port} -p${password_mysql} <<EOF
mysql -u${username} -p${password_mysql} --connect-expired-password <<EOF
set global validate_password_policy=0;
set global validate_password_length=1;
alter user 'root'@'localhost' identified by '123456';
quit
EOF
new_password="123456" #这里是配置远程访问策略
mysql -h${hostname} -u${username} -P${port} -p${new_password} <<EOF
GRANT ALL PRIVILEGES ON *.*TO 'root'@'%' IDENTIFIED BY '123456';
flush privileges;
quit
EOF
有问题,欢迎大家留言交流哈,😄
后续 安装遇到问题,记录一下
在执行这条命令:rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm 的时候,报错如下;
[dsjyy@vm-cqhyzh-mysql ~]$ rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm
warning: perl-Data-Dumper-2.145-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
error: Failed dependencies:
perl >= 0:5.006_001 is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(:MODULE_COMPAT_5.16.3) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(B::Deparse) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(Carp) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(Exporter) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(Scalar::Util) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(XSLoader) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(bytes) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(constant) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
perl(overload) is needed by perl-Data-Dumper-2.145-3.el7.x86_64
报错原因:缺少perl的安装包
解决方案:安装perl的相关依赖包即可,所有的perl的安装包如下;
-rw-r--r--. 1 dsjyy dsjyy 8360316 Feb 3 2021 perl-5.16.3-299.el7_9.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 19672 Jul 4 2014 perl-Carp-1.26-244.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 19244 Jul 4 2014 perl-constant-1.27-2.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 1545440 Jul 4 2014 perl-Encode-2.51-7.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 29092 Jul 4 2014 perl-Exporter-5.68-3.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 27088 Jul 4 2014 perl-File-Path-2.09-2.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 57680 Jul 4 2014 perl-File-Temp-0.23.01-3.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 78236 Jul 4 2014 perl-Filter-1.49-3.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 57176 Apr 25 2018 perl-Getopt-Long-2.40-3.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 39292 Jul 4 2014 perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 706128 Feb 3 2021 perl-libs-5.16.3-299.el7_9.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 45324 Feb 3 2021 perl-macros-5.16.3-299.el7_9.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 12592 Jul 4 2014 perl-parent-0.225-244.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 84468 Jul 4 2014 perl-PathTools-3.40-5.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 53064 Feb 3 2021 perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 114320 Jul 4 2014 perl-podlators-2.5.1-3.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 88756 Jul 4 2014 perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 221216 Jul 4 2014 perl-Pod-Simple-3.28-4.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 27436 Jul 4 2014 perl-Pod-Usage-1.63-3.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 36808 Jul 4 2014 perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 49868 Apr 3 2020 perl-Socket-2.010-5.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 78888 Jul 4 2014 perl-Storable-2.45-3.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 14056 Jul 4 2014 perl-Text-ParseWords-3.29-4.el7.noarch.rpm
-rw-r--r--. 1 dsjyy dsjyy 50392 Jul 4 2014 perl-threads-1.87-4.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 39868 Jul 4 2014 perl-threads-shared-1.43-6.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 46304 Jul 4 2014 perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
-rw-r--r--. 1 dsjyy dsjyy 24792 Jul 4 2014 perl-Time-Local-1.2300-2.el7.noarch.rpm
从官网下载太麻烦了,从下面的地址下载即可,提取码: jnbr
https://pan.baidu.com/share/init?surl=WC-pUB8JTnr1H0rfA6S0GA
安装方法
[dsjyy@vm-cqhyzh-mysql perl]$ sudo rpm -ivh perl* --nodeps --force
[sudo] password for dsjyy:
warning: perl-5.16.3-299.el7_9.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:perl-parent-1:0.225-244.el7 ################################# [ 4%]
2:perl-HTTP-Tiny-0.033-3.el7 ################################# [ 7%]
3:perl-podlators-2.5.1-3.el7 ################################# [ 11%]
4:perl-Pod-Perldoc-3.20-4.el7 ################################# [ 15%]
5:perl-Pod-Escapes-1:1.04-299.el7_9################################# [ 19%]
6:perl-Text-ParseWords-3.29-4.el7 ################################# [ 22%]
7:perl-Encode-2.51-7.el7 ################################# [ 26%]
8:perl-Pod-Usage-1.63-3.el7 ################################# [ 30%]
9:perl-constant-1.27-2.el7 ################################# [ 33%]
10:perl-Carp-1.26-244.el7 ################################# [ 37%]
11:perl-Exporter-5.68-3.el7 ################################# [ 41%]
12:perl-Filter-1.49-3.el7 ################################# [ 44%]
13:perl-libs-4:5.16.3-299.el7_9 ################################# [ 48%]
14:perl-macros-4:5.16.3-299.el7_9 ################################# [ 52%]
15:perl-File-Path-2.09-2.el7 ################################# [ 56%]
16:perl-File-Temp-0.23.01-3.el7 ################################# [ 59%]
17:perl-PathTools-3.40-5.el7 ################################# [ 63%]
18:perl-Scalar-List-Utils-1.27-248.e################################# [ 67%]
19:perl-Socket-2.010-5.el7 ################################# [ 70%]
20:perl-Storable-2.45-3.el7 ################################# [ 74%]
21:perl-threads-1.87-4.el7 ################################# [ 78%]
22:perl-threads-shared-1.43-6.el7 ################################# [ 81%]
23:perl-Time-HiRes-4:1.9725-3.el7 ################################# [ 85%]
24:perl-Pod-Simple-1:3.28-4.el7 ################################# [ 89%]
25:perl-Getopt-Long-2.40-3.el7 ################################# [ 93%]
26:perl-Time-Local-1.2300-2.el7 ################################# [ 96%]
27:perl-4:5.16.3-299.el7_9 ################################# [100%]
安装完成之后,验证一下
[dsjyy@vm-cqhyzh-mysql perl]$ perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 44 registered patches, see perl -V for more detail)
Copyright 1987-2012, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.