date: 2020-08-11title: mysql-5.7.23安装脚本 #标题
tags: mysql #标签
categories: shell # 分类
记录下mysql二进制离线部署脚本。
# tree install-mysql-5.7.23 # 目录结构install-mysql-5.7.23├── install.log # 脚本安装过程中产生的日志├── install-mysql-5.7.23.sh # 安装脚本├── libaio-0.3.109-13.el7.x86_64.rpm # 某些系统需要此rpm包├── my.cnf # mysql主配置文件└── mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz # mysql二进制包# cat install-mysql-5.7.23.sh # 安装脚本内容如下#!/usr/bin/env bashinit_mysql_root_passwd='cF!TSadGT6y%'tar_pkg="mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz"cur_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"mysql_home=/app/usr/mysql-5.7.23/mysql_3306=${mysql_home}mysql-3306/mysql_data=${mysql_3306}datainstall_log=${cur_dir}/install.logitem=1h2() {printf "\n${underline}${bold}${white}%s${reset}\n" "$@"}h2 "[Step $item]: 确认执行此脚本用户是否为root..."; let item+=1if [[ ${USER} != root ]];thenecho -e "\033[31m此脚本必须使用 root 执行当前用户为 ${USER}正在退出脚本...\033[0m"sleep 1 && exit 1fimkdir -p ${mysql_3306}/{data,logs}id mysql &> /dev/null || useradd -M -s /sbin/nologin mysql &>> ${install_log}set -eh2 "[Step $item]: 解压..."; let item+=1cd ${cur_dir}rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm &>> ${install_log}tar zxf ${tar_pkg} -C ${mysql_home} --strip-components=1cp ${cur_dir}/my.cnf ${mysql_3306}h2 "[Step $item]: 配置环境变量..."; let item+=1cat >> /etc/profile <<EOFPATH=${mysql_home}bin/:\$PATHEOFsource /etc/profileh2 "[Step $item]: 初始化..."; let item+=1mysqld --initialize --user=mysql --datadir=${mysql_data} --basedir=${mysql_home} &>> ${install_log}tmp_passwd=$(grep root@localhost ${install_log} | awk -F 'root@localhost: ' '{print $2}')h2 "[Step $item]: 启动mysql..."; let item+=1mysqld_safe --defaults-file=${mysql_3306}my.cnf &>> ${install_log} &sleep 8# 修改root密码if [[ $(ss -lnput | grep 3306 | wc -l) -ne 0 ]];thenmysqladmin -uroot -p''${tmp_passwd}'' password ''${init_mysql_root_passwd}''echo -e "\033[33mMysql 启动成功, 数据库root密码为: ${init_mysql_root_passwd} ,请妥善保管!请执行指令 source /etc/profile 重新加载环境变量。\033[0m"fi# cat my.cnf # 主配置文件如下[client]port=3306socket=/tmp/mysql.sockdefault-character-set=utf8[mysql]port=3306socket=/tmp/mysql.sockdefault-character-set=utf8[mysqld]port=3306character-set-server=utf8socket=/tmp/mysql.sockbasedir=/app/usr/mysql-5.7.23/datadir=/app/usr/mysql-5.7.23/mysql-3306/dataexplicit_defaults_for_timestamp=truefederatedlower_case_table_names=1secure_file_priv =/app/usr/mysql-5.7.23/mysql-3306back_log=150max_connections=3000max_connect_errors=10table_open_cache=2048external-locking=FALSEsecure_file_priv =max_allowed_packet=32Msort_buffer_size=8Mjoin_buffer_size=8Mthread_cache_size=8query_cache_size=512Mquery_cache_limit=4Mtransaction_isolation=REPEATABLE-READtmp_table_size=96Mmax_heap_table_size=96M###***slow query parameterslong_query_time=1slow_query_log = 1slow_query_log_file=/app/usr/mysql-5.7.23/mysql-3306/logs/slow.log#***MyISAM parameterskey_buffer_size=32Mread_buffer_size=2Mread_rnd_buffer_size=16Mbulk_insert_buffer_size=64Mmyisam_sort_buffer_size = 16Mmyisam_max_sort_file_size = 16Mmyisam_repair_threads = 1skip-name-resolve###***master-slave replication parametersserver-id=14063#slave-skip-errors=all#***Innodb storage engine parametersinnodb_buffer_pool_size=8192Minnodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5Ginnodb_data_file_path=ibdata1:10M:autoextend#innodb_file_io_threads=8#innodb_thread_concurrency=16innodb_flush_log_at_trx_commit=1innodb_log_buffer_size=16Minnodb_log_file_size=128Minnodb_log_files_in_group=3innodb_max_dirty_pages_pct=90innodb_buffer_pool_dump_pct=90innodb_lock_wait_timeout=2innodb_file_per_table=on[mysqldump]quickmax_allowed_packet=32M[myisamchk]key_buffer=16Msort_buffer_size=16Mread_buffer=8Mwrite_buffer=8M[mysqld_safe]open-files-limit=8192
