5

Postfix is throwing up an error saying:

Jul 9 14:41:26 tmail postfix/trivial-rewrite[4342]: warning: connect to mysql server unix:/var/run/mysqld/mysqld.sock: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Jul 9 14:41:26 tmail postfix/trivial-rewrite[4342]: fatal: mysql:/etc/postfix/mysql-yaa-aliases.cf(0,lock|fold_fix): table lookup problem

In main.cf I have:

virtual_alias_maps = mysql:/etc/postfix/mysql-yaa-aliases.cf

In mysql-yaa-aliases.cf I have:

hosts = unix:/var/run/mysqld/mysqld.sock
# hosts = 127.0.0.1
user = yaa_admin
password = ********
dbname = yaa
query = SELECT dest FROM yaa_active_aliases WHERE address='%s'

If I use 127.0.0.1 it works (not ideal as I would prefer the speed of a socket.) Also if I test the socket with postmap it works:

# postmap -q "bholly@example.org" mysql:/etc/postfix/mysql-yaa-aliases.cf
bholly@example.org, bholly@autoreply.example.org

When I use strace on the trivial-rewrite daemon I get:

connect(11, {sa_family=AF_FILE, path="/var/run/mysqld/mysqld.sock"}, 110) = -1 ENOENT (No such file or directory)

Whereas strace using postmap I get:

connect(4, {sa_family=AF_FILE, path="/var/run/mysqld/mysqld.sock"}, 110) = 0

BTW - Currently running Postfix 2.7, but the fault still shows with 2.9

ΣΚΟΤ
  • 354
  • 2
  • 8

1 Answers1

13

FOUND IT :) - trivial-rewrite takes place in a chroot jail, therefore the socket file REALLY doesn't exist.

The solution (thanks to https://serverfault.com/questions/229389 for helping me with this) is to add this to the /etc/fstab:

/var/run/mysqld /var/spool/postfix/var/run/mysqld bind defaults,bind 0 0

Thus allowing Postfix to keep its chroot config, and giving it access to the mysql socket.

Community
  • 1
  • 1
ΣΚΟΤ
  • 354
  • 2
  • 8
  • 8
    How about using as `host` value `127.0.0.1`? It helped me to solve this issue. It uses TCP connection which doesn't write socket (unlike `localhost` value on unix). – pevik Sep 18 '13 at 21:06
  • I think using a TCP connection is a better solution. Would be worthy of being a separate answer to this question. EDIT: I see, the question's closed. Oh well, I got my answer ;-) – Andy Beverley Mar 06 '16 at 12:10
  • @pevik I am trying to use 127.0.0.1 but still get the error Can't connect to MySQL server on . Any pointers? – coder006 May 25 '16 at 18:04
  • @pevik Very old comments, but worth answering. When both methods are possible, unix sockets are always preferable. There was a time when it would be considered wrong to use TCPIP over unix sockets for a local connection. Today CPUs are fast enough that more people use that shortcut. But engaging the TCP/IP stack for local connections is still wasteful and even with some localhost shortcuts in the system still adds significant overhead. https://jasonbarnabe.wordpress.com/2014/10/01/mysql-connections-sockets-vs-tcp/ – Kurt Fitzner May 08 '21 at 17:02