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:
1 2 |
[mysqld] 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:
1 2 |
mkdir /var/run/mysqld 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
1 2 |
# 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.
Conclusion
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.
Perfect man! Thank you for this solution.
Glad you found our post useful :)
Thanks for this! I’ve been looking for such a solution!
Quick question… for your /etc/tmpfiles.d/mariadb.conf solution to work does mysql need to have a user account in /etc/passwd? This was suggested to me. Currently with my setup mysql only exists in /etc/group. I work in a big organisation and the reboots only happen once per month, so I can’t quickly test the solution. Thanks in advance for any help!
Hello Kmat, thanks for your kind feedback! If you liked our post, feel free to like us on FB/Twitter :)
Regarding your question, the answer is yes: mysql does install its user, therefore such user will also have an entry in the /etc/passwd file. You can easily check it out with a
$ grep mysql /etc/passwd
right after the install.
While we’re at it, it’s worth nothing that the default MySQL behaviour in /etc/passwd arguably enforces a (minor) security issue which is well explained here:
https://bugzilla.redhat.com/show_bug.cgi?id=176389
Basically, the mysql user has bash permissions instead of nologin permissions. However, since the mysql account is created with no password, no one can perform a login using that account unless there is a password change, therefore such setting is almost always accepted the way it is.
Hi Ryan, thank you so much for your quick reply! Thanks for confirming that I’ll need to have a mysql user account in existence on the server. Currently my passwd file doesn’t have any entry for mysql so I’ll have to add it myself manually.
I’m hoping that adding the mysql user account, along the your /etc/tmpfiles.d/mysql.conf solution will solve all my problems!