MySQL
mysqld
database management
server shutdown
tutorial

How to stop mysqld

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Stopping the `mysqld` service is an essential system administration task for managing a MySQL database server. Whether for maintenance, troubleshooting, or upgrading MySQL, there are several methods to safely halt the `mysqld` process. This article will delve into various techniques, explaining the technical details and considerations for each approach.

Reasons for Stopping MySQL

  • Routine Maintenance: Periodic updates or server hardware maintenance.
  • Restarting for Configuration Changes: Applying updates to MySQL configuration files (`my.cnf`).
  • Troubleshooting: Diagnosing errors or performance issues that require a restart.
  • Upgrading MySQL: Before upgrading, you must safely shut down your instance to ensure no data corruption.

Methods to Stop `mysqld`

1. Using `systemctl`

`systemctl` is commonly used to manage services in systems with `systemd`. To stop `mysqld` using `systemctl`, follow this command:

  • Advantages:
    • Integrates smoothly with Linux distributions that use `systemd`.
    • Handles dependencies and ensures the process is terminated properly.
  • Example:
    • Invoke the command in the terminal; systemd will internally send a SIGTERM signal to the `mysqld` process, initiating a graceful shutdown sequence.
  • Advantages:
    • Compatible with a wide range of Linux distributions that utilize `init.d`.
  • Drawbacks:
    • May not be available on modern systems that have transitioned to `systemd`.
  • Procedure:
    • Use the `cat` command to read the process ID from the `mysqld.pid` file.
    • `kill -TERM` initiates a clean shutdown.
  • Considerations:
    • Ensure the PID file path is correct for your specific configuration.
    • A forced kill (`kill -9`) might lead to data corruption if used improperly.
  • Advantages:
    • Ensures a clean shutdown through MySQL's internal mechanisms.
    • Can authenticate using MySQL user credentials.
  • Security:
    • Requires `root` or an administrative MySQL user's credentials.
    • Prompts for a password unless provided with the `-pPASSWORD` flag.
  • Data Integrity: Always prefer methods that allow MySQL to gracefully terminate and ensure all pending transactions are committed.
  • Security: Secure user credentials and ensure that only authorized personnel can stop the MySQL service.
  • If `mysqld` does not stop or hangs, verify that no ongoing transactions are causing the delay.
  • Check error logs located typically at `/var/log/mysql/error.log` for any warnings or errors.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.