September 7, 2015

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

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.

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

Trên VPS Centos 
Centos 6 (MariaDB cũng dùng chung lệnh) :
Hoặc:
Centos 7
MySQL:
MariaDB:
 Trên VPS Ubuntu
Hoặc
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

2. 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:
Để thêm  vào VPS crontab tự động chạy auto-start-mysql 5 phút 1 lần  ta dùng lệnh:
Nội dung của  file auto-start-mysql  như sau:
Trên VPS Centos
Centos 6:
Centos 7:
MySQL:
MariaDB:
 Trên VPS Ubuntu
MySQL:
MariaDB:

Related Posts:

  • How to Change the Location of MySQL on cPanel There may be some situations where you have to move the location of MySQL, for example, if you’re out of disk space or perhaps looking to host it on another device to increase performance. Whatever the reason, moving MySQL i… Read More
  • Installing Custom MySQL Versions If at all possible, you should use WHM to install MySQL, however, if you need a version of MySQL outside of what they are offering in current cPanel versions, it is possible to install your own versions. These instructions … Read More
  • Chạy lệnh mysql không cần password Thủ thuật này dành cho những người lười thôi. Còn nếu bạn lo ngại đến vấn đề bảo mật thì tốt nhất không nên sử dụng. Tạo file nano ~/.my.cnf rồi điền thông tin MySQL username và password tương ứng: [client] u… Read More
  • Disk Space for MySQL Databases in cPanel Show as 0MB When you upgrade from cPanel 11.24 to 11.25, your users may notice that in cPanel, their MySQL databases show 0MB of disk space used, when they know their databases are much larger. This is due to an option in cPanel that yo… Read More
  • Restoring Database Privileges I’ve seen several cases in the last few months where database users seemingly “disappear” or privileges appear to become invalid when restoring MySQL data or an entire server from raw backups. This may commonly be attributed… Read More

0 comments:

Post a Comment