1 Star 2 Fork 1

hxsaj/tools_shell

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mysql_auto_install.sh 9.74 KB
一键复制 编辑 原始数据 按行查看 历史
gohou 提交于 2024-03-23 20:20 . 修改部分实现
#!/usr/bin/env bash
set -e
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :CentOS7.X 二进制安装mysql
# Platform :RedHatEL7.x Based Platform
# Version :1.01
# Date :2022-10-14
# Author :mugoLH
# Contact :hxsaj@126.com
# Company :liando
# depend on:
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# 脚本引用 Import the script
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# 变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# 已被此脚本位置参数2代替
#Downloads_bin_file_address="https://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz"
# 已被此脚本位置参数1代替
#install_dir="/data"
# 函数列表 Function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# 检查wget命令
function wget_chack(){
if [[ $(whereis wget | wc -l) -ne 1 ]];then
echo -e "[ 安装wget ]: " && yum -y -q install wget
fi
}
# 下载 mysql 二进制文件
function check_or_download_file(){
BintarFile_or_DownloadURL=${1}
# 判断是文件还是下载地址
if [[ -f ${BintarFile_or_DownloadURL} ]];then
export Bin_tar_file=${BintarFile_or_DownloadURL}
echo -e "[ 安装文件 ]: ${Bin_tar_file}"
else
# 检查下链接是否正确
if [[ $(curl -I -m 10 -o /dev/null -s -w %{http_code} ${BintarFile_or_DownloadURL} ) -eq 200 ]];then
wget_chack
echo -e "[ 下载文件 ]:" && wget -c ${BintarFile_or_DownloadURL} --no-check-certificate
# 确定下载文件的的名字
export Bin_tar_file=$(echo "${BintarFile_or_DownloadURL}" | awk -F "/" '{print$NF}')
echo -e "[ 安装文件 ]: ${Bin_tar_file}"
else
echo -e "不是tar.gz/xz压缩包 或 文件下载URL错误!"
exit 2
fi
fi
}
# 安装 mysql 5.x 二进制文件
function mysql_install(){
install_dir=${1}
server_port=${2}
BintarFile_or_DownloadURL=${3}
if [[ $# -eq 3 ]];then
# 检查文件或下载文件
check_or_download_file ${BintarFile_or_DownloadURL}
# 创建安装目录
if [[ ! -d ${install_dir} ]];then
echo -e "创建安装目录:${install_dir}" && mkdir -p ${install_dir}
elif [[ $(ls -A ${install_dir}) ]]; then
echo -e "安装目录不为空" && exit 1001
fi
# 解压二进制到指定目录
echo -e "[ 解压文件 ]: ${Bin_tar_file}"
# 判断文件压缩类型
suffix=$(ls ${Bin_tar_file} | awk -F '.' '/tar/{print $NF}')
if [ ${suffix} == "xz" ];then
tar -Jxf ${Bin_tar_file} -C ${install_dir}/
Bin_Basename=$(tar -Jtf ${Bin_tar_file} | head -5 | awk -F "/" '{print$1}' |sort -nr |uniq)
elif [ ${suffix} == "gz" ]; then
tar -zxf ${Bin_tar_file} -C ${install_dir}/
Bin_Basename=$(tar -ztf ${Bin_tar_file} | head -5 | awk -F "/" '{print$1}' |sort -nr |uniq)
fi
# 修改目录名字,便于使用
Base_Mysql=$(echo ${Bin_Basename} |awk -F "-" '{print$1"-"$2}')
mv ${install_dir}/${Bin_Basename} ${install_dir}/${Base_Mysql}_${server_port}
# 创建配置目录和数据目录
mkdir -p ${install_dir}/${Base_Mysql}_${server_port}/{data,conf}
# 生成配置文件
echo -e "[ 生成配置 ]:${install_dir}/${Base_Mysql}_${server_port}/conf/my.cnf "
if [ $(echo ${Bin_Basename} | awk -F "[-|.]" '{print$2}') -eq 5 ];then
cat > ${install_dir}/${Base_Mysql}_${server_port}/conf/my.cnf << EOF
[client]
default-character-set = utf8mb4
[mysql]
port = ${server_port}
socket = ${install_dir}/${Base_Mysql}_${server_port}/mysql.sock
default-character-set = utf8mb4
[mysqld]
port = ${server_port}
default_storage_engine = InnoDB
basedir = ${install_dir}/${Base_Mysql}_${server_port}
datadir = ${install_dir}/${Base_Mysql}_${server_port}/data
socket = ${install_dir}/${Base_Mysql}_${server_port}/mysql.sock
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
max_connections = 2000
max_allowed_packet = 128M
innodb_file_per_table = 1
tmp_table_size = 134217728
max_heap_table_size = 134217728
lower_case_table_names = 1
log-bin = mysql-bin
max_binlog_size = 1024M
expire_logs_days = 1
log_slave_updates = 1
server-id = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
elif [ $(echo ${Bin_Basename} | awk -F "[-|.]" '{print$2}') -eq 8 ];then
cat > ${install_dir}/${Base_Mysql}_${server_port}/conf/my.cnf << EOF
[mysql]
# 默认字符集
default-character-set=utf8mb4
[client]
#修改默认端口号
port = ${server_port}
socket = ${install_dir}/${Base_Mysql}_${server_port}/mysql.sock
[mysqld]
#修改默认端口号
port = ${server_port}
server-id = ${server_port}
user = mysql
socket = ${install_dir}/${Base_Mysql}_${server_port}/mysql.sock
# 安装目录
basedir = ${install_dir}/${Base_Mysql}_${server_port}
# 数据存放目录
datadir = ${install_dir}/${Base_Mysql}_${server_port}/data
log-bin = ${install_dir}/${Base_Mysql}_${server_port}/data/mysql-bin
innodb_data_home_dir =${install_dir}/${Base_Mysql}_${server_port}/data
innodb_log_group_home_dir =${install_dir}/${Base_Mysql}_${server_port}/data
#日志及进程数据的存放目录
log-error =${install_dir}/${Base_Mysql}_${server_port}/mysql.log
pid-file =${install_dir}/${Base_Mysql}_${server_port}/mysql.pid
# 服务端使用的字符集默认为8比特编码
character-set-server=utf8mb4
#不区分大小写
lower_case_table_names=1
autocommit =1
##################以上要修改的########################
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
#query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
binlog_format=mixed
binlog_expire_logs_seconds =864000
# 创建新表时将使用的默认存储引擎
default_storage_engine = InnoDB
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF
fi
# 创建运行账号
if ! id mysql >/dev/null 2>&1;then
useradd mysql
fi
# 授权相关目录权限
chown -R mysql.mysql ${install_dir}
# 写入环境变量
echo -e "[ 环境变量 ]:/etc/bashrc"
if [ $(grep -w "${install_dir}/${Base_Mysql}_${server_port}/bin" /etc/bashrc | wc -l) -eq 0 ];then
echo -e "export PATH=${install_dir}/${Base_Mysql}_${server_port}/bin:\$PATH" >> /etc/bashrc
fi
# 环境变量生效
source /etc/bashrc
# 软连接sock文件
[ -L /tmp/mysql_${server_port}.sock ] && rm -f /tmp/mysql_${server_port}.sock
ln -s ${install_dir}/${Base_Mysql}_${server_port}/mysql.sock /tmp/mysql_${server_port}.sock
# 初始化 mysql
echo -e "[ 初始服务 ]: ${install_dir}/${Base_Mysql}_${server_port}/bin/mysqld --defaults-file=${install_dir}/${Base_Mysql}_${server_port}/conf/my.cnf --user=mysql --initialize-insecure > /dev/null 2>&1 &"
${install_dir}/${Base_Mysql}_${server_port}/bin/mysqld --defaults-file=${install_dir}/${Base_Mysql}_${server_port}/conf/my.cnf --user=mysql --initialize-insecure > /dev/null 2>&1 &
# 启动mysql
sleep 10
echo -e "[ 启动mysql]: ${install_dir}/${Base_Mysql}_${server_port}/bin/mysqld_safe --defaults-file=${install_dir}/${Base_Mysql}_${server_port}/conf/my.cnf --user=mysql &"
${install_dir}/${Base_Mysql}_${server_port}/bin/mysqld_safe --defaults-file=${install_dir}/${Base_Mysql}_${server_port}/conf/my.cnf --user=mysql > /dev/null 2>&1 &
# 免密登录
sleep 5
echo -e "[ 部署完成 ]: 请执行: ${install_dir}/${Base_Mysql}_${server_port}/bin/mysql -uroot -p --socket=${install_dir}/${Base_Mysql}_${server_port}/mysql.sock 登录数据库修改密码(初次登录无密码)!"
echo -e "[ 使用建议 ]:
1. 修改root密码 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '594hxs';FLUSH PRIVILEGES;
2. 远程root登录 CREATE USER 'root'@'%' IDENTIFIED BY '594hxs';GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';FLUSH PRIVILEGES;
3. 创建数据库 CREATE DATABASE db0 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;FLUSH PRIVILEGES;
4. 创建用户 CREATE USER 'user0'@'%' IDENTIFIED BY '594hxs';FLUSH PRIVILEGES;
5. 用户授权 GRANT ALL PRIVILEGES ON db0.* TO 'user0'@'%';FLUSH PRIVILEGES;"
fi
}
if [[ $# -eq 3 ]];then
install_dir=${1}
server_port=${2}
BintarFile_or_DownloadURL=${3}
mysql_install ${install_dir} ${server_port} "${BintarFile_or_DownloadURL}"
else
echo -e "[ 使用方法 ]:$0 \"[安装位置{字符串}]\" \"端口\" \"[下载二进制的URL地址 | 二进制压缩包绝对路径]\""
fi
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/hxsaj/tools_shell.git
git@gitee.com:hxsaj/tools_shell.git
hxsaj
tools_shell
tools_shell
master

搜索帮助