Step-by-Step Shell Scripts for Deploying MySQL and Creating Users on a Linux Node
This article provides a detailed shell-script tutorial for installing MySQL 5.7 on a Linux node, initializing the database, setting the root password, and automatically creating multiple user accounts with random passwords, suitable for operations and database administrators. The guide includes full script listings, explains repository configuration, service management, and error handling, making it a practical reference for DevOps engineers.
This guide walks through two Bash scripts that automate the installation of MySQL 5.7 on a Linux host and the creation of a batch of user accounts with randomly generated passwords.
The first script, mysql_install.sh , checks whether the MySQL package is already installed, adds the official MySQL repository if needed, installs the server, starts the service, extracts the temporary root password from the log, and then sets a permanent root password.
#!/usr/bin/bash
#1.检查是否存在mysql对应的软件包
rpm_install_mysql(){
rpm_check_mysql=$(rpm -qa|grep mysql-community-server|wc -l)
rpm_check_mysql_version=$(rpm -qa|grep mysql-community-server)
if [ $rpm_check_mysql -eq 0 ];then
cat >/etc/yum.repos.d/mysql.repo<<-EOF
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
EOF
yum clean all && yum makecache
yum install -y mysql-community-server
else
echo "${rpm_check_mysql_version%-*} Installed Ok"
fi
}
#2.初始化MySQL数据库
clear_mysql(){
systemctl start mysqld
if [ $? -eq 0 ];then
Mysql_old_pass=$(grep "temporary password" /var/log/mysqld.log |tail -n1|awk -F '[ :]' '{print $NF}')
mysqladmin -uroot -p"$Mysql_old_pass" password "hahashen.com"
fi
}
rpm_install_mysql
clear_mysqlThe second script, user_passwd.sh , verifies it runs as root, then iterates from user_00 to user_10 . For each iteration it generates a random 10‑character password, creates the user if it does not already exist, sets the password, and records the username and password in /tmp/user.txt .
#!/usr/bin/bash
if [ $UID -ne 0 ];then
echo "请使用root账户执行创建用户"
exit 1
fi
for i in $(seq -w 00 10)
do
username=user_$i
pass=$(mkpasswd -l 10 -C 3 -c 3 -d 2 -s 2)
id $username &>/dev/null
if [ $? -eq 1 ];then
useradd $username && \
echo "$pass"|passwd --stdin $username &>/dev/null
id $username &>/dev/null
if [ $? -eq 0 ];then
echo "Useradd $username is OK"
else
echo "Useradd $username is Err"
fi
else
echo "Useradd $username is already exists"
fi
echo -e "$username \t $pass" >> /tmp/user.txt
doneThese scripts provide a reproducible, hands‑on method for setting up a MySQL service and populating it with test users, which can be adapted for larger‑scale deployments or CI/CD pipelines.
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.