参考 https://blog.csdn.net/qq_35995514/article/details/134350572?spm=1001.2014.3001.5501
源文章是centos 的 教程,这里为了大家的方便,再原作者基础上做了修改,记录了ubuntu的22.04的我的配置,加了一个删除原有mysql 的脚本
文章目录
- 一、安装下载
- **my.cnf 配置文件**
- 二、执行安装
- **install_mysql.sh 安装脚本**
- 本机免密脚本 ssh_keygen.sh
- mysql 删除脚本
一、安装下载
下载地址:
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.44-el7-x86_64.tar.gz
脚本和tar包结构
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: dearning
# CreateTime: 2024-09-11
# Desc: install mysql5.7.44 on Ubuntu
#
echo "******** INSTALL MYSQL *********"BASEDIR=$(cd "$(dirname "$0")"; pwd)# 卸载原有的mariadb
OLD_MYSQL=$(dpkg -l | grep mariadb | awk '{print $2}')
for mariadb in $OLD_MYSQL; doapt-get remove --purge -y $mariadb
done# 删除原有的my.cnf
rm -rf /etc/mysql/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/mysqlmkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/log
chown -R mysql:mysql /usr/local/mysql/data# 粘贴配置文件my.cnf
cp $BASEDIR/config/my.cnf /usr/local/mysql/# 安装mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/# 创建持久的 MySQL 服务文件
if [ ! -f /etc/systemd/system/mysql.service ]; thencat > /etc/systemd/system/mysql.service <<EOF
[Unit]
Description=MySQL Community Server
After=network.target[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf
ExecStop=/usr/local/mysql/bin/mysqladmin -u root -p shutdown
Type=exec
Restart=on-failure[Install]
WantedBy=multi-user.target
EOF
fi# 重新加载系统守护进程
sudo systemctl daemon-reload# 启动mysql
systemctl start mysql# 设置开机启动
systemctl enable mysql# 修改环境变量
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
EOFmysqlPw=$(sed -n 2p /root/.mysql_secret)
mysqlPwTMP=$(sed -n 2p /root/.mysql_secret)1
mysqlNewPw=sjc123
hostname=$(hostname)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
#! /bin/bash
#
# Author: dearning
# CreateTime: 2024-09-11
# Desc: 本机免密
#
set -x# 获取本机 IP
ip=$(hostname -I | awk '{print $1}')
ssh_hosts=${ip}
ssh_networkname=(windp-aio)
ssh_passwd=winner@001# 安装 expect
if ! command -v expect &> /dev/null; thenecho "安装 expect 命令"sudo apt-get updatesudo apt-get install -y expect
fi ################################
# 生成 SSH 公钥
################################
create_ssh_pub(){echo "生成本地 SSH 公钥"/usr/bin/expect << eofset timeout 30spawn ssh-keygen -t rsa -b 2048expect {"Enter file in which to save the key*" { send "\n"; exp_continue }"Overwrite (y/n)?" { send "y\n"; exp_continue }"Enter passphrase (empty for no passphrase):" { send "\n"; exp_continue }"Enter same passphrase again:" { send "\n"; exp_continue }}
eof
}if [ ! -f ~/.ssh/id_rsa.pub ]; thencreate_ssh_pub
fi#################################
# 复制 SSH 公钥到对应的主机上
#################################
config_copy_ssh(){echo "复制公钥到对应的主机上"/usr/bin/expect << eofset timeout 30spawn ssh-copy-id -i ~/.ssh/id_rsa.pub $1@$2expect {"Are you sure you want to continue connecting (yes/no)?" { send "yes\n"; exp_continue }"password:" { send "${ssh_passwd}\n"; exp_continue }}
eof
}for name in ${ssh_networkname[*]}; dotimeout 5 ssh root@${name} "echo ${name}: 'This is success!'"if [[ $? -ne 0 ]]; thenecho "复制公钥到: ${name}"config_copy_ssh root ${name} > /dev/nullfi
doneecho "SSH 免密登录设置完成。"
mysql 删除脚本
rm_mysql.sh
sudo systemctl stop mysql
sudo systemctl disable mysql
sudo userdel mysql
sudo groupdel mysql
sudo rm -rf /var/lib/mysql
sudo rm -rf /tmp/mysql.sock
sudo rm -rf /usr/bin/mysql
sudo rm -rf /tmp/mysql.sock
sudo rm -rf /usr/local/mysql
sudo rm -f /etc/mysql/my.cnf
sudo rm -f /etc/init.d/mysqld
sudo rm -f /etc/systemd/system/mysql.service
sudo rm -f /lib/systemd/system/mysql.service
sudo rm -f /etc/profile.d/mysql.sh