【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博客

相关推荐
Yaml43 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
追风林3 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
Hsu_kk5 小时前
MySQL 批量删除海量数据的几种方法
数据库·mysql
编程学无止境5 小时前
第02章 MySQL环境搭建
数据库·mysql
knight-n5 小时前
MYSQL库的操作
数据库·mysql
eternal__day7 小时前
MySQL_聚合函数&分组查询
数据库·mysql
咕哧普拉啦7 小时前
乐尚代驾十订单支付seata、rabbitmq异步消息、redisson延迟队列
java·spring boot·mysql·spring·maven·乐尚代驾·java最新项目
春哥的魔法书8 小时前
数据库基础(5) . DCL
数据库·mysql
鬼才血脉9 小时前
docker+mysql配置
mysql·adb·docker
一个假的前端男9 小时前
mysql 安装 windows
数据库·mysql