一、安装包和脚本准备:
下载所需的安装包上传至任务目录下,这里我以8.0.33版本为例,安装包大家自行去MySQL官网下载。
链接: https://pan.baidu.com/s/10KFBi40h2oGURQTm9Z9IvQ
提取码: dgex
二、安装:
编辑好脚本,赋予脚本执行权限,然后source执行脚本。只需要输入自己所需要的版本号即可轻松完成数据库的安装。
[root@localhost ~]#chmod +x mysql_install.sh
[root@localhost ~]#source mysql_install.sh
三、脚本:
#!/bin/bash
echo "-----------------------------开始MYSQL安装--------------------------------------"
start_time=$(date +%s)
echo -e "\e[31m***************一键安装mysql任何版本数据库******************\e[0m"
echo -e "输入安装版本,如\e[31m8.0.27\e[0m"
read version
find / -name mysql | xargs rm -rf
port=$(netstat -antup|grep mysql|wc -l)
if [ $port != 0 ]
then echo "mysql进程存在,请先杀掉进程"
ps -ef |grep mysqld
exit 1
fi
echo "-----------------创建所需目录及用户并上传安装包----------------------------"
# 获取当前所在目录位置
current_dir=$(pwd)
echo "当前所在目录位置: $current_dir"
# 目标路径
target_dir="/opt"
# 检查目标路径是否存在,如果不存在则创建
if [ ! -d "$target_dir" ]; then
mkdir -p "$target_dir"
echo "已创建目录: $target_dir"
fi
# 移动当前目录下的所有文件到目标路径
mv $current_dir/* $target_dir
echo "已将当前目录下所有文件移动至 $target_dir"
mkdir -p /data/mysql
groupadd mysql
useradd -r -g mysql mysql
cd /opt/
tar -xvf mysql-$version-linux-glibc2.12-x86_64.tar.xz
mv mysql-$version-linux-glibc2.12-x86_64/ /usr/local/
cd /usr/local/
mv mysql-$version-linux-glibc2.12-x86_64/ mysql
chown -R mysql.mysql /usr/local/mysql/
echo "-----------------------------卸载原有的mysql组件--------------------------"
yum list installed | grep mariadb
yum -y remove mariadb*
yum remove mariadb*
chown mysql:mysql -R /data/mysql
touch /etc/my.cnf
chmod 644 /etc/my.cnf
MYSQL_ROOT_PASSWORD=123456
cat <<EOF >/etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
server_id=1
port=3306
character-set-server=utf8
innodb_rollback_on_timeout = ON
character-set-server = utf8
collation-server=utf8_general_ci
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
max_connections=10000
sync_binlog=1
binlog_format=row
log-bin=mysql-bin
expire_logs_days=30
[mysql]
socket=/tmp/mysql.sock
default-character-set=utf8
[client]
EOF
echo "-----------------------------------初始化数据库-----------------------------------"
cd /usr/local/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
path=$(grep 'MYSQL_HOME' /etc/profile|wc -l)
if [ $path != 0 ]
then
echo -e "\e[31m MYSQL_HOME路径存在\e[0m"
else
echo "export MYSQL_HOME=/usr/local/mysql/bin" >> /etc/profile
echo "export PATH=\$PATH:\$MYSQL_HOME" >> /etc/profile
source /etc/profile
fi
echo "---------------------------------启动MYSQL服务---------------------------------------"
service mysql start
echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib'>>/etc/profile
sleep 3
source /etc/profile
cat /data/mysql/mysql.err|grep password
chkconfig --add mysql
chkconfig mysql on
chkconfig --list mysql
echo "-----------------------------恭喜!MYSQL安装成功--------------------------------------"
end_time=$(date +%s)
execution_time=$((end_time - start_time))
echo "脚本执行时间:${execution_time} 秒"
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.err`
mysqladmin -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD}
mysql -uroot -p123456 -e "update mysql.user set host ='%' where user ='root';"
mysql -uroot -p123456 -e "flush privileges;"
mysql -uroot -p123456
说明:此脚本适用于mysql任何版本,如有问题,请大家随时联系我。