Failed to restart mysqld.service: Unit not found.

Introduction

When working with MySQL, you may encounter an error message like "Failed to restart mysqld.service: Unit not found." This error usually occurs when you try to restart the MySQL service, but the underlying service unit file is missing or misconfigured. In this article, we will explore the possible causes of this error and provide step-by-step solutions to fix it.

Possible Causes

There can be several reasons why you might encounter the "Failed to restart mysqld.service: Unit not found." error:

  1. Missing MySQL installation: If MySQL is not installed on your system, attempting to restart the service will result in this error message. Make sure you have a valid MySQL installation before proceeding.

  2. Misconfigured service unit file: The service unit file responsible for starting and restarting MySQL might be misconfigured or missing. The unit file typically resides in the /etc/systemd/system/ directory.

  3. Incomplete installation: If the installation process was interrupted or not completed successfully, it could result in missing or improperly configured unit files.

Solution

To resolve the "Failed to restart mysqld.service: Unit not found." error, follow these steps:

Step 1: Check MySQL Installation

Ensure that MySQL is properly installed on your system. You can verify this by running the following command in your terminal:

mysql --version

If MySQL is not installed, you need to install it before proceeding further. The installation steps may vary depending on your operating system. Refer to the official MySQL documentation for detailed installation instructions.

Step 2: Locate the MySQL Service Unit File

The MySQL service unit file is responsible for starting and restarting the MySQL service. By default, the unit file is named mysqld.service and resides in the /etc/systemd/system/ directory. Run the following command to locate the unit file:

ls /etc/systemd/system/mysqld.service

If the unit file is missing or not found, it indicates a misconfiguration or an incomplete installation. In such cases, you may need to reinstall MySQL or manually create the unit file.

Step 3: Reinstall MySQL (if necessary)

If the MySQL unit file is missing or you suspect an incomplete installation, it is recommended to reinstall MySQL. The exact steps depend on your operating system and package manager. For example, on Ubuntu, you can reinstall MySQL using the following commands:

sudo apt-get remove --purge mysql-server
sudo apt-get install mysql-server

Step 4: Manually Create the MySQL Unit File

If reinstalling MySQL did not resolve the issue, you may need to manually create the MySQL service unit file. Follow these steps:

  1. Open a text editor and create a new file named mysqld.service. Ensure you have appropriate permissions to create files in the /etc/systemd/system/ directory.

  2. Add the following content to the mysqld.service file:

[Unit]
Description=MySQL Database Server
After=network.target

[Service]
ExecStart=/usr/sbin/mysqld
User=mysql
Group=mysql
Restart=always

[Install]
WantedBy=multi-user.target
  1. Save the file and exit the text editor.

  2. Finally, run the following command to reload the systemd daemon:

sudo systemctl daemon-reload

Step 5: Restart MySQL Service

After creating or fixing the MySQL service unit file, you can now restart the MySQL service:

sudo systemctl restart mysqld.service

If everything is configured correctly, the service should restart without any errors. You can check the service status using the following command:

sudo systemctl status mysqld.service

Conclusion

The "Failed to restart mysqld.service: Unit not found." error can occur due to missing or misconfigured MySQL service unit files. In this article, we discussed the possible causes of the error and provided step-by-step solutions to resolve it. By following these instructions, you should be able to restart the MySQL service successfully. Remember to always double-check your installation and verify the unit file configuration to avoid such errors.