【Linux】Centos7 shell实现MySQL5.7 tar 一键安装
作者:mmseoamin日期:2023-12-11

【Linux】Centos7 shell实现MySQL5.7 tar 一键安装,第1张

🦄 个人主页——🎐个人主页 🎐✨🍁

🪁🍁🪁🍁🪁🍁🪁🍁 感谢点赞和关注 ,每天进步一点点!加油!🪁🍁🪁🍁🪁🍁🪁🍁

目录

一、安装下载

二、执行安装


一、安装下载


【Linux】Centos7 shell实现MySQL5.7 tar 一键安装,第2张

下载地址:

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.44-el7-x86_64.tar.gz

脚本和tar包结构

【Linux】Centos7 shell实现MySQL5.7 tar 一键安装,第3张

my.cnf 配置文件

[root@windp-aio opt]# cat config/my.cnf 
[mysql]
socket=/var/lib/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8
[mysqld]
socket=/var/lib/mysql/mysql.sock
# set mysql server port  
port = 3306
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=200
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true
[mysql.server]
user=mysql
basedir=/usr/local/mysql

二、执行安装


install_mysql.sh 安装脚本

#! /bin/bash
#
# Author: kangll
# CreateTime: 2023-11-10
# Desc: install mysql5.7.44
#
echo "******** INSTALL MYSQL *********"
BASEDIR=$(cd "$(dirname "$0")"; pwd)
# 卸载原有的mariadb
OLD_MYSQL=`rpm -qa|grep mariadb`
profile=/etc/profile
for mariadb in $OLD_MYSQL
do
	rpm -e --nodeps $mariadb
done
# 删除原有的my.cnf
rm -rf /etc/my.cnf
#添加用户组 用户
groupadd mysql
useradd -g mysql mysql
#解压mysql包并修改名称
tar -zxvf  $BASEDIR/mysql-5.7.44-el7-x86_64.tar.gz -C /usr/local
mv /usr/local/mysql-5.7.44-el7-x86_64 /usr/local/mysql
#更改所属的组和用户
chown -R mysql /usr/local/mysql
chgrp -R mysql /usr/local/mysql
mkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/log
chown -R mysql:mysql /usr/local/mysql/data
#粘贴配置文件my.cnf 内容见八 中的 my.cnf
cp $BASEDIR/config/my.cnf /usr/local/mysql/
# 安装mysql
/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
# 设置文件及目录权限:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chown 777 /usr/local/mysql/my.cnf
chmod +x /etc/init.d/mysqld
mkdir /var/lib/mysql
chmod 777  /var/lib/mysql
# 启动mysql
/etc/init.d/mysqld start
# 设置开机启动
chkconfig --level 35 mysqld on
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
# 修改环境变量 
ln -s /usr/local/mysql/bin/mysql /usr/bin
ln -s /var/lib/mysql/mysql.sock   /tmp/
cat > /etc/profile.d/mysql.sh< 

执行完脚本 成功登录

【Linux】Centos7 shell实现MySQL5.7 tar 一键安装,第4张

ssh 操作需要免密 附上 本机免密脚本 ssh_keygen.sh

#! /bin/bash
#
#Author: kangll
#CreateTime: 2023-11-10
#Desc: 本机免密
#
set -x
# get ip 
ip=$(ip addr show | grep -E 'inet [0-9]' | awk '{print $2}' | awk -F '/' '{print $1}' | sed -n '$p')
ssh_hosts=${ip}
ssh_networkname=(windp-aio)
ssh_passwd=winner@001
 
# 定义修改/etc/hosts文件的方法
#echo "开始修改地址映射"
 
#for ((i=0;i<${#ssh_hosts[*]};i++));do
#    sed -i '/^'"${ssh_hosts[$i]}"'/d' /etc/hosts
#	echo "尝试连接: ${ssh_networkname[$i]}"
#	timeout 5 ssh root@${ssh_networkname[$i]} "echo ${ssh_networkname[$i]}: 'This is success!'"
#	if [[ $? -ne 0 ]];then
#		echo "添加地址映射:${ssh_hosts[$i]} ${ssh_networkname[$i]}"
#		echo "${ssh_hosts[$i]} ${ssh_networkname[$i]}" >> /etc/hosts
#	fi
#done
 
expect -v
if [ `echo $?` -ne 0 ];then
	echo "安装expect命令"
	yum install -y expect
fi 
################################
#
################################
create_ssh_pub(){
	echo "生成本地ssh公钥"
	/usr/bin/expect << eof
	# 设置捕获字符串后,期待回复的超时时间
	set timeout 30
	 
	spawn ssh-keygen -t rsa -b 1024
	 
	## 开始进连续捕获
	expect	{
		".ssh/id_rsa)"      { send "\n";  exp_continue }
		"Overwrite (y/n)?"  { send "y\n"; exp_continue }
		"no passphrase):"   { send "\n";  exp_continue }
		"passphrase again:" { send "\n";  exp_continue }
	}
eof
}
if [ ! -f /root/.ssh/id_rsa.pub ];then
	create_ssh_pub
fi
 
#################################
# 定义复制ssh公钥方法
#################################
config_copy_ssh(){
	echo "复制公钥到对应的主机上"
	/usr/bin/expect << eof
	# 设置捕获字符串后,期待回复的超时时间
	set timeout 30
	 
	spawn ssh-copy-id -i /root/.ssh/id_rsa.pub @
	 
	## 开始进连续捕获
	expect	{
		"connecting (yes/no)?" { send "yes\n";  exp_continue }
		"s password:"          { send "${ssh_passwd}\n"; exp_continue }
	}
eof
}
for name in ${ssh_networkname[*]};do
	timeout 5 ssh root@${name} "echo ${name}: 'This is success!'"
	if [[ $? -ne 0 ]];then
		echo "复制文件到: ${name}"
		config_copy_ssh root ${name} > /dev/null
	fi
	
done

参考链接:ansible 实现自动化部署ambari(纯离线)_ansible ambari-CSDN博客