Home | History | Annotate | Line # | Download | only in html
      1 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"
      2         "https://www.w3.org/TR/html4/loose.dtd">
      3 
      4 <html>
      5 
      6 <head>
      7 
      8 <title>Postfix MySQL/MariaDB Howto</title>
      9 
     10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     11 <link rel='stylesheet' type='text/css' href='postfix-doc.css'>
     12 
     13 </head>
     14 
     15 <body>
     16 
     17 <h1><img src="postfix-logo.jpg" width="203" height="98" ALT="">Postfix MySQL/MariaDB Howto</h1>
     18 
     19 <hr>
     20 
     21 <h2>Introduction</h2>
     22 
     23 <p> The Postfix mysql map type allows you to hook up Postfix to
     24 MySQL or MariaDB databases. This implementation allows for multiple
     25 databases: you can use one for a <a href="virtual.5.html">virtual(5)</a> table, one for an
     26 <a href="access.5.html">access(5)</a> table, and one for an <a href="aliases.5.html">aliases(5)</a> table if you want.  You
     27 can specify multiple servers for the same database, so that Postfix
     28 can switch to a good database server if one goes bad.  </p>
     29 
     30 <p> Even though the component name is 'mysql' in Postfix, MariaDB/MySQL
     31 client and servers can be used interchangeably as the functionality
     32 Postfix relies on is available in both. However, the code bases may
     33 still diverge in the future. </p>
     34 
     35 <p> Busy mail servers using mysql maps will generate lots of
     36 concurrent mysql clients, so the mysql server(s) should be run with
     37 this fact in mind.  You can reduce the number of concurrent mysql
     38 clients by using the Postfix <a href="proxymap.8.html">proxymap(8)</a> service. </p>
     39 
     40 <h2>Building Postfix with MySQL support</h2>
     41 
     42 <p> These instructions assume that you build Postfix from source
     43 code as described in the <a href="INSTALL.html">INSTALL</a> document. Some modification may
     44 be required if you build Postfix from a vendor-specific source
     45 package.  </p>
     46 
     47 <p> Note: to use mysql with Debian GNU/Linux's Postfix, all you
     48 need is to install the postfix-mysql package and you're done.
     49 There is no need to recompile Postfix. </p>
     50 
     51 <p> The Postfix MySQL client utilizes the mysql client library,
     52 which can be obtained from: </p>
     53 
     54 <blockquote>
     55     <p> <a href="https://www.mysql.com/downloads/">https://www.mysql.com/downloads/</a> </p>
     56 </blockquote>
     57 
     58 <p> Many Linux distributions compile Postfix MySQL support using
     59 the equivalent client library from MariaDB: </p>
     60 
     61 <blockquote>
     62     <p> <a href="https://mariadb.org/connector-c/">https://mariadb.org/connector-c/</a> </p>
     63 </blockquote>
     64 
     65 <p> In order to build Postfix with mysql map support, you will need to add
     66 -DHAS_MYSQL and -I for the directory containing the mysql headers, and
     67 the mysqlclient library (and libm) to <a href="MYSQL_README.html">AUXLIBS_MYSQL</a>, for example: </p>
     68 
     69 <blockquote>
     70 <pre>
     71 make -f Makefile.init makefiles \
     72     "CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include" \
     73     "<a href="MYSQL_README.html">AUXLIBS_MYSQL</a>=-L/usr/local/mysql/lib -lmysqlclient -lz -lm"
     74 </pre>
     75 </blockquote>
     76 
     77 <p> If your MySQL shared library is in a directory that the RUN-TIME
     78 linker does not know about, add a "-Wl,-R,/path/to/directory" option after
     79 "-lmysqlclient". </p>
     80 
     81 <p> Postfix versions before 3.0 use AUXLIBS instead of <a href="MYSQL_README.html">AUXLIBS_MYSQL</a>.
     82 With Postfix 3.0 and later, the old AUXLIBS variable still supports
     83 building a statically-loaded MySQL database client, but only the new
     84 <a href="MYSQL_README.html">AUXLIBS_MYSQL</a> variable supports building a dynamically-loaded or 
     85 statically-loaded MySQL database client.  </p>
     86 
     87 <blockquote>
     88  
     89 <p> Failure to use the <a href="MYSQL_README.html">AUXLIBS_MYSQL</a> variable will defeat the purpose
     90 of dynamic database client loading. Every Postfix executable file
     91 will have MYSQL database library dependencies. And that was exactly
     92 what dynamic database client loading was meant to avoid. </p>
     93  
     94 </blockquote>
     95 
     96 <p> On Solaris, use this instead: </p>
     97 
     98 <blockquote>
     99 <pre>
    100 make -f Makefile.init makefiles \
    101     "CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include" \
    102     "<a href="MYSQL_README.html">AUXLIBS_MYSQL</a>=-L/usr/local/mysql/lib -R/usr/local/mysql/lib \
    103         -lmysqlclient -lz -lm"
    104 </pre>
    105 </blockquote>
    106 
    107 <p> Then, just run 'make'. This requires libz, the compression
    108 library.  Older mysql implementations build without libz. </p>
    109 
    110 <h2>Using MySQL tables</h2>
    111 
    112 <p> Once Postfix is built with mysql support, you can specify a
    113 map type in <a href="postconf.5.html">main.cf</a> like this: </p>
    114 
    115 <blockquote>
    116 <pre>
    117 <a href="postconf.5.html#alias_maps">alias_maps</a> = <a href="mysql_table.5.html">mysql</a>:/etc/postfix/mysql-aliases.cf
    118 </pre>
    119 </blockquote>
    120 
    121 <p> The file /etc/postfix/mysql-aliases.cf specifies lots of
    122 information telling Postfix how to reference the mysql database.
    123 For a complete description, see the <a href="mysql_table.5.html">mysql_table(5)</a> manual page. </p>
    124 
    125 <h2>Example: local aliases </h2>
    126 
    127 <pre>
    128 #
    129 # mysql config file for <a href="local.8.html">local(8)</a> <a href="aliases.5.html">aliases(5)</a> lookups
    130 #
    131 
    132 # The user name and password to log into the mysql server.
    133 user = someone
    134 password = some_password
    135 
    136 # The database name on the servers.
    137 dbname = customer_database
    138 
    139 # For Postfix 2.2 and later The SQL query template.
    140 # See <a href="mysql_table.5.html">mysql_table(5)</a> for details.
    141 query = SELECT forw_addr FROM mxaliases WHERE alias='%s' AND status='paid'
    142 
    143 # For Postfix releases prior to 2.2. See <a href="mysql_table.5.html">mysql_table(5)</a> for details.
    144 select_field = forw_addr
    145 table = mxaliases
    146 where_field = alias
    147 # Don't forget the leading "AND"!
    148 additional_conditions = AND status = 'paid'
    149 
    150 # This is necessary to make UTF8 queries work for Postfix 2.11 .. 3.1,
    151 # and is the default setting as of Postfix 3.2.
    152 option_group = client
    153 </pre>
    154 
    155 <h2>Additional notes</h2>
    156 
    157 <p> Postfix 3.2 and later read <b>[client]</b> option group settings
    158 by default. To disable this, specify no <b>option_file</b> and
    159 specify "<b>option_group =</b>" (i.e. an empty value).  </p>
    160 
    161 <p> Postfix 3.1 and earlier don't read <b>[client]</b> option group
    162 settings unless a non-empty <b>option_file</b> or <b>option_group</b>
    163 value are specified. To enable this, specify, for example
    164 "<b>option_group = client</b>".  </p>
    165 
    166 <p> The MySQL configuration interface setup allows for multiple
    167 mysql databases: you can use one for a virtual table, one for an
    168 access table, and one for an aliases table if you want. </p>
    169 
    170 <p> Since sites that have a need for multiple mail exchangers may
    171 enjoy the convenience of using a networked mailer database, but do
    172 not want to introduce a single point of failure to their system,
    173 we've included the ability to have Postfix reference multiple hosts
    174 for access to a single mysql map.  This will work if sites set up
    175 mirrored mysql databases on two or more hosts.  Whenever queries
    176 fail with an error at one host, the rest of the hosts will be tried
    177 in random order.  If no mysql server hosts are reachable, then mail
    178 will be deferred until at least one of those hosts is reachable.
    179 </p>
    180 
    181 <h2>Credits</h2>
    182 
    183 <ul>
    184 
    185 <li> The initial version was contributed by Scott Cotton and Joshua
    186 Marcus, IC Group, Inc.</li>
    187 
    188 <li> Liviu Daia revised the configuration interface and added the
    189 <a href="postconf.5.html">main.cf</a> configuration feature.</li>
    190 
    191 <li> Liviu Daia with further refinements from Jose Luis Tallon and
    192 Victor Duchovni developed the common query, result_format, domain and
    193 expansion_limit interface for LDAP, MySQL and PostgreSQL.</li>
    194 
    195 </ul>
    196 
    197 </body>
    198 
    199 </html>
    200