等待mysql联机,执行命令,并在bash

时间:2016-07-29 13:27:03

标签: mysql bash

我正在开发一个打开mysql的bash脚本,执行一些操作而不是关闭它。

我担心/usr/bin/mysqladmin ping -h 127.0.0.1 --silent不是一种安全的方式。如何确保mysql为ON或OFF以便我可以继续?

P.S。这是docker容器,所以我没有service

if [[ ! -f /var/lib/mysql/status.secured ]]; then
  echo "Starting MariaDB... to secure it!"
  /usr/bin/mysqld_safe
  while ! /usr/bin/mysqladmin ping -h 127.0.0.1 --silent; do
    sleep 1
  done
  mysqladmin -u root password "$DATABASE_PASS"
  mysql -u root -p"$DATABASE_PASS" -e "UPDATE mysql.user SET Password=PASSWORD('$DATABASE_PASS') WHERE User='root'"
  mysql -u root -p"$DATABASE_PASS" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
  mysql -u root -p"$DATABASE_PASS" -e "DELETE FROM mysql.user WHERE User=''"
  mysql -u root -p"$DATABASE_PASS" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
  mysql -u root -p"$DATABASE_PASS" -e "FLUSH PRIVILEGES"
  touch /var/lib/mysql/status.secured
  mysqladmin -u root -p"$DATABASE_PASS" -h 127.0.0.1 --protocol=tcp shutdown
  while /usr/bin/mysqladmin ping -h 127.0.0.1 --silent; do
    sleep 1
  done
fi

我特别担心关机部分,或许最好检查PID是否存在?还是grep ps aux

1 个答案:

答案 0 :(得分:0)

我可以建议两个选项:

使用mysqladmin并检查退出状态

mysqladmin -h server_ip -u username -p passsword processlist
if [ $? -ne 0 ] then; ... system down

使用grep并检查流程mysql

STATUS=$(grep mysql | wc -l);
if [ "STATUS" -ne 1 ] then; ... system down