Web888.vn
  • Shop
  • Blog
Đăng nhập
  • Đăng nhập / Đăng ký

Please enter key search to display results.

Home
  • Direct Admin
  • operating system knowledges
  • Linux server
  • Technology News
Instructions for using cronjob to schedule automatic restart of Mysql when the server is overloaded

Instructions for using cronjob to schedule automatic restart of Mysql when the server is overloaded

  • 24-07-2022
  • Toanngo92
  • 0 Comments

Mục lục

  • 1. Restart Mysql manually when overloaded
  • 2. Create a script (Bash script) to restart MySQL
  • 3. Check if the script file you just created is working properly
  • 4. Automate script file execution with cron

1. Restart Mysql manually when overloaded

MySQL may have problems when it runs out of memory or because of some other problem in my case when the server runs many different sites. In most cases, we can solve this problem by manually restarting the MySQL service.

To check if MySQL is running, run the command:

 sudo service mysql status // kiểm tra trạng thái mysql

Or:

 sudo service mysqld status // kiểm tra trạng thái nếu sử dụng mariadb

If Mysql state is off, restart using command line:

 sudo service mysql restart // hoặc sudo service mysqld restart

If you do it manually, it's simple, but if the database is overloaded in the middle of the night, when we are sleeping, the website access will be interrupted, that's the problem, so how to solve the problem this ?

With linux/Centos, the operating system provides us with the Cron feature, which is a time-based automatic scripting job scheduler for Linux so that it can run the scripts without us manipulating them. crafts again.

2. Create a script (Bash script) to restart MySQL

You can configure cron to automatically check the status of the MySQL server and restart it if it crashes. Of course, this isn't a permanent fix for your dingy MySQL server, but it can save you time until you can investigate further.

First, we need to create a simple BASH script and store it in the home directory (or wherever you want), then instruct cron to run this BASH script once a minute.

You can put this script anywhere, but in this example, we will put it in the root directory. or, you can use bitvise to access the server and create a folder that simulates your command line, next, create a folder called scripts

 cd /root/ // change directory to root folder sudo mkdir scripts // make dir scripts with root permission
use bitvise to access root directory

Go to scripts folder and create mysqlmon file with nano tool (data editor on linux terminal), or use bitvise to create this file

 cd scripts sudo nano mysqlmon.sh
Use bitvise to create mysqlmon.sh

Insert this code into the newly created mysqlmon.sh file depending on whether your service is mysql or mysqld:

 #!/bin/bash # Check if MySQL is running sudo service mysql status > /dev/null 2>&1 # Restart the MySQL service if it's not running. if [ $? != 0 ]; then echo -e "MySQL Service was down. Restarting now...n" sudo service mysql restart else echo -e "MySQL Service is running already. Nothing to do here.n" fi // sử dụng 1 trong 2 đoạn code này phù hợp server của bạn, chỉ khác nhau mysql và mysqld #!/bin/bash # Check if MySQL is running sudo service mysqld status > /dev/null 2>&1 # Restart the MySQL service if it's not running. if [ $? != 0 ]; then echo -e "MySQL Service was down. Restarting now...n" sudo service mysqld restart else echo -e "MySQL Service is running already. Nothing to do here.n" fi

If using nano, save and exit (press Ctrl + X , press Y(yes) then press enter)

Grant execute permission to the script:

 sudo chmod +x mysqlmon.sh

3. Check if the script file you just created is working properly

Run this command to execute the newly created script file:

 sudo ./mysqlmon.sh

If the Mysql service is still running, the terminal will display the line:

 MySQL Service is running already. Nothing to do here.

If you want to see what happens when the script detects if MySQL crashes, stop the MySQL service, keep in mind this step, your server will temporarily crash due to not being able to load the database:

 sudo service mysql stop

After stopping, try running the mysqlmon.sh file you just created again to see if it recognizes and restarts mysql:

 sudo ./mysqlmon.sh

If run correctly, this line of text will be displayed

 MySQL Service was down. Restarting now..

Check the status one last time to see if it was successful:

 sudo service mysql status

4. Automate script file execution with cron

Create cron files with nano or bitvise:

 cd ~ cd /etc/cron.d // chuyển tơi thư mục etc/cron.d nano -w auto_restart // khởi tạo file auto_restart với trình biên tập nano

Paste this command line into the file auto_restart

 */1 * * * * root /root/scripts/mysqlmon.sh >/dev/null 2>&1 // chạy file này 1 phút 1 lần, tăng số 1 lên nhiều nếu bạn muốn thời gian tự động chạy lâu hơn
auto_restart file displayed in cron.d

Save the cron file to finish.

Note, when using this way, you won't be able to find the cron command with the command line crontab -l , to see if your command is recognized use the command line:

 head -n -0 /var/spool/cron/* /etc/crontab /etc/cron.d/* // xem các kịch bản tự động chạy trong thư mục cron.d ls /etc/cron.{hourly,daily,weekly,monthly}/ // xem các file cron tự động chạy trong các thư mục cron theo giờ, ngày, tuần, tháng ...

Good luck !

Bài viết liên quan:

Installing PHP and introducing PHP basics
Instructions for using the plugin wp bakery builder (visual composer) to make a landing page
Instructions for installing and activating Office 2013
15 pocket tips in daily life you should know
What is WordPress and why should learn about wordpress?
Install composer use for Xampp on Windows
Guide to debug (debug) when programming C/C++ with Dev C++
Binary integer representation and binary operations
Create Gmail STMP and install for WordPress, Laravel, php…
Install and enable fileinfo extension in PHP 7 linux (centos,ubuntu)
The thing that limits your growth is the relationship you have
10 groups of customers that business people often encounter and how to behave professionally

THÊM BÌNH LUẬN Cancel reply

Dịch vụ thiết kế Wesbite

NỘI DUNG MỚI CẬP NHẬT

Step-by-step guide to installing Microsoft SQL Server and connecting to the database for beginners

Reasons Why People Are Increasingly Inexperienced in Life

PHP Data Types

Table (table) in SQL Server

Basic data types in C

Giới thiệu

web888.vn là chuyên trang chia sẻ và cập nhật tin tức công nghệ, chia sẻ kiến thức, kỹ năng. Chúng tôi rất cảm ơn và mong muốn nhận được nhiều phản hồi để có thể phục vụ quý bạn đọc tốt hơn !

Liên hệ quảng cáo: [email protected]

Kết nối với web888

© web888.vn - Tech888 Co .Ltd since 2019

Đăng nhập

Trở thành một phần của cộng đồng của chúng tôi!
Registration complete. Please check your email.
Đăng nhập bằng google
Đăng kýBạn quên mật khẩu?

Create an account

Welcome! Register for an account
The user name or email address is not correct.

Your personal data will be used to support your experience throughout this website, to manage access to your account, and for other purposes described in our privacy policy.

Registration confirmation will be emailed to you.
Log in Lost your password?

Reset password

Recover your password
Password reset email has been sent.
The email could not be sent. Possible reason: your host may have disabled the mail function.
A password will be e-mailed to you.
Log in Register
×