MySQL Installation and Configuration Bash Script
This article provides a step‑by‑step Bash script for installing, configuring, and starting MySQL 8.0 on a Linux system, including user creation, directory setup, permission handling, initialization checks, and service management, and ensures proper environment variables.
This guide presents a Bash script that automates the installation and configuration of MySQL 8.0.17 on a Linux server.
The script performs the following actions:
#!/bin/bash
set -e
mysql_install () {
tar -zxf mysql-8.0.17-linux-glibc2.12-x86_64.tar.gz && mv mysql-8.0.17-linux-glibc2.12-x86_64 /usr/local/mysql && cd /usr/local/mysql
mysql_pid=`ps -ef |grep mysqld |wc -l`
if [ 1 -eq $mysql_pid ];then
echo "mysql 进程没运行"
else
pkill mysqld
echo "结束 mysql 进程"
fi
#useradd mysql
mysql_user=`cat /etc/passwd |grep mysql|wc -l`
if [ 1 -eq $mysql_user ];then
echo "Mysql 用户存在"
else
echo -e "Mysql 用户不存在,开始添加 mysql 用户"
useradd mysql
echo -e "添加 mysql 用户成功!!!"
fi
mysql_data=/data/mysql/mysql
myslq_logs=/data/mysql/logs
if [ !-d $mysql_data ];then
mkdir /data/mysql/mysql -p
else
echo "mysql 数据目录存在"
cd /data/mysql && rm -rf /data/mysql/mysql
fi
if [ !-d $mysql_logs ];then
mkdir /data/mysql/logs -p
else
echo "mysql 日志目录存在"
cd /data/mysql && rm -rf /data/mysql/logs
fi
chown -R mysql:mysql /data/mysql/ && chown -R mysql:mysql /usr/local/mysql
yes|cp -a /root/my.cnf /etc/my.cnf
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql
cp -r /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/init.d/mysqld
mysql_init=`cat mysql-error.log |grep -i "root@localhost:"|wc -l`
if [ 1 -eq $mysql_init ];then
echo "mysql 初始化成功"
else
echo "mysql 初始化失败"
fi
chkconfig mysqld on && chkconfig --add mysqld
echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile
/etc/init.d/mysqld start
mysql_start=`ps -ef |grep mysql|grep -v grep|grep -v mysql_install.sh|wc -l`
if [ 2 -eq $mysql_start ];then
echo "mysql 启动成功"
else
echo "mysql 启动失败"
fi
}
mysql_installThe script extracts the MySQL binaries, creates a dedicated mysql user if missing, prepares data and log directories, sets appropriate ownership, initializes the database, registers the service, updates the PATH, and finally starts MySQL while verifying the process.
After execution, the MySQL service should be running and ready for use.
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.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
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.
