Get 25 USD Free To USE VPSSIM

Tạo crontab tự động bật lại MySQL khi bị stop trên VPS/Server

Posted on: 31/12/2014 Last Updated: 31/12/2014 15 Comments

Với những VPS có RAM ít và bạn không rõ về config MySQL dẫn tới hiện tượng thi thoảng website quá tải và bị lỗi không thể kết nối tới database. Bài viết này sẽ hướng dẫn bạn cách khắc phục lỗi chết MySQL và tạo crontab  tự động khởi động lại dịch vụ MySQL trên VPS/Server khi  website bị hiển thị lỗi tương tự như dưới:

Error establishing a database connection

This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.

  • Are you sure you have the correct username and password?
  • Are you sure that you have typed the correct hostname?
  • Are you sure that the database server is running?

If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

Lệnh bật lại dịch vụ MySQL

Trên VPS Centos

Centos 6 (MariaDB cũng dùng chung lệnh) :

service mysql start

Hoặc:

/etc/init.d/mysql start

Centos 7

MySQL:

systemctl start mysql.service

MariaDB:

systemctl start mariadb.service

 Trên VPS Ubuntu

/etc/init.d/mysql start

Hoặc

/etc/init.d/mysqld start

Tùy trường hợp cụ thể trên server của bạn là mysql hay mysqld mà bạn chọn lệnh.

tu dong khoi dong lai mysql

Tạo Crontab tự động khởi động (bật ) lại MySQL

Khi service MySQL bị stop trên VPS do quá tải hoặc thiếu RAM, ta sẽ dùng lệnh trên để bật lại. Ngoài ra, bạn có thể tạo một crontab tự động check MySQL nếu dịch vụ này đang bị stop thì tự động bật lại.

Trước tiên cần tạo một file đặt tên là auto-start-mysql chẳng hạn, ta đặt file này trong folder root hoặc folder tùy ý bạn chọn. Sau đó chmod file này bằng lệnh:

chmod +x /root/auto-start-mysql

Để thêm  vào VPS crontab tự động chạy auto-start-mysql 5 phút 1 lần  ta dùng lệnh:

(crontab -u root -l ; echo "*/5 * * * * /root/auto-start-mysql") | crontab -u root -

Xem thêm: Thêm và xóa Crontab trong Cronjob bằng command

Nội dung của  file auto-start-mysql  như sau:

Trên VPS Centos

Centos 6:

if [ ! "$(/sbin/service mysql status | awk 'NR==1 {print $3}')" == "running" ]; then
service mysql start
/etc/init.d/mysql restart
exit
fi

Centos 7:

MySQL:

if [ ! "$(systemctl status  mysql.service | awk 'NR==3 {print $2}')" == "active" ]; then
/bin/systemctl start  mysql.service
exit
fi

MariaDB:

if [ ! "$(/bin/systemctl status  mariadb.service | awk 'NR==3 {print $2}')" == "active" ]; then
systemctl start mariadb.service
exit
fi

 Trên VPS Ubuntu

MySQL:

if [ ! "$(service mysql status | awk 'NR==1 {print $2}')" == "start/running," ]; then 
/etc/init.d/mysql start
exit
fi

MariaDB:

if [ "$(service mysql status | awk 'NR==1 {print $4}')" == "stopped." ]; then 
/etc/init.d/mysql start
exit
fi

Xem thêm:

Chúc bạn thành công.

Bài Viết Liên Quan:
Thẻ:,


Tạm thời mình đóng comment, bạn vui lòng sử dụng diễn đàn để được trả lời nhanh nhất.
Diễn đàn: https://community.vpssim.vn