Linux - MySQL and MariaDB sock file folder issues - how to fix How to force MySQL and MariaDB services to create the /var/run/mysqld/mysqld.sock and /var/run/mariadb/mariadb.sock files and folders

MySQL Master-Master Replication setup in 5 easy steps

Yesterday I wrote a small tutorial explaining how to fix a common MySQL or MariaDB problem related to the pid file not being created upon the service startup. That same tutorial, with some key differences, can be used to force the MySQL or MariaDB service to create their mysqld.sock or mariadb.sock file to allow socket connections.

Check the my.cnf file

The first thing to do is to check our /etc/my.cnf file to see if the socket attribute is present:

Notice that we can either use /var/run/mysqld/mysqld.sock (as shown above) or /var/run/mariadb/mariadb.sock, 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:

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.sock or mariadb.sock 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 sock 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 sock 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
Needless to say, we need to use mysqld or mariadb  depending on the path we've chosen early on.

After reboot, mysql should find the /var/run/mysqld/ folder already there, thus being able to create the sock 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 sock file issue for good.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.