linux 一键部署mysql 8.4.2 绿色版

亲测有效

vim deploy_mysql.sh
#! /bin/bash

# mysql安装目录是 ${wget_path}/${mysql_dir}

# 修改mysql的登录密码
updatePassword="sykj@2024"
wget_path="/data"
mysql_dir="mysql"
#.tar.xz文件
mysql_dowmload_path="https://downloads.mysql.com/archives/get/p/23/file/mysql-8.4.2-linux-glibc2.28-x86_64.tar.xz"

mysql_tar=$(echo $mysql_dowmload_path | awk -F/ '{print $NF}')
mysql_tar_dir=$(basename $mysql_tar .tar.xz)
mkdir -p ${wget_path} 
echo "下载mysql的tar文件,解压"
wget  -P ${wget_path} ${mysql_dowmload_path}
tar xvf ${wget_path}/$mysql_tar -C ${wget_path}
echo "重命名"
mv ${wget_path}/$mysql_tar_dir ${wget_path}/${mysql_dir}
echo "创建mysql数据目录"
mkdir ${wget_path}/${mysql_dir}/data
echo "配置mysql"
cat <<EOF > ${wget_path}/${mysql_dir}/my.cnf
[client]
socket=${wget_path}/${mysql_dir}/mysql.sock

[mysqld]
user=root
datadir=${wget_path}/${mysql_dir}/data
basedir=${wget_path}/${mysql_dir}
socket=${wget_path}/${mysql_dir}/mysql.sock
port=3306
#允许最大连接数
max_connections=2000
#日志路径
log-error=${wget_path}/${mysql_dir}/mysqld.log
#开启慢sql日,超过2秒记录
slow_query_log = 1
long_query_time = 2
slow_query_log_file = ${wget_path}/${mysql_dir}/mysql-slow.log
#禁用主机名解析
skip-name-resolve
#字符集配置
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
EOF

echo "初始化mysql"
${wget_path}/${mysql_dir}/bin/mysqld  --defaults-file=${wget_path}/${mysql_dir}/my.cnf  --initialize
echo "启动mysql"
${wget_path}/${mysql_dir}/bin/mysqld  --defaults-file=${wget_path}/${mysql_dir}/my.cnf &
echo "获取mysql登录密码"
password=$(grep 'temporary password' ${wget_path}/${mysql_dir}/mysqld.log | awk '{print $NF}')
sleep 3
echo "登录mysql修改root用户密码,创建远程登录用户"
${wget_path}/${mysql_dir}/bin/mysql -S ${wget_path}/${mysql_dir}/mysql.sock --connect-expired-password -uroot -p"${password}" << eof
ALTER USER root@'localhost' identified by "Sykj_2022";
INSTALL PLUGIN validate_password SONAME 'validate_password.so';
set global validate_password_policy=LOW;
set global validate_password_length=1;
ALTER USER root@'localhost' identified by "${updatePassword}";
CREATE USER 'root'@'%'  identified by "${updatePassword}";
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
flush privileges;
eof
echo "脚本运行成功!"
chmod a+x deploy_mysql.sh

 客户端连接mysqld命令

/data/mysql/bin/mysql -uroot -S /data/mysql/mysql.sock -p

 

posted @ 2024-10-23 12:06  苦逼yw  阅读(8)  评论(0编辑  收藏  举报