0

I am trying to make my own mail service using Postfix, but I run into an issue in the setup phase, and it's that postfix can't connect to the database. Here's what the error log says:

⛔Database connection string : mysql:host=localhost;dbname=postfixadmindb;charset=UTF8
⛔Problem connecting to database, check database configuration ($CONF['database_*'] entries in config.local.php)
⛔SQLSTATE[HY000] [2002] Permission denied

Here's the config file:

<?php
        $CONF['database_type'] = 'mysqli';
        $CONF['database_host'] = 'localhost';
        $CONF['database_user'] = 'postfixuser';
        $CONF['database_password'] = 'XXXXXXXX';
        $CONF['database_name'] = 'postfixadmindb';
        $CONF['configured'] = true;
        $CONF['encrypt'] = 'md5crypt';
        $CONF['setup_password'] = 'the setup password';
?>

First of all, I tried connecting to the database normally:
sudo mysql -u postfixuser -p'thecorrectpassword' -h localhost postfixadmindb
This works fine and brings me tho the MariaDB shell as expected
I then granted postfixuser all privileges in the database, but that didn't solve the issue.
I checked the log under /var/log/mysql/error.log but it's empty

I'm doing this on a WSL machine running Ubuntu 22.04.1 LTS, serving the postfixadmin web pages with apache.

  • 1
    Running `sudo mysql -u postf..` will run the mysql commandline client with root privileges and is an insufficient check to see if an unprivileged user can also connect to the mysql socket. Try running that again without `sudo` and if that fails there may be a file system permission problem prevent the postfix user from connecting to the database via the MariaDB socket. – HBruijn Mar 28 '23 at 13:16
  • Thanks for the reply, I just checked and running it without `sudo` does indeed fail: `mysql -u postfixuser -p'XXXX' -h localhost postfixadmindb >ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (13)` – swegbarca Mar 28 '23 at 13:35
  • I solved the problem by granting read and execute permissions to all users on the /run/mysqld directory – swegbarca Mar 28 '23 at 14:31
  • Converted my comment to an answer – HBruijn Mar 28 '23 at 15:05

1 Answers1

0

Running sudo mysql -u postf.. will run the mysql command-line client with root privileges and is an insufficient check to see if an unprivileged user can also connect to the mysql socket.

Try running that again without sudo and if that fails with an error such as Can't connect to local server through socket '/run/mysqld/mysqld.sock'there may be a file system permission problem that prevents the postfix user from connecting to the database via the MariaDB socket.

Changing the file system permissions on that directory/path is then the solution.

sudo chmod 0755  /run/mysqld/

It should be noted that directories under /run and/or /var/run are often created at boot time by systemd, in this case by the MySQL/MariaDB service unit.

You may need to edit the systemd unit file to make that change in permissions persistent.
If the RuntimeDirectory directive is used you would need to set the correct permissions with the RuntimeDirectoryMode=0755 directive.

If tempfiles.d is used, edit the spec for that

HBruijn
  • 77,029
  • 24
  • 135
  • 201