Table of Contents
If you've stumbled upon this post it most likely means that you've just discovered that your MySQL or MariaDB service, despite running without issues, are not creating their mysqld.pid or mariadb.pid file - which you need to setup monit or another similar monitoring/tracking service.
I'm writing this post because it often happens to me as well, therefore I thought it would be wise to write a small tutorial about such common issue.
Check the my.cnf file
The first thing to do is to check our /etc/my.cnf file to see if the pid-file attribute is present:
pid-file = /var/run/mysqld/mysqld.pid
Notice that we can either use /var/run/mysqld/mysqld.pid (as shown above) or /var/run/mariadb/mariadb.pid, as long as such path will be consistant with what we'll do in the next steps.
Check the /var/run/ folder
The next thing to do is to check your machine's /var/run/ folder. Ideally, we should be able to see either a /mysqld/ folder or a /mariadb/ folder in there (depending on the path we've chosen in the previous step): if there's nothing like that we need to create it and give the proper rights to mysql user and group. This can be done with the following commands:
chown mysql:mysql /var/run/mysqld
Right after that, we can try to restart the mysqld / mariadb service and see what happens inside that folder. If everything has been done properly, we should be able to see the mysqld.pid or mariadb.pid file.
That's great! However, there's still something to check before we could call it done.
Reboot the machine
Now that our MySQL or MariaDB pid file is up and running, we need to ensure that our machine will be able to recreate it - together with its containing folder - upon each startup. The best thing we can do to check that is to reboot the machine using a sudo reboot command and see what happens.
- If the MySQL / MariaDB service starts up with no issues, we can positively say that we're done!
- If the service doesn't startup automatically, don't worry and keep reading.
Creating the tmp folders
As we might already know, the /var/run/ files and folders are created by the system upon each reboot. Depending on your configuration, your machine could be unable to create the /run/mysqld or /run/mariadb folder, thus preventing the service to create the pid file (due to that missing folder) and properly run: this frequently happens if the mariadb / mysqld startup script is invoked with non-elevated permissions - such as user: mysql and group:mysql - while the /run/ folder is only writable by user:root.
The best thing we can do to fix that without altering the existing permissions is to force systemd to create such folder on each startup. We can easily do that following the steps below:
- Create a new /etc/tmpfiles.d/mysql.conf file.
- Paste the following content, replacing
# systemd tmpfile settings for mysql or mariadb
d /var/run/mysqld 0755 mysql mysql -
After reboot, mysql should find the /var/run/mysqld/ folder already there, thus being able to create the pid file and start without issues.
That's it! I hope that this small tutorial will help other System Administrators looking for a way to fix the MySQL / MariaDB pid file issue for good.