Comprehensive Guide to MySQL Installation, Configuration, and One‑Click Deployment Script
This article provides a step‑by‑step guide to preparing the operating system, installing and configuring MySQL, tuning system parameters, and using a Bash one‑click deployment script to automate the setup and initialization of a MySQL 5.7.32 instance on Linux.
Good preparation is half the battle; this guide starts with the fundamentals of installing MySQL and continues through best‑practice system tuning, configuration, and automation.
1. MySQL installation and deployment process
Operating system preparation
Disable SELinux and manage security through MySQL itself.
Configure firewalls (firewalld/iptables) – either set rules or disable temporarily.
Set the correct time zone.
For high‑performance machines, increase the network MTU to 9000 and enable Jumbo Frames on the private switch.
Choose XFS for databases with large files and high concurrency; ext4 works well for smaller workloads.
Adjust the I/O scheduler: deadline for HDDs, noop for SSDs.
Tune virtual memory with vm.swappiness to improve MySQL memory usage.
Set resource limits in /etc/security/limits.conf (e.g., nofile, nproc).
Modify kernel parameters such as net.ipv4.tcp* and semaphore settings for InnoDB.
MySQL software installation
Download the official MySQL package (prefer tar.gz for flexibility).
Install required dependencies.
Deploy the tar.gz package to a chosen directory.
Post‑installation configuration
Create a dedicated mysql user and group.
Set up data directories ( data, binlog, dbdata, logs, tmp, undo) with appropriate ownership and permissions.
Write a tailored my.cnf based on hardware resources (buffer pool size, connection limits, InnoDB settings, logging, replication, etc.).
Initialize the data directory and start the MySQL server.
Reset the root password using the temporary password from the error log.
2. One‑click MySQL deployment script
The following Bash script automates the entire process for MySQL 5.7.32 on a Linux host. It checks for root privileges, optimizes file limits, extracts the tar.gz package, creates the MySQL user, prepares directories, generates my.cnf, initializes the database, and resets the root password.
#!/bin/bash
#####MySQL5.7.32数据库自动安装脚本
# Version: 1.0
# Author: kevinCUI
# Date: 2020-12-31
#mysql 安装包的绝对路径,去掉.tar.gz
tarGzPath=/opt/idc/
tarGzFile=mysql-5.7.32-linux-glibc2.12-x86_64
#mysql 安装路径
installPath=/home/mysql/
#my.cnf配置文件
mysqlcnf=/home/mysql/my.cnf
#mysql serverid需要设置唯一的id,比如 ip+3位数字
mysqlServerid=1010101
#mysql 密码(不可擅自修改)
defaultPwd=123456
#mysql 端口
mysqlPort=3306
#mysql数据目录
data_default=${installPath}${mysqlPort}
data_datadir=${data_default}/data
data_binlog=${data_default}/binlog
data_dbdata=${data_default}/dbdata
data_logs=${data_default}/logs
data_tmp=${data_default}/tmp
data_undo=${data_default}/undo
CheckRoot(){
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root to install"
exit 1
fi
clear
}
#优化文件最大打开数
DependFile(){
if [ $( cat /etc/security/limits.conf | grep "mysql" | wc -l ) -lt 1 ] ;then
cat >>/etc/security/limits.conf <<EOF
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
mysql soft nproc 65536
mysql hard nproc 65536
mysql soft nofile 65536
mysql hard nofile 65536
EOF
fi
# additional limit file handling omitted for brevity
}
#拷贝tar.gz包
DecompressionTarGz(){
if [ ! -e ${tarGzPath}${tarGzFile}.tar.gz ];then
echo "${tarGzPath}${tarGzFile}.tar.gz 不存在!请检查后重新执行脚本"
exit 1
fi
if [ ! -d ${installPath}${tarGzFile} ];then
mkdir -p ${installPath}
tar -xvf ${tarGzPath}${tarGzFile}.tar.gz -C ${installPath} &>/dev/null
fi
}
#添加组合角色
AddMysqlUser(){
if ! id -u mysql >/dev/null 2>&1; then
groupadd mysql
useradd -g mysql -r -s /sbin/nologin -M mysql
fi
}
#创建mysql 数据目录
createMysqlFolder(){
mkdir -p ${data_datadir} ${data_binlog} ${data_dbdata} ${data_logs} ${data_tmp} ${data_undo}
chown -R mysql:mysql ${data_default}
chmod 700 ${data_tmp}
}
#创建my.cnf
MakeMyCnf(){
cat >${mysqlcnf} <<EOF
[mysqld_safe]
user = mysql
nice = 0
[client]
socket = ${data_datadir}/mysql.sock
port = ${mysqlPort}
[mysqld]
skip_ssl
skip-name-resolve
character_set_server = utf8mb4
collation_server = utf8mb4_unicode_ci
explicit_defaults_for_timestamp = ON
lower_case_table_names = 1
port = ${mysqlPort}
max_connections = 2000
expire_logs_days = 10
default-time_zone = '+8:00'
interactive_timeout = 600
lock_wait_timeout = 300
max_allowed_packet = 1024M
innodb_buffer_pool_size = 1024M
innodb_flush_method = O_DIRECT
# ... (other settings omitted for brevity)
EOF
}
#初始化数据库
InitDataBase(){
${installPath}${tarGzFile}/bin/mysqld --defaults-file=${mysqlcnf} --basedir=${installPath}${tarGzFile} --datadir=${data_datadir} --user=mysql --initialize
${installPath}${tarGzFile}/bin/mysqld_safe --defaults-file=${mysqlcnf} --user=mysql &
}
#重置密码
ResetPwd(){
sleep 10s
pwd=$(grep "A temporary password is generated for root@localhost:" ${data_logs}/error.log)
pwd=${pwd##*root@localhost:}
pwd=${pwd// /}
${installPath}${tarGzFile}/bin/mysql -uroot -p${pwd} -S ${data_datadir}/mysql.sock --connect-expired-password -e "alter user 'root'@'localhost' identified by '${defaultPwd}';"
}
main(){
CheckRoot
DependFile
DecompressionTarGz
AddMysqlUser
createMysqlFolder
MakeMyCnf
InitDataBase
ResetPwd
}
main3. Summary
A solid start is half the battle; careful planning of MySQL installation and configuration prevents many later issues, and a well‑tuned environment leads to smoother operation and scalability.
Welcome to follow the public WeChat account "Internet Stack Architecture" for more valuable information.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
