【Linux】Centos7 shell实现MySQL5.7 tar 一键安装

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

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

目录

一、安装下载

二、执行安装


一、安装下载


下载地址:

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

脚本和tar包结构

my.cnf 配置文件

cs 复制代码
[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 安装脚本

bash 复制代码
#! /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<<EOF
  export PATH=$PATH:/usr/local/mysql/bin
EOF
mysqlPw=`sed -n 2p /root/.mysql_secret`
mysqlPwTMP=`sed -n 2p /root/.mysql_secret`1
mysqlNewPw=Winner001
hostname=`"hostname"`
#ssh $hostname "source /etc/profile;java -version"
ssh $hostname "source /etc/profile;mysqladmin -h127.0.0.1 -uroot -p'$mysqlPw' password '$mysqlPwTMP';mysqladmin -h127.0.0.1 -uroot -p'$mysqlPwTMP' password '$mysqlNewPw';exit"


echo "******** MYSQL installation completed *******

执行完脚本 成功登录

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

bash 复制代码
#! /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 $1@$2
	 
	## 开始进连续捕获
	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博客

相关推荐
苹果醋31 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
别致的影分身2 小时前
使用C语言连接MySQL
数据库·mysql
过过过呀Glik2 小时前
在 Ubuntu 上安装 MySQL 的详细指南
mysql·ubuntu
Sunyanhui16 小时前
牛客网 SQL36查找后排序
数据库·sql·mysql
老王笔记6 小时前
MHA binlog server
数据库·mysql
2401_871213307 小时前
mysql高阶语句
数据库·mysql
山山而川粤9 小时前
网络安全宣传系统|Java|SSM|JSP|
java·开发语言·后端·学习·mysql
_汤姆大叔10 小时前
MySQL 高级操作全解析
数据库·mysql
呼啦啦啦啦啦啦啦啦11 小时前
【MySQL篇】聚合查询,联合查询
数据库·mysql
今天不coding12 小时前
MySQL体系架构
数据库·mysql·体系架构·网络连接层·服务层·存储引擎层·系统文件层