Skip to content

Instantly share code, notes, and snippets.

@fengjijiao
Last active May 16, 2022 13:53
Show Gist options
  • Save fengjijiao/fe1b080fe3b83d1b60e598ca88e2430b to your computer and use it in GitHub Desktop.
Save fengjijiao/fe1b080fe3b83d1b60e598ca88e2430b to your computer and use it in GitHub Desktop.
simply to install mysql for centos7
#!/bin/bash
#only test for centos 7
MYSQL_SERIES=8.0
MYSQL_VERSION=8.0.29
MYSQL_DOWNLOAD_URL=https://dev.mysql.com/get/Downloads/MySQL-${MYSQL_SERIES}/mysql-${MYSQL_VERSION}-el7-x86_64.tar.gz
MYSQL_INSTALL_DIR=/usr/local/mysql
MYSQL_SOURCE_DIR="${MYSQL_INSTALL_DIR}-${MYSQL_VERSION}"
MYSQL_DATA_DIR=/var/lib/mysql
MYSQL_LOG_DIR=/var/log/mariadb
DEFAULT_ROOT_PASSWORD=123456789
function check_error() {
if [ $? -ne 0 ]; then
echo $1
remove
exit 1
fi
}
function install() {
yum install -y libncurses*
cat << EOT >/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
EOT
wget -O /tmp/mysql.tar.gz $MYSQL_DOWNLOAD_URL
check_error "download mysql.tar.gz error!"
mkdir $MYSQL_SOURCE_DIR
tar -zxvf /tmp/mysql.tar.gz --strip-components 1 -C $MYSQL_SOURCE_DIR
check_error "decompression failed!"
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
ln -s $MYSQL_SOURCE_DIR $MYSQL_INSTALL_DIR
cd $MYSQL_INSTALL_DIR
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files
bin/mysqld --initialize-insecure --user=mysql
check_error "run initialize failed!"
bin/mysql_ssl_rsa_setup
check_error "run mysql_ssl_rsa_setup failed!"
mkdir $MYSQL_LOG_DIR
touch ${MYSQL_LOG_DIR}/mariadb.log
chown -R mysql:mysql $MYSQL_LOG_DIR
chmod -R 750 $MYSQL_LOG_DIR
bin/mysqld_safe --user=mysql
check_error "run mysqld_safe failed!"
cp support-files/mysql.server /etc/init.d/mysql.server
cat << EOT > /etc/systemd/system/mysql.service
[unit]
Description=mysql
Wants=network.target
After=syslog.target network-online.target
[Service]
User=mysql
Group=mysql
Type=simple
Environment=GOGC=20
ExecStart=/usr/local/mysql/bin/mysqld
Restart=on-failure
RestartSec=10
KillMode=process
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOT
systemctl enable --now mysql
echo "export PATH=\$PATH:${MYSQL_INSTALL_DIR}/bin" >> /root/.bash_profile
source /root/.bash_profile
sleep 3s
mysql -uroot -h127.0.0.1 -e "
tee /tmp/temp.log
ALTER USER 'root'@'localhost' IDENTIFIED BY '${DEFAULT_ROOT_PASSWORD}';
flush privileges;
notee
quit"
bin/mysql_secure_installation -h127.0.0.1 -p${DEFAULT_ROOT_PASSWORD} << EOF
N
N
Y
Y
Y
Y
EOF
rm -rf /tmp/mysql.tar.gz
clear
echo "root password: ${DEFAULT_ROOT_PASSWORD}"
echo "install finished!"
}
function remove() {
CONVERTED_MYSQL_INSTALL_DIR=$(echo $MYSQL_INSTALL_DIR | sed 's/\//\\\//g')
sed -i "/export PATH=\$PATH:${CONVERTED_MYSQL_INSTALL_DIR}\/bin/d" /root/.bash_profile
source /root/.bash_profile
systemctl stop mysql
systemctl disable mysql
rm -rf /etc/systemd/system/mysql.service
rm -rf $MYSQL_LOG_DIR
rm -rf $MYSQL_DATA_DIR
rm -rf $MYSQL_INSTALL_DIR
rm -rf $MYSQL_SOURCE_DIR
userdel mysql
groupdel mysql
clear
echo "remove finished!"
}
function help() {
cat << EOF
mysql install helper!!!
usage:
./mysql.sh menu
install: install mysql
remove: remove mysql
EOF
}
function main() {
if [ $# -lt 1 ]; then
help
else
case $1 in
install)
install
;;
remove)
remove
;;
*)
echo 'command not found!'
help
;;
esac
fi
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment