diff options
authorEdward Rudd <>2004-01-24 02:07:33 (GMT)
committer Edward Rudd <>2004-01-24 02:07:33 (GMT)
commit3f25cbb47c251d84980f4176be06173388d29225 (patch)
parentf230d2468cdc8ee3d290d34a36593b154f51aee1 (diff)
added DocBook version of the manual
1 files changed, 1600 insertions, 0 deletions
diff --git a/Documentation/manual.xml b/Documentation/manual.xml
new file mode 100644
index 0000000..f2d36ff
--- /dev/null
+++ b/Documentation/manual.xml
@@ -0,0 +1,1600 @@
1<?xml version="1.0" encoding="UTF-8"?>
2<?xml-stylesheet href="file://localhost/home/urkle/Documents/DocBook/docbook.css" type="text/css"?>
3<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "file:/usr/share/sgml/docbook/xml-dtd-4.1.2-1.0-8/docbookx.dtd">
5 <articleinfo>
6 <title>mod_log_sql Manual</title>
7 <author>
8 <firstname>Christopher</firstname>
9 <othername>B.</othername>
10 <surname>Powell</surname>
11 <affiliation>
12 <address format="linespecific"><email>chris &lt;at&gt; grubbybaby &lt;dot&gt; com</email></address>
13 </affiliation>
14 </author>
15 <author>
16 <firstname>Edward</firstname>
17 <surname>Rudd</surname>
18 <contrib>Porting from Lyx to DocBook</contrib>
19 <affiliation>
20 <address format="linespecific"><email>eddie &lt;at&gt; omegaware &lt;dot&gt; com</email></address>
21 </affiliation>
22 </author>
23 <copyright>
24 <year>2001</year>
25 <year>2002</year>
26 <year>2003</year>
27 <holder>Christopher B. Powell</holder>
28 </copyright>
29 <copyright>
30 <year>2004</year>
31 <holder>Edward Rudd</holder>
32 </copyright>
33 <revhistory>
34 <revision>
35 <revnumber>1.0</revnumber>
36 <date>2004-01-22</date>
37 <revremark>Initial Conversion from Lyx to Docbook</revremark>
38 </revision>
39 </revhistory>
40 </articleinfo>
41 <sect1 id="intro">
42 <title>Introduction</title>
43 <sect2>
44 <title>Summary</title>
45 <para>This Apache module will permit you to log to a SQL database; it can log each access request as well as data associated with each request: cookies, notes, and inbound/outbound headers. Unlike logging to a flat text file -- which is standard in Apache -- a SQL-based log exhibits tremendous flexibility and power of data extraction. (See section [sub:why] in the FAQ for further discussion and examples of the advantages to SQL.)</para>
46 <para>This module can either replace or happily coexist with mod_log_config, Apache's text file logging facility. In addition to being more configurable than the standard module, mod_log_sql is much more flexible.</para>
47 </sect2>
48 <sect2 id="copyright">
49 <title>Approach</title>
50 <para>This project was formerly known as "mod_log_mysql." It was renamed "mod_log_sql" in order to reflect the project goal of database in-specificity. The module currently supports MySQL, but support for other database back-ends is underway.</para>
51 <para>In order to save speed and overhead, links are kept alive in between queries. This module uses one dedicated SQL link per httpd child, opened by each child process when it is born. Among other things, this means that this module supports logging into only one MySQL server, and for now, also, only one SQL database. But that's a small tradeoff compared to the blinding speed of this module. Error reporting is robust throughout the module and will inform the administrator of database issues in the Apache ErrorLog for the server/virtual server.</para>
52 <para>Virtual hosts are supported in the same manner they are in the regular logging modules. The administrator defines some basic 'global' directives in the main server config, then defines more specific 'local' directives inside each VirtualHost stanza.</para>
53 <para>A robust "preserve" capability has now been implemented. This permits the module to preserve any failed INSERT commands to a local file on its machine. In any situation that the database is unavailable -- e.g. the network fails or the database host is rebooted -- mod_log_sql will note this in the error log and begin appending its log entries to the preserve file (which is created with the user &amp; group ID of the running Apache process, e.g. "nobody/nobody" on many Linux installations). When database availability returns, mod_log_sql seamlessly resumes logging to it. When convenient for the sysadmin, he/she can easily import the preserve file into the database because it is simply a series of SQL insert statements.</para>
54 </sect2>
55 <sect2>
56 <title>What gets logged by default?</title>
57 <para>All the data that would be contained in the "Combined Log Format" is logged by default, plus a little extra. Your best bet is to begin by accepting this default, then later customize the log configuration based on your needs. The documentation of the run-time directives includes a full explanation of what you can log, including examples -- see section [sec:ConfRef].</para>
58 </sect2>
59 <sect2>
60 <title>Miscellaneous Notes</title>
61 <itemizedlist>
62 <listitem>
63 <para>Note which directives go in the 'main server config' and which directives apply to the 'virtual host config'. This is made clear in the directive documentation.</para>
64 </listitem>
65 <listitem>
66 <para>The 'time_stamp' field is stored in an UNSIGNED INTEGER format, in the standard unix "seconds since the epoch" format. This is superior to storing the access time as a string due to size requirements: an UNSIGNED INT requires 4 bytes, whereas an Apache date string (e.g. "18/Nov/2001:13:59:52 -0800") requires 26 bytes: those extra 22 bytes become significant when multiplied by thousands of accesses on a busy server. Besides, an INT type is far more flexible for comparisons, etc.</para>
67 <para>In MySQL 3.21 and above you can easily convert this to a human readable format using from_unixtime(), e.g.: </para>
68 <programlisting format="linespecific">select remote_host,request_uri,from_unixtime(time_stamp) from access_log; </programlisting>
69 <para>The enclosed perl program "" extracts your access log in a format that is completely compatible with the Combined Log Format. You can then feed this to your favorite web log analysis tool.</para>
70 </listitem>
71 <listitem>
72 <para>The table's string values can be CHAR or VARCHAR, at a length of your choice. VARCHAR is superior because it truncates long strings; CHAR types are fixed-length and will be padded with spaces, resulting in waste. Just like the time_stamp issue described above, that kind of space waste multiplies over thousands of records.</para>
73 </listitem>
74 <listitem>
75 <para>Be careful not to go overboard setting fields to NOT NULL. If a field is marked NOT NULL then it must contain data in the INSERT statement, or the INSERT will fail. These mysterious failures can be quite frustrating and difficult to debug.</para>
76 </listitem>
77 <listitem>
78 <para>When Apache logs a numeric field, it uses a '-' character to mean "not applicable," e.g. the number of bytes returned on a 304 (unchanged) request. Since '-' is an illegal character in an SQL numeric field, such fields are assigned the value 0 instead of '-' which, of course, makes perfect sense anyway.</para>
79 </listitem>
80 </itemizedlist>
81 </sect2>
82 <sect2>
83 <title>Author / Maintainer</title>
84 <para>The actual logging code was taken from the already existing flat file text modules, so all that credit goes to the Apache Software Foundation.</para>
85 <para>The MySQL routines and directives were added by Zeev Suraski &lt;;. </para>
86 <para>All changes from 1.06+ and the new documentation were added by Chris Powell <email>chris &lt;at&gt; grubbybaby &lt;dot&gt; com</email>. It seems that the module had fallen into the "un-maintained" category -- it had not been updated since 1998 -- so Chris adopted it as the new maintainer.</para>
87 <para>In December of 2003, Edward Rudd <email>eddie &lt;at&gt; omegaware &lt;dot&gt; com</email> porting the module to Apache 2.0, cleaning up the code, converting the documentation to DocBook, optimizing the main logging loop, and added the much anticipated database abstraction layer.</para>
88 </sect2>
89 </sect1>
90 <sect1>
91 <title>Installation</title>
92 <sect2>
93 <title>Requirements</title>
94 <itemizedlist>
95 <listitem>
96 <para>A compatible system. mod_log_sql was authored and tested on systems based on Red Hat Linux (Red Hat, Mandrake), but the module should easily adapt to any modern distribution. mod_log_sql has also been ported successfully to Solaris and FreeBSD.</para>
97 </listitem>
98 <listitem>
99 <para>Apache 1.3 or 2.0, 1.2 is no longer supported, but may still compile. Ideally you should already have successfully compiled Apache and understand the process, but this document tries to make it simple for beginners.</para>
100 </listitem>
101 <listitem>
102 <para>The MySQL development headers. This package is called different things on different distributions. For example, Red Hat 6.x calls this RPM "MySQL-devel" whereas Mandrake calls it "libmysql10-devel." Both MySQL 3.23.x and 4.x are supported.</para>
103 </listitem>
104 <listitem>
105 <para>MySQL &gt;= 3.23.15 configured, installed and running on either localhost or an accessible networked machine. You should already have a basic understanding of MySQL and how it functions.</para>
106 </listitem>
107 <listitem>
108 <para>Optionally, if you want to be able to log SSL information such as keysize or cipher, you need OpenSSL and mod_ssl installed.</para>
109 </listitem>
110 </itemizedlist>
111 </sect2>
112 <sect2>
113 <title>Platform Specific Notes</title>
114 <para><remark>This section is currently not applicable as the new autoconf setup should autodetect everything for your server..Win32 system, however, need to be updated.</remark>These installation documents assume a relatively modern GNU/Linux scenario. mod_log_sql has been ported to other platforms; following are notes on compiling the module for those platforms.</para>
115 <sect3>
116 <title>&lt;sub:Solaris&gt;Solaris</title>
117 <para>The nanosleep() function used in mod_log_sql relies on linking aginst the librt library. Make the following alterations before proceeding: </para>
118 <orderedlist>
119 <listitem>
120 <para>In Makefile, search for the string "-lmysqlclient -lz" and change it to read "-lmysqlclient -lz -lrt"</para>
121 </listitem>
122 <listitem>
123 <para> In part [step:Linking] of section [sec:Static] below, change "-lmysqlclient -lm -lz" to read "-lmysqlclient -lm -lz -lrt"</para>
124 </listitem>
125 </orderedlist>
126 </sect3>
127 <sect3>
128 <title>BSD</title>
129 <para>No notes are available at present, but they are desired. If you have successfully ported mod_log_sql to BSD, please contact [(||the maintaniner, Chris Powell] and help fill in this section.</para>
130 </sect3>
131 <sect3>
132 <title>Win32</title>
133 <para>No notes are available at present, but they are desired. If you have successfully ported mod_log_sql to Win32, please contact [(||the maintaniner, Chris Powell] and help fill in this section.</para>
134 </sect3>
135 <sect3>
136 <title>OS X</title>
137 <para>mod_log_sql should compile and work out-of-the-box on this platform. Here are some notes from a user successfully running the module on OS X:</para>
138 <para>The only changes I had to make were to where I had the various libraries installed. Here are the changes I made to the head of the Makefile: APACHESOURCE = /usr/local/src/apache_1.3.27 (Wasn't sure if this was really needed or not, so I downloaded the Apache source just in case) APACHEINSTALLED = /usr/sbin APACHEHEADERS = /usr/include/httpd APXS = $(APACHEINSTALLED)/apxs MYSQLLIBRARIES = /usr/local/mysql/lib MYSQLHEADERS = /usr/local/mysql/include I'm using a binary installation of MySQL and the default apache installation on OS X Client 10.2.3, the locations of these files may vary depending on how you've installed MySQL and will almost certainly be different if you're using OS X Server.</para>
139 <para> My thanks to Tom Wiebe for being the first (to my knowlege) mod_log_sql user on OS X and for providing these notes.</para>
140 </sect3>
141 <sect3>
142 <title>Digital Unix</title>
143 <para>Digital Unix, like Solaris, needs to be linked against librt; see section [sub:Solaris]. Here are further notes from a user successfully running the module on Digital Unix:</para>
144 <para>Instead of trying to get the module to remember where the MySQL libraries were, I instead compiled apache with the information: LDFLAGS='-rpath /isp/mysql/lib/mysql' ./configure ... Everything worked as expected after that. (The error I got without this was "/sbin/loader: Fatal Error: cannot map" ) Digital Unix (v4.0f, at least ) appears to follow the same requirements needed by Solaris, so simply adding librt to the module made it compile without errors. As for the warnings, here's the text: mod_log_sql.c: In function `extract_request_duration': mod_log_sql.c:292: warning: long int format, different type arg (arg 4) mod_log_sql.c: In function `extract_request_timestamp': mod_log_sql.c:497: warning: long int format, different type arg (arg 4) Poking around in the code, it looks like the compiler was complaining that what time() is returning doesn't play nicely with %ld by default. I just typecast them as (long)'s and the warnings went away ( not that the module wasn't working correctly without them ). The module works very well so far in testing... hasn't dropped a single log entry yet. </para>
145 <para>My thanks to Jim Turner for permitting me to quote him here, and for being the first known user of mod_log_sql on Digital Unix</para>
146 </sect3>
147 </sect2>
148 <sect2>
149 <title>Do I want a DSO or a static module</title>
150 <para><remark>This section is no longer applicable, as the new autoconf setup only allows DSO with Apache 1.3 and 2.0.</remark>You need to know the answer to this question before you proceed. The answer is pretty straightforward: what have you done in the past? If you like all your Apache modules to be dynamic, then you should keep doing that. If you're more of an old-school type and prefer to compile the modules right into apache, do that. Both methods work equally well. </para>
151 <para>FWIW, the DSO method is more modern and increasing in popularity because apxs takes care of a lot of dirty little details for you. As you'll see below, the static-module method is a little more complex.</para>
152 </sect2>
153 <sect2>
154 <title>Installation as an Apache DSO (preferred)</title>
155 <orderedlist>
156 <listitem>
157 <para>Unpack the archive into a working directory.</para>
158 <screen>$ tar -xzf mod_log_sql-1.94.tar.gz
159$ cd mod_log_sql-1.9</screen>
160 </listitem>
161 <listitem>
162 <para>run configure to configure the source directory.</para>
163 <screen>$ ./configure</screen>
164 <para>The <filename>configure</filename> script should automatically detect all the required libraries and program if the are installed in standard locations.. If it returns an error, here is a description of the arguments you can specify when you run <filename>configure</filename>.</para>
165 <variablelist>
166 <varlistentry>
167 <term>--with-apxs=/usr/sbin/apxs</term>
168 <listitem>
169 <para>This is the full path to the apxs binary, or the directory which contains the program. This program is part of the Apache 1.3 and 2.0 installation.</para>
170 <para>The default is to search <filename>/usr/bin/apxs</filename> and <filename>/usr/sbin/apxs</filename>.</para>
171 <para>Specifying a directory here will search $directory/apxs, $directory/bin/apxs, and $directory/sbin/apxs</para>
172 <para>If you have more than one version of Apache installed, you need to specify the correct apxs binary for the one you wish to compile for.</para>
173 </listitem>
174 </varlistentry>
175 <varlistentry>
176 <term>--with-mysql=/path/to/mysql</term>
177 <listitem>
178 <para>This is the directory to search for the <filename>libmysqlclient</filename> library and the <application>MySQL</application> headers.</para>
179 <para>The default is to search <filename>/usr/include</filename>, <filename>/usr/include/mysql</filename>, <filename>/usr/local/include</filename>, and <filename>/usr/local/include/mysql</filename> for <application>MySQL</application> headers.. And <filename>/usr/lib</filename>. <filename>/usr/lib/mysql</filename>, <filename>/usr/local/lib</filename>, and <filename>/usr/local/lin/mysql</filename> for the <application>MySQL</application> libraries.</para>
180 <para>Specifying this argument will search $directory/include and $directory/mysql for <application>MySQL</application> headers. And $directory/lib and $directory/lib/mysql for <application>MySQL</application> libraries.</para>
181 </listitem>
182 </varlistentry>
183 <varlistentry>
184 <term>--enable-ssl</term>
185 <listitem>
186 <para>Specifying this argument will enable the search for mod_ssl and SSL headers, and if found will enable compilation of SSL support into mod_log_sql. SSL support is compiled into a separate module that can be loaded after the main mod_log_sql.</para>
187 </listitem>
188 </varlistentry>
189 <varlistentry>
190 <term>--with-ssl-inc=/usr/include/openssl</term>
191 <listitem>
192 <para>This is the path to the SSL toolkit header files that were used to compile mod_ssl. If you want SSL support you most likely need to specify this.</para>
193 <para>The default is to search <filename>/usr/include</filename> and <filename>/usr/include/openssl</filename>.</para>
194 <para>Specifying this argument will search that directory for the SSL headers.</para>
195 </listitem>
196 </varlistentry>
197 <varlistentry>
198 <term>--with-db-inc=/usr/include/db1</term>
199 <listitem>
200 <para>This argument is only needed when compiling SSL support for Apache 1.3, and needs to be the directory which contains the ndbm.h header file. You can find this by using </para>
201 <screen format="linespecific">$ locate ndbm.h
204 <para>As far as I can tell, there is no difference as to which you specify, but it should be the one that you compiled mod_ssl with. If unsure specify the <filename>/usr/include/db1</filename>.</para>
205 </listitem>
206 </varlistentry>
207 <varlistentry>
208 <term>--disable-apachetest</term>
209 <listitem>
210 <para>This will disable the apache version test. However there is a side affect if you specify this where I will not be able to determine which version of Apache you are compiling for. So don't specify this.. If you are having troubles with the script detecting your Apache version, then send a bug report along with your system OS version and versions of related packages.</para>
211 </listitem>
212 </varlistentry>
213 <varlistentry>
214 <term>--disable-mysqltest</term>
215 <listitem>
216 <para>This will disable the MySQL compile test. Specify this if for some reason the test fail but you know you have specified the correct directories. If mod_los_sql also fails to compile report a bug along with your system OS version and versions of related packages.</para>
217 </listitem>
218 </varlistentry>
219 </variablelist>
220 </listitem>
221 <listitem>
222 <para>Now compile the module with GNU make. You may have to specify gmake on some systems like FreeBSD.</para>
223 <screen>$ make</screen>
224 </listitem>
225 <listitem>
226 <para>If there were no errors, you can now install the module(s). If you compiled as a non-root user you may need to switch users with <application>su</application> or <application>sudo</application>.</para>
227 <screen>$ su -c "make install"
229 </listitem>
230 <listitem>
231 <para>Now edit your Apache configuration and load the modules.</para>
232 <remark>If you are loading the SSL logging module, you need to make sure it is loaded after mod_ssl and mod_log_sql.</remark>
233 <orderedlist>
234 <listitem>
235 <para>Insert these lines to either the main <filename>httpd.conf</filename> or a file included via an include directive.</para>
236 <programlisting>LoadModule log_sql_module modules/
237&lt;IfModule mod_ssl.c&gt;
238LoadModule log_sql_ssl_module moduels/
240 <remark>If you did not compile SSL support in mod_log_sql, do not include the lines between the &lt;IfModule&gt; directives.</remark>
241 </listitem>
242 <listitem>
243 <para>If you are using Apache 1.3 you may need add these lines later in the configuration.</para>
244 <programlisting>AddModule mod_log_sql.c
245&lt;IfModule mod_ssl.c&gt;
246AddModule mod_log_sql_ssl.c
248 <remark>If you did not compile SSL support in mod_log_sql, do not include the lines between the &lt;IfModule&gt; directives.</remark>
249 </listitem>
250 </orderedlist>
251 </listitem>
252 </orderedlist>
253 </sect2>
254 </sect1>
255 <sect1>
256 <title>&lt;sec:Configuration&gt;Configuration</title>
257 <sect2>
258 <title>&lt;sub:PrepDb&gt;Preparing MySQL for logging</title>
259 <para>You have to prepare the database to receive data from <application>mod_log_sql</application>, and set up run-time directives in <filename>httpd.conf</filename> to control how and what <application>mod_log_sql</application> logs.</para>
260 <para>This section will discuss how to get started with a basic configuration. Full documentation of all available run-time directives is available in section [sec:ConfRef].</para>
261 <orderedlist>
262 <listitem>
263 <para>mod_log_sql can make its own tables on-the-fly, or you can pre-make the tables by hand. The advantage of letting the module make the tables is ease-of-use, but for raw performance you will want to pre-make the tables in order to save some overhead. In this basic setup we'll just let the module create tables for us.</para>
264 </listitem>
265 <listitem>
266 <para>We still need to have a logging database created and ready, so run the MySQL command line client and create a database:</para>
267 <screen># mysql -uadmin -pmypassword
268Enter password:
269mysql&gt; create database apachelogs;</screen>
270 </listitem>
271 <listitem>
272 <para>&lt;part:CrTbl&gt;If you want to hand-create the tables, run the enclosed 'create-tables' SQL script as follows ("create_tables.sql" needs to be in your current working directory).</para>
273 <screen>mysql&gt; use apachelogs
274Database changed
275mysql&gt; source create_tables.sql</screen>
276 </listitem>
277 <listitem>
278 <para>Create a specific <application>MySQL</application> userid that <application>httpd</application> will use to authenticate and enter data. This userid need not be an actual Unix user. It is a userid internal to <application>MySQL</application> with specific privileges. In the following example command, "apachelogs" is the database, "loguser" is the userid to create, "" is the name of the Apache machine, and "l0gger" is the password to assign. Choose values that are different from these examples.</para>
279 <screen>mysql&gt; grant insert,create on apachelogs.* to identified by 'l0gger';</screen>
280 </listitem>
281 <listitem>
282 <para>You may be especially security-paranoid and want "loguser" to not have "create" capability within the "apachelogs" database. You can disable that privilege, but the cost is that you will not be able to use the module's on-the-fly table creation feature. If that cost is acceptable, hand-create the tables as described in step [part:CrTbl] and use the following GRANT statement instead of the one above:</para>
283 <screen>mysql&gt; grant insert on apachelogs.* to identified by 'l0gger';</screen>
284 </listitem>
285 <listitem>
286 <para>&lt;step:EnaLog&gt;Enable full logging of your <application>MySQL</application> daemon (at least temporarily for debugging purposes) if you don't do this already. Edit /etc/my.cnf and add the following line to your [mysqld] section:</para>
287 <programlisting>log=/var/log/mysql-messages</programlisting>
288 <para>Then restart <application>MySQL</application></para>
289 <screen># /etc/rc.d/init.d/mysql restart</screen>
290 </listitem>
291 </orderedlist>
292 </sect2>
293 <sect2>
294 <title>A very basic logging setup in Apache</title>
295 <orderedlist>
296 <listitem>
297 <para>Tell the module what database to use and the appropriate authentication information.</para>
298 <para>So, edit httpd.conf and insert the following lines somewhere after any LoadModule / AddModule statements. Make sure these statements are "global," i.e. not inside any VirtualHost stanza. You will also note that you are embedding a password in the file. Therefore you are advised to "chmod 660 httpd.conf" to prevent unauthorized regular users from viewing your database user and password.</para>
299 <para>Use the <application>MySQL</application> database called "apachelogs" running on "". Use username "loguser" and password "l0gg3r" to authenticate to the database. Permit the module create tables for us.</para>
300 <example>
301 <title>Basic Example</title>
302 <programlisting>LogSQLLoginInfo loguser l0gg3r
303LogSQLDatabase apachelogs
304LogSQLCreateTables on</programlisting>
305 </example>
306 <para>If your database resides on localhost instead of another host, specify the MySQL server's socket file as follows:</para>
307 <programlisting>LogSQLSocketFile /your/path/to/mysql.sock</programlisting>
308 <para>If your database is listening on a port other than 3306, specify the correct TCP port as follows:</para>
309 <programlisting>LogSQLTCPPort 1234</programlisting>
310 </listitem>
311 <listitem>
312 <para>The actual logging is set up on a virtual-host-by-host basis. So, skip down to the virtual host you want to set up. Instruct this virtual host to log entries to the table "access_log" by inserting a LogSQLTransferLogTable directive. (The LogSQLTransferLogTable directive is the minimum required to log -- other directives that you will learn about later simply tune the module's behavior.)</para>
313 <programlisting>&lt;VirtualHost;
314 [snip]
315 LogSQLTransferLogTable access_log
316 [snip]
318 </listitem>
319 <listitem>
320 <para>Restart apache.</para>
321 <screen># /etc/rc.d/init.d/httpd stop
322# /etc/rc.d/init.d/httpd start</screen>
323 </listitem>
324 </orderedlist>
325 </sect2>
326 <sect2>
327 <title>Testing the basic setup</title>
328 <orderedlist>
329 <listitem>
330 <para>Visit your web site in a browser to trigger some hits, then confirm that the entries are being successfully logged:</para>
331 <screen># mysql -umysqladmin -p -e "select * from access_log" apachelogs
332Enter password:</screen>
333 <para>Several lines of output should follow, corresponding to your hits on the site. You now have basic functionality. Don't disable your regular Apache logs until you feel comfortable that the database is behaving as you'd like and that things are going well. If you do not see any entries in the access_log, please consult section [faq:NothingLogged] of the FAQ on how to debug and fix the situation.</para>
334 </listitem>
335 <listitem>
336 <para>You can now activate the advanced features of mod_log_sql, which are described in the next section.</para>
337 </listitem>
338 </orderedlist>
339 </sect2>
340 <sect2>
341 <title>How to tune logging with run-time directives</title>
342 <sect3>
343 <title>Instructing the module what to log</title>
344 <para>The most basic directive for the module is LogSQLTransferLogFormat, which tells the module which information to send to the database; logging to the database will not take place without it. Place a LogSQLTransferLogFormat directive in the VirtualHost stanza of each virtual host that you want to activate.</para>
345 <para>After LogSQLTransferLogFormat you supply a string of characters that tell the module what information to log. In the configuration directive reference (section [sub:Frmat]) there is a table which clearly defines all the possible things to log. Let's say you want to log only the "request time," the "remote host," and the "request"; you'd use:</para>
346 <programlisting>LogSQLTransferLogFormat hUS</programlisting>
347 <para>But a more appropriate string to use is</para>
348 <programlisting>LogSQLTransferLogFormat AbHhmRSsTUuv</programlisting>
349 <para>which logs all the information required to be compatible with the Combined Log Format (CLF).</para>
350 <para>If you don't choose to log everything that is available, that's fine. Fields in the unused columns in your table will simply contain NULL.</para>
351 <para>Some of the LogSQLTransferLogFormat characters require a little extra configuration:</para>
352 <itemizedlist>
353 <listitem>
354 <para>If you specify 'c' to indicate that you want to log the cookie value, you must also tell the module which cookie you mean by using LogSQLWhichCookie -- after all, there could be many cookies associated with a given request. Fail to specify LogSQLWhichCookie, and no cookie information at all will be logged. </para>
355 </listitem>
356 <listitem>
357 <para>If you specify 'M' to indicate that you want to log the machine ID, you must also tell the module this machine's identity using the LogSQLMachineID directive. Fail to specify LogSQLMachineID, and a simple '-' character will be logged in the machine_id column.</para>
358 </listitem>
359 </itemizedlist>
360 </sect3>
361 <sect3>
362 <title>&lt;sub:Ignore&gt;Instructing the module what NOT to log using filtering directives</title>
363 <para>One "accept" and two "ignore" directives allow you to fine-tune what the module should not log. These are very handy for keeping your database as uncluttered as possible and keeping your statistics free of unneeded numbers. Think of each one as a gatekeeper.</para>
364 <para><emphasis>It is important to remember that each of these three directives is purely optional. mod_log_sql's default is to log everything.</emphasis></para>
365 <para>When a request comes in, the contents of LogSQLRequestAccept are evaluated first. This optional, "blanket" directive lets you specify that only certain things are to be accepted for logging, and everything else discarded. Because it is evaluated before LogSQLRequestIgnore and LogSQLRemhostIgnore it can halt logging before those two filtering directives "get their chance." </para>
366 <para>Once a request makes it past LogSQLRequestAccept, it still can be excluded based on LogSQLRemhostIgnore and LogSQLRequestIgnore. A good way to use LogSQLRemhostIgnore is to prevent the module from logging the traffic that your internal hosts generate. LogSQLRequestIgnore is great for preventing things like requests for "favicon.ico" from cluttering up your database, as well as excluding the various requests that worms make, etc.</para>
367 <para>You can specify a series of strings after each directive. Do not use any type of globbing or regular-expression syntax -- each string is considered a match <emphasis>if it is a substring of the larger request or remote-host; the comarison is case-sensitive</emphasis>. This means that "LogSQLRemhostIgnore micro" will ignore requests from "," "," "," etc. "LogSQLRequestIgnore gif" will instruct the module to ignore requests for "leftbar.gif," "bluedot.gif" and even "giftwrap.jpg" -- but "RED.GIF" and "Tree.Gif" would still get logged because of case sensitivity.</para>
368 <para>A summary of the decision flow:</para>
369 <orderedlist>
370 <listitem>
371 <para>If LogSQLRequestAccept exists and a request does not match anything in that list, it is discarded.</para>
372 </listitem>
373 <listitem>
374 <para>If a request matches anything in the LogSQLRequestIgnore list, it is discarded.</para>
375 </listitem>
376 <listitem>
377 <para>If a reqiest matches anything in the LogSQLRemhostIgnore list, it is discarded.</para>
378 </listitem>
379 <listitem>
380 <para>Otherwise the request is logged.</para>
381 </listitem>
382 </orderedlist>
383 <para>This means that you can have a series of directives similar to the following:</para>
384 <programlisting>LogSQLRequestAccept *.html *.gif *.jpg
385LogSQLRequestIgnore statistics.html bluedot.jpg</programlisting>
386 <para>So the first line instructs the module to only log files with html, gif and jpg suffixes; requests for "formail.cgi" and "" will never be considered for logging. ("LeftArrow.JPG" will also never be considered for logging -- remember, the comparison is case sensitive.) The second line prunes the list further -- you never want to log requests for those two objects.</para>
387 <tip>
388 <para>If you want to match all the hosts in your domain such as "" and "", simply specify:</para>
389 <programlisting format="linespecific">LogSQLRemhostIgnore</programlisting>
390 </tip>
391 <tip>
392 <para>A great way to catch the vast majority of worm-attack requests and prevent them from being logged is to specify:</para>
393 <programlisting format="linespecific">LogSQLRequestIgnore root.exe cmd.exe default.ida</programlisting>
394 </tip>
395 <tip>
396 <para>To prevent the logging of requests for common graphic types, make sure to put a '.' before the suffix to avoid matches that you didn't intend:</para>
397 <programlisting format="linespecific">LogSQLRequestIgnore .gif .jpg</programlisting>
398 </tip>
399 </sect3>
400 </sect2>
401 <sect2>
402 <title>Advanced logging scenarios</title>
403 <sect3>
404 <title>Using the module in an ISP environment</title>
405 <para>mod_log_sql has three basic tiers of operation:</para>
406 <orderedlist>
407 <listitem>
408 <para>The administrator creates all necessary tables by hand and configures each Apache VirtualHost by hand. (LogSQLCreateTables Off)</para>
409 </listitem>
410 <listitem>
411 <para>The module is permitted to create necessary tables on-the-fly, but the administrator configures each Apache VirtualHost by hand. (LogSQLCreateTables On)</para>
412 </listitem>
413 <listitem>
414 <para>The module is permitted to create all necessary tables and to make intelligent, on-the-fly configuration of each VirtualHost. (LogSQLMassVirtualHosting On)</para>
415 </listitem>
416 </orderedlist>
417 <para>Many users are happy to use the module in its most minimal form: they hand-create any necessary tables (using "create_tables.sql"), and they configure each VirtualHost by hand to suit their needs. However, some administrators need extra features due to a large and growing number of VirtualHosts. The LogSQLMassVirtualHosting directive activates module capabilities that make it far easier to manage an ISP environment, or any situation characterized by a large and varying number of virtual servers.</para>
418 <itemizedlist>
419 <listitem>
420 <para>the on-the-fly table creation feature is activated automatically</para>
421 </listitem>
422 <listitem>
423 <para>the transfer log table name is dynamically set from the virtual host's name (example: a virtual host "" gets logged to table "access_www_grubbybaby_com")</para>
424 </listitem>
425 </itemizedlist>
426 <para>There are numerous benefits. The admin will not need to create new tables for every new VirtualHost. (Although the admin will still need to drop the tables of virtual hosts that are removed.) The admin will not need to set LogSQLTransferLogTable for each virtual host -- it will be configured automatically based on the host's name. Because each virtual host will log to its own segregated table, data about one virtual server will segregate from others; an admin can grant users access to the tables they need, and they will be unable to view data about another user's virtual host.</para>
427 <para>In an ISP scenario the admin is likely to have a cluster of many front-end webservers logging to a back-end database. mod_log_sql has a feature that permits analysis of how well the web servers are loadbalancing: the LogSQLMachineID directive. The administrator uses this directive to assign a unique identifier to each machine in the web cluster, e.g. "LogSQLMachineID web01," "LogSQLMachineID web02," etc. Used in conjunction with the 'M' character in LogSQLTransferLogFormat, each entry in the SQL log will include the machine ID of the machine that created the entry. This permits the administrator to count the entries made by each particular machine and thereby analyze the front-end loadbalancing algorithm.</para>
428 </sect3>
429 <sect3>
430 <title>&lt;secMulTable&gt;Logging many-to-one data in separate tables</title>
431 <para>A given HTTP request can have a one-to-many relationship with certain kinds of data. For example, a single HTTP request can have 4 cookies, 3 headers and 5 "mod_gzip" notes associated with it. mod_log_sql is capable of logging these relationships due to the elegance of SQL relational data.</para>
432 <para>You already have a single table containing access requests. One of the columns in that table is 'id' which is intended to contain the unique request ID supplied by the standard Apache module mod_unique_id -- all you need to do is compile in that module and employ the LogSQLTransferLogFormat character 'I'. Thereafter, each request gets a unique ID that can be thought of as a primary key within the database, useful for joining multiple tables. So let's envision several new tables: a notes table, a cookies table, and a table for inbound and outbound headers.</para>
433 <table>
434 <title>&lt;tblAcc&gt;access_log</title>
435 <tgroup cols="6">
436 <colspec colname="1" colnum="1"/>
437 <colspec colname="2" colnum="2"/>
438 <colspec colname="3"/>
439 <colspec colname="4"/>
440 <colspec colname="5" colwidth="40"/>
441 <colspec colname="6" colwidth="70"/>
442 <thead>
443 <row>
444 <entry colname="1">id</entry>
445 <entry colname="2">remote_host</entry>
446 <entry colname="3">request_uri</entry>
447 <entry colname="4">time_stamp</entry>
448 <entry colname="5">status</entry>
449 <entry colname="6">bytes_sent</entry>
450 </row>
451 </thead>
452 <tbody>
453 <row>
454 <entry colname="1">PPIDskBRH30AAGPtAsg</entry>
455 <entry colname="2"></entry>
456 <entry colname="3">/mod_log_sql/index.html</entry>
457 <entry colname="4">1022493617</entry>
458 <entry colname="5">200</entry>
459 <entry colname="6">2215</entry>
460 </row>
461 </tbody>
462 </tgroup>
463 </table>
464 <table>
465 <title>&lt;tblNotes&gt;notes_log</title>
466 <tgroup cols="3">
467 <colspec colname="1"/>
468 <colspec colname="2"/>
469 <colspec colname="3" colwidth="30"/>
470 <thead>
471 <row>
472 <entry colname="1">id</entry>
473 <entry colname="2">item</entry>
474 <entry colname="3">val</entry>
475 </row>
476 </thead>
477 <tbody>
478 <row>
479 <entry colname="1">PPIDskBRH30AAGPtAsg</entry>
480 <entry colname="2">mod_gzip_result</entry>
481 <entry colname="3">OK</entry>
482 </row>
483 <row>
484 <entry colname="1">PPIDskBRH30AAGPtAsg</entry>
485 <entry colname="2">mod_gzip_compression_ratio</entry>
486 <entry colname="3">69</entry>
487 </row>
488 </tbody>
489 </tgroup>
490 </table>
491 <table>
492 <title>&lt;tblHdr&gt;headers_log</title>
493 <tgroup cols="3">
494 <colspec colname="1" colnum="1"/>
495 <colspec colname="2" colnum="2"/>
496 <colspec colname="3" colnum="3"/>
497 <thead>
498 <row>
499 <entry colname="1">id</entry>
500 <entry colname="2">item</entry>
501 <entry colname="3">val</entry>
502 </row>
503 </thead>
504 <tbody>
505 <row>
506 <entry colname="1">PPIDskBRH30AAGPtAsg</entry>
507 <entry colname="2">Content-Type</entry>
508 <entry colname="3">text/html</entry>
509 </row>
510 <row>
511 <entry colname="1">PPIDskBRH30AAGPtAsg</entry>
512 <entry colname="2">Accept-Encoding</entry>
513 <entry colname="3">gzip, deflate</entry>
514 </row>
515 <row>
516 <entry colname="1">PPIDskBRH30AAGPtAsg</entry>
517 <entry colname="2">Expires</entry>
518 <entry colname="3">Tue, 28 May 2002 10:00:18 GMT</entry>
519 </row>
520 <row>
521 <entry colname="1">PPIDskBRH30AAGPtAsg</entry>
522 <entry colname="2">Cache-Control</entry>
523 <entry colname="3">max-age=86400</entry>
524 </row>
525 </tbody>
526 </tgroup>
527 </table>
528 <para>We have a certain request, and its unique ID is "PPIDskBRH30AAGPtAsg". Within each separate table will be multiple entries with that request ID: several cookie entries, several header entries, etc. As you can see in tables [tblAcc], [tblNotes] and [tblHdr], you have a one-to-many relationship for request PPIDskBRH30AAGPtAsg: that one access has two associated notes and four associated headers. You can extract this data easily using the power of SQL's "select" statement and table joins. To see the notes associated with a particular request:</para>
529 <screen>select a.remote_host, a.request_uri, n.item, n.val from access_log a, notes_log n
530where and'PPIDskBRH30AAGPtAsg';</screen>
531 <table>
532 <title></title>
533 <tgroup cols="4">
534 <colspec colname="1"/>
535 <colspec colname="2"/>
536 <colspec colname="3"/>
537 <colspec colname="4" colwidth="30"/>
538 <thead>
539 <row>
540 <entry colname="1">remote_host</entry>
541 <entry colname="2">request_uri</entry>
542 <entry colname="3">item</entry>
543 <entry colname="4">val</entry>
544 </row>
545 </thead>
546 <tbody>
547 <row>
548 <entry colname="1"></entry>
549 <entry colname="2">/mod_log_sql/index.html</entry>
550 <entry colname="3">mod_gzip_result</entry>
551 <entry colname="4">OK</entry>
552 </row>
553 <row>
554 <entry colname="1"></entry>
555 <entry colname="2">/mod_log_sql/index.html</entry>
556 <entry colname="3">mod_gzip_compression_ratio</entry>
557 <entry colname="4">69</entry>
558 </row>
559 </tbody>
560 </tgroup>
561 </table>
562 <para>Naturally you can craft similar statements for the outboud headers, inbound headers and cookies, all of which can live in separate tables. Your statements are limited in power only by your skill with SQL.</para>
563 <para>In order to use this capability of mod_log_sql, you must do several things.</para>
564 <itemizedlist>
565 <listitem>
566 <para>Compile mod_unique_id into Apache (statically or as a DSO). mod_log_sql employs the unique request ID that mod_unique_id provides in order to key between the separate tables. You can still log the data without mod_unqiue_id, but it will be completely uncorrelated and you will have no way to discern any meaning.</para>
567 </listitem>
568 <listitem>
569 <para>Create the appropriate tables. This will be done for you if you permit mod_log_sql to create its own tables using LogSQLCreateTables On, or if you use the enclosed "create_tables.sql" script.</para>
570 </listitem>
571 <listitem>
572 <para>Create a SQL index on the "id" column. Without this index, table joins will be deathly slow. I recommend you consult the MySQL documentation on the proper way to create a column index if you are not familiar with this operation.</para>
573 </listitem>
574 <listitem>
575 <para>Within each appropriate VirtualHost stanza, use the LogSQLWhich* and LogSQL*LogTable directives to tell the module what and where to log the data. In the following example, I have overridden the name for the notes table whereas I have left the other table names at their defaults. I have then specified the cookies, headers and notes that interest me. (And as you can see, these directives do not require me to add any characters to LogSQLTransferLogTable.)</para>
576 <programlisting>&lt;VirtualHost;
577 (snip)
578 LogSQLNotesLogTable notestable
579 LogSQLWhichCookies bluecookie redcookie greencookie
580 LogSQLWhichNotes mod_gzip_result mod_gzip_compression_ratio
581 LogSQLWhichHeadersOut Expires Content-Type Cache-Control
582 LogSQLWhichHeadersIn UserAgent Accept-Encoding Host
583 (snip)
585 </listitem>
586 </itemizedlist>
587 </sect3>
588 <sect3>
589 <title>Using the same database for production and test</title>
590 <para>Although sub-optimal, it is not uncommon to use the same back-end database for the "production" webservers as well as the "test" webservers (budgetary constraints, rack-space limits, etc.). Furthermore, an administrator in this situation may be unable to use LogSQLRemhostIgnore to exclude requests from the test servers -- perhaps the generated entries are genuinely useful for analytical or QA purposes, but their value after analysis is minimal.</para>
591 <para>It is wasteful and potentially confusing to permit this internal test data to clutter the database, and a solution to the problem is the proper use of the LogSQLMachineID directive. Assume a scenario where the production webservers have IDs like "web01," "web02," and so on -- and the test webservers have IDs like "test01," "test02," etc. Because entries in the log database are distinguished by their source machine, an administrator may purge unneeded test data from the access log as follows:</para>
592 <programlisting>delete from access_log where machine_id like 'test%';</programlisting>
593 </sect3>
594 <sect3>
595 <title>&lt;sub:DelayedIns&gt;Optimizing for a busy database</title>
596 <para>A busy MySQL database will have SELECT statements running concurrently with INSERT and UPDATE statements. A long-running SELECT can in certain circumstances block INSERTs and therefore block mod_log_sql. A workaround is to enable mod_log_sql for "delayed inserts," which are described as follows in the MySQL documentation.</para>
597 <para>The DELAYED option for the INSERT statement is a MySQL-specific option that is very useful if you have clients that can't wait for the INSERT to complete. This is a common problem when you use MySQL for logging and you also periodically run SELECT and UPDATE statements that take a long time to complete. DELAYED was introduced in MySQL Version 3.22.15. It is a MySQL extension to ANSI SQL92.</para>
598 <para>INSERT DELAYED only works with ISAM and MyISAM tables. Note that as MyISAM tables supports concurrent SELECT and INSERT, if there is no free blocks in the middle of the data file, you very seldom need to use INSERT DELAYED with MyISAM. </para>
599 <para>When you use INSERT DELAYED, the client will get an OK at once and the row will be inserted when the table is not in use by any other thread.</para>
600 <para>Another major benefit of using INSERT DELAYED is that inserts from many clients are bundled together and written in one block. This is much faster than doing many separate inserts. </para>
601 <para>The general disadvantages of delayed inserts are</para>
602 <orderedlist>
603 <listitem>
604 <para>The queued rows are only stored in memory until they are inserted into the table. If mysqld dies unexpectedly, any queued rows that were not written to disk are lost.</para>
605 </listitem>
606 <listitem>
607 <para>There is additional overhead for the server to handle a separate thread for each table on which you use INSERT DELAYED.</para>
608 </listitem>
609 </orderedlist>
610 <warning>
611 <para>The MySQL documentation concludes, "This means that you should only use INSERT DELAYED when you are really sure you need it!" Furthermore, the current state of error return from a failed INSERT DELAYED seems to be in flux, and may behave in unpredictable ways between different MySQL versions. See section [sub:DelayedInsFAQ] in the FAQ -- you have been warned.</para>
612 </warning>
613 <para>If you are experiencing issues which could be solved by delayed inserts, then set LogSqlDelayedInserts On in the <filename>httpd.conf</filename>. All regular INSERT statements are now INSERT DELAYED, and you should see no more blocking of the module.</para>
614 </sect3>
615 </sect2>
616 <sect2>
617 <title>&lt;sec:ConfRef&gt;Configuration directive reference</title>
618 <para>It is imperative that you understand which directives are used only once in the main server config, and which are used inside VirtualHost stanzas and therefore multiple times within httpd.conf. The "context" listed with each entry informs you of this.</para>
619 <variablelist>
620 <varlistentry>
621 <term>LogSQLCookieLogTable</term>
622 <listitem>
623 <cmdsynopsis sepchar=" ">
624 <command>LogSQLCookieLogTable</command>
625 <arg choice="req" rep="norepeat"><replaceable></replaceable>table-name</arg>
626 </cmdsynopsis>
627 <simpara>Example: LogSQLCookieLogTable cookie_log</simpara>
628 <simpara>Default: cookies</simpara>
629 <simpara>Context: virtual host</simpara>
630 <para>Defines which table is used for logging of cookies. Working in conjunction with LogSQLWhichCookies, you can log many of each request's associated cookies to a separate table. For meaningful data retrieval the cookie table is keyed to the access table by the unique request ID supplied by the standard Apache module mod_unique_id.</para>
631 <note>
632 <para>You must create the table (see create-tables.sql, included in the package), or LogSQLCreateTables must be set to "on".</para>
633 </note>
634 </listitem>
635 </varlistentry>
636 <varlistentry>
637 <term>LogSQLCreateTables</term>
638 <listitem>
639 <cmdsynopsis>
640 <command>LogSQLCreateTables</command>
641 <arg choice="req">flag</arg>
642 </cmdsynopsis>
643 <simpara>Example: LogSQLCreateTables On</simpara>
644 <simpara>Default: Off</simpara>
645 <simpara>Context: main server config</simpara>
646 <para>mod_log_sql has the ability to create its tables on-the-fly. The advantage to this is convenience: you don't have to execute any SQL by hand to prepare the table. This is especially helpful for people with lots of virtual hosts (who should also see the LogSQLMassVirtualHosting directive).</para>
647 <para>There is a slight disadvantage: if you wish to activate this feature, then the userid specified in LogSQLLoginInfo must have CREATE privileges on the database. In an absolutely paranoid, locked-down situation you may only want to grant your mod_log_sql user INSERT privileges on the database; in that situation you are unable to take advantage of LogSQLCreateTables. But most people -- even the very security-conscious -- will find that granting CREATE on the logging database is reasonable.</para>
648 <note>
649 <para>This is defined only once in the <filename moreinfo="none">httpd.conf</filename> file.</para>
650 </note>
651 </listitem>
652 </varlistentry>
653 <varlistentry>
654 <term>LogSQLDatabase</term>
655 <listitem>
656 <cmdsynopsis>
657 <command>LogSQLDatabase</command>
658 <arg choice="req"><replaceable>database</replaceable></arg>
659 </cmdsynopsis>
660 <simpara>Example: LogSQLDatabase loggingdb</simpara>
661 <simpara>Context: main server config</simpara>
662 <para>Defines the database that is used for logging. "database" must be a valid db on the MySQL host defined in LogSQLLoginInfo</para>
663 <note>
664 <para>This is defined only once in the <filename>httpd.conf</filename> file.</para>
665 <para>This directive Must be defined for logging to be enabled.</para>
666 </note>
667 </listitem>
668 </varlistentry>
669 <varlistentry>
670 <term>LogForcePreserve</term>
671 <listitem>
672 <cmdsynopsis>
673 <command>LogSQLForcePreserve</command>
674 <arg choice="req">flag</arg>
675 </cmdsynopsis>
676 <simpara>Example: LogForcePreserve On</simpara>
677 <simpara>Default: Off</simpara>
678 <simpara>Context: main server config</simpara>
679 <para>You may need to perform debugging on your database and specifically want mod_log_sql to make no attempts to log to it. This directive instructs the module to send all its log entries directly to the preserve file and to make no database INSERT attempts.</para>
680 <para>This is presumably a directive for temporary use only; it could be dangerous if you set it and forget it, as all your entries will simply pile up in the preserve file.</para>
681 <note>
682 <para>This is defined only once in the <filename>httpd.conf</filename> file.</para>
683 </note>
684 </listitem>
685 </varlistentry>
686 <varlistentry>
687 <term>LogSQLHeadersInLogTable</term>
688 <listitem>
689 <cmdsynopsis>
690 <command>LogSQLHeadersInLogTable</command>
691 <arg choice="req"><replaceable>table-name</replaceable></arg>
692 </cmdsynopsis>
693 <simpara>Example: LogSQLHeadersInLogTable headers</simpara>
694 <simpara>Default: headers_in</simpara>
695 <simpara>Context: virtual host</simpara>
696 <para>Defines which table is used for logging of inbound headers. Working in conjunction with LogSQLWhichHeadersIn, you can log many of each request's associated headers to a separate table. For meaningful data retrieval the headers table is keyed to the access table by the unique request ID supplied by the standard Apache module mod_unique_id.</para>
697 <note>
698 <para>Note that you must create the table (see create-tables.sql, included in the package), or LogSQLCreateTables must be set to "on".</para>
699 </note>
700 </listitem>
701 </varlistentry>
702 <varlistentry>
703 <term>LogSQLHeadersOutLogTable</term>
704 <listitem>
705 <cmdsynopsis sepchar=" ">
706 <command moreinfo="none">LogSQLHeadersOutLogTable</command>
707 <arg choice="req" rep="norepeat"><replaceable>table-name</replaceable></arg>
708 </cmdsynopsis>
709 <simpara>Example: LogSQLHeadersOutLogTable headers</simpara>
710 <simpara>Default: headers_out</simpara>
711 <simpara>Context: virtual host</simpara>
712 <para>Defines which table is used for logging of outbound headers. Working in conjunction with LogSQLWhichHeadersOut, you can log many of each request's associated headers to a separate table. For meaningful data retrieval the headers table is keyed to the access table by the unique request ID supplied by the standard Apache module mod_unique_id.</para>
713 <note>
714 <para>Note that you must create the table (see create-tables.sql, included in the package), or LogSQLCreateTables must be set to "on".</para>
715 </note>
716 </listitem>
717 </varlistentry>
718 <varlistentry>
719 <term>LogSQLLoginInfo</term>
720 <listitem>
721 <cmdsynopsis>
722 <command>LogSQLLoginInfo</command>
723 <arg choice="req"><replaceable>host</replaceable></arg>
724 <arg choice="req"><replaceable>user</replaceable></arg>
725 <arg choice="req"><replaceable>password</replaceable></arg>
726 </cmdsynopsis>
727 <simpara>Example: LogSQLLoginInfo logwriter passw0rd</simpara>
728 <simpara>Context: main server config</simpara>
729 <para>Defines the general parameters of the MySQL host to which you will be logging. "host" is the hostname or IP address of the MySQL machine, and is simply "localhost" if the database lives on the same machine as Apache. "user" is the MySQL userid (not a Unix userid!) with INSERT privileges on the table defined in LogSQLTransferLogTable. "password" is that user's password.</para>
730 <note>
731 <para>This is defined only once in the <filename moreinfo="none">httpd.conf</filename> file.</para>
732 <para>This directive Must be defined for logging to be enabled.</para>
733 </note>
734 </listitem>
735 </varlistentry>
736 <varlistentry>
737 <term>LogSQLMachineID</term>
738 <listitem>
739 <cmdsynopsis>
740 <command></command>
741 <arg></arg>
742 </cmdsynopsis>
743 <simpara></simpara>
744 <simpara></simpara>
745 <simpara></simpara>
746 <para></para>
747 <para></para>
748 <note>
749 <para><filename></filename></para>
750 </note>
751 </listitem>
752 </varlistentry>
753 <varlistentry>
754 <term>LogSQLMassVirtualHosting</term>
755 <listitem>
756 <cmdsynopsis>
757 <command></command>
758 <arg></arg>
759 </cmdsynopsis>
760 <simpara></simpara>
761 <simpara></simpara>
762 <simpara></simpara>
763 <para></para>
764 <para></para>
765 <note>
766 <para><filename></filename></para>
767 </note>
768 </listitem>
769 </varlistentry>
770 <varlistentry>
771 <term>LogSQLNotesLogTable</term>
772 <listitem>
773 <cmdsynopsis>
774 <command></command>
775 <arg></arg>
776 </cmdsynopsis>
777 <simpara></simpara>
778 <simpara></simpara>
779 <simpara></simpara>
780 <para></para>
781 <para></para>
782 <note>
783 <para><filename></filename></para>
784 </note>
785 </listitem>
786 </varlistentry>
787 <varlistentry>
788 <term>LogSQlPreserveFile</term>
789 <listitem>
790 <cmdsynopsis>
791 <command></command>
792 <arg></arg>
793 </cmdsynopsis>
794 <simpara></simpara>
795 <simpara></simpara>
796 <simpara></simpara>
797 <para></para>
798 <para></para>
799 <note>
800 <para><filename></filename></para>
801 </note>
802 </listitem>
803 </varlistentry>
804 <varlistentry>
805 <term>LogSQLRemhostIgnore</term>
806 <listitem>
807 <cmdsynopsis>
808 <command></command>
809 <arg></arg>
810 </cmdsynopsis>
811 <simpara></simpara>
812 <simpara></simpara>
813 <simpara></simpara>
814 <para></para>
815 <para></para>
816 <note>
817 <para><filename></filename></para>
818 </note>
819 </listitem>
820 </varlistentry>
821 <varlistentry>
822 <term>LogSQLRequestAccept</term>
823 <listitem>
824 <cmdsynopsis>
825 <command></command>
826 <arg></arg>
827 </cmdsynopsis>
828 <simpara></simpara>
829 <simpara></simpara>
830 <simpara></simpara>
831 <para></para>
832 <para></para>
833 <note>
834 <para><filename></filename></para>
835 </note>
836 </listitem>
837 </varlistentry>
838 <varlistentry>
839 <term>LogSQLRequestIgnore</term>
840 <listitem>
841 <cmdsynopsis>
842 <command></command>
843 <arg></arg>
844 </cmdsynopsis>
845 <simpara></simpara>
846 <simpara></simpara>
847 <simpara></simpara>
848 <para></para>
849 <para></para>
850 <note>
851 <para><filename></filename></para>
852 </note>
853 </listitem>
854 </varlistentry>
855 <varlistentry>
856 <term>LogSQLSocketFile</term>
857 <listitem>
858 <cmdsynopsis>
859 <command></command>
860 <arg></arg>
861 </cmdsynopsis>
862 <simpara></simpara>
863 <simpara></simpara>
864 <simpara></simpara>
865 <para></para>
866 <para></para>
867 <note>
868 <para><filename></filename></para>
869 </note>
870 </listitem>
871 </varlistentry>
872 <varlistentry>
873 <term>LogSQLTCPPort</term>
874 <listitem>
875 <cmdsynopsis>
876 <command></command>
877 <arg></arg>
878 </cmdsynopsis>
879 <simpara></simpara>
880 <simpara></simpara>
881 <simpara></simpara>
882 <para></para>
883 <para></para>
884 <note>
885 <para><filename></filename></para>
886 </note>
887 </listitem>
888 </varlistentry>
889 <varlistentry>
890 <term>&lt;sub:Frmat&gt;LogSQLTransferLogFormat </term>
891 <listitem>
892 <cmdsynopsis>
893 <command></command>
894 <arg></arg>
895 </cmdsynopsis>
896 <simpara></simpara>
897 <simpara></simpara>
898 <simpara></simpara>
899 <para></para>
900 <para></para>
901 <note>
902 <para><filename></filename></para>
903 </note>
904 </listitem>
905 </varlistentry>
906 <varlistentry>
907 <term>LogSQLTransferLogTable</term>
908 <listitem>
909 <cmdsynopsis>
910 <command></command>
911 <arg></arg>
912 </cmdsynopsis>
913 <simpara></simpara>
914 <simpara></simpara>
915 <simpara></simpara>
916 <para></para>
917 <para></para>
918 <note>
919 <para><filename></filename></para>
920 </note>
921 </listitem>
922 </varlistentry>
923 <varlistentry>
924 <term>LogSQLWhichCookie</term>
925 <listitem>
926 <cmdsynopsis>
927 <command></command>
928 <arg></arg>
929 </cmdsynopsis>
930 <simpara></simpara>
931 <simpara></simpara>
932 <simpara></simpara>
933 <para></para>
934 <para></para>
935 <note>
936 <para><filename></filename></para>
937 </note>
938 </listitem>
939 </varlistentry>
940 <varlistentry>
941 <term>LogSQLWhichCookies</term>
942 <listitem>
943 <cmdsynopsis>
944 <command></command>
945 <arg></arg>
946 </cmdsynopsis>
947 <simpara></simpara>
948 <simpara></simpara>
949 <simpara></simpara>
950 <para></para>
951 <para></para>
952 <note>
953 <para><filename></filename></para>
954 </note>
955 </listitem>
956 </varlistentry>
957 <varlistentry>
958 <term>LogSQLWhichHeadersIn</term>
959 <listitem>
960 <cmdsynopsis>
961 <command></command>
962 <arg></arg>
963 </cmdsynopsis>
964 <simpara></simpara>
965 <simpara></simpara>
966 <simpara></simpara>
967 <para></para>
968 <para></para>
969 <note>
970 <para><filename></filename></para>
971 </note>
972 </listitem>
973 </varlistentry>
974 <varlistentry>
975 <term>LogSQLWhichHeadersOut</term>
976 <listitem>
977 <cmdsynopsis>
978 <command></command>
979 <arg></arg>
980 </cmdsynopsis>
981 <simpara></simpara>
982 <simpara></simpara>
983 <simpara></simpara>
984 <para></para>
985 <para></para>
986 <note>
987 <para><filename></filename></para>
988 </note>
989 </listitem>
990 </varlistentry>
991 <varlistentry>
992 <term>LogSQLWhichNotes</term>
993 <listitem>
994 <cmdsynopsis>
995 <command></command>
996 <arg></arg>
997 </cmdsynopsis>
998 <simpara></simpara>
999 <simpara></simpara>
1000 <simpara></simpara>
1001 <para></para>
1002 <para></para>
1003 <note>
1004 <para><filename></filename></para>
1005 </note>
1006 </listitem>
1007 </varlistentry>
1008 </variablelist>
1009 </sect2>
1010 </sect1>
1011 <sect1>
1012 <title>FAQ</title>
1013 <qandaset>
1014 <qandadiv>
1015 <title>General module questions</title>
1016 <qandaentry>
1017 <question>
1018 <para>Why log to an SQL database?</para>
1019 </question>
1020 <answer>
1021 <para>To begin with, let's get it out of the way: logging to a database is not a panacea. But while there are complexities with this solution, the benefit can be substantial for certain classes of administrator or people with advanced requirements:</para>
1022 <itemizedlist>
1023 <listitem>
1024 <para>Chores like log rotation go away, as you can DELETE records from the SQL database once they are no longer useful. For example, the excellent and popular log-analysis tool Webalizer ( does not need historic logs after it has processed them, enabling you to delete older logs.</para>
1025 </listitem>
1026 <listitem>
1027 <para>People with clusters of web servers (for high availability) will benefit the most - all their webservers can log to a single SQL database. This obviates the need to collate/interleave the many separate logfiles, which can be / highly/ problematic.</para>
1028 </listitem>
1029 <listitem>
1030 <para>People acquainted with the power of SQL SELECT statements will know the flexibility of the extraction possibilities at their fingertips.</para>
1031 </listitem>
1032 </itemizedlist>
1033 <para>For example, do you want to see all your 404's? Do this:</para>
1034 <screen>select remote_host,status,request_uri,bytes_sent,from_unixtime(time_stamp) from acc_log_tbl where status=404 order by time_stamp;</screen>
1035 <table>
1036 <title></title>
1037 <tgroup cols="5">
1038 <colspec colname="1"/>
1039 <colspec colname="2"/>
1040 <colspec colname="3"/>
1041 <colspec colname="4"/>
1042 <colspec colname="5"/>
1043 <thead>
1044 <row>
1045 <entry colname="1">remote_host</entry>
1046 <entry colname="2">status</entry>
1047 <entry colname="3">request_uri</entry>
1048 <entry colname="4">bytes_sent</entry>
1049 <entry colname="5">from_unixtime(time_stamp)</entry>
1050 </row>
1051 </thead>
1052 <tbody>
1053 <row>
1054 <entry colname="1"></entry>
1055 <entry colname="2">404</entry>
1056 <entry colname="3">/favicon.ico</entry>
1057 <entry colname="4">321</entry>
1058 <entry colname="5">2001-11-20 02:30:56</entry>
1059 </row>
1060 <row>
1061 <entry colname="1"></entry>
1062 <entry colname="2">404</entry>
1063 <entry colname="3">/favicon.ico</entry>
1064 <entry colname="4">333</entry>
1065 <entry colname="5">2001-11-20 02:45:25</entry>
1066 </row>
1067 <row>
1068 <entry colname="1"></entry>
1069 <entry colname="2">404</entry>
1070 <entry colname="3">/favicon.ico</entry>
1071 <entry colname="4">321</entry>
1072 <entry colname="5">2001-11-20 03:01:00</entry>
1073 </row>
1074 <row>
1075 <entry colname="1"></entry>
1076 <entry colname="2">404</entry>
1077 <entry colname="3">/favicon.ico</entry>
1078 <entry colname="4">333</entry>
1079 <entry colname="5">2001-11-20 03:26:05</entry>
1080 </row>
1081 </tbody>
1082 </tgroup>
1083 </table>
1084 <para>Or do you want to see how many bytes you've sent within a certain directory or site? Do this:</para>
1085 <screen>select request_uri,sum(bytes_sent) as bytes,count(request_uri) as howmany from acc_log_tbl where request_uri like '%mod_log_sql%' group by request_uri order by howmany desc;</screen>
1086 <table>
1087 <title></title>
1088 <tgroup cols="3">
1089 <colspec colname="1"/>
1090 <colspec colname="2"/>
1091 <colspec colname="3"/>
1092 <thead>
1093 <row>
1094 <entry colname="1">request_uri</entry>
1095 <entry colname="2">bytes</entry>
1096 <entry colname="3">howmany</entry>
1097 </row>
1098 </thead>
1099 <tbody>
1100 <row>
1101 <entry colname="1">/mod_log_sql/style_1.css</entry>
1102 <entry colname="2">157396 </entry>
1103 <entry colname="3">1288</entry>
1104 </row>
1105 <row>
1106 <entry colname="1">/mod_log_sql/</entry>
1107 <entry colname="2">2514337</entry>
1108 <entry colname="3">801</entry>
1109 </row>
1110 <row>
1111 <entry colname="1">/mod_log_sql/mod_log_sql.tar.gz</entry>
1112 <entry colname="2">9769312</entry>
1113 <entry colname="3">456</entry>
1114 </row>
1115 <row>
1116 <entry colname="1">/mod_log_sql/faq.html</entry>
1117 <entry colname="2">5038728</entry>
1118 <entry colname="3">436</entry>
1119 </row>
1120 </tbody>
1121 </tgroup>
1122 </table>
1123 <para>Or maybe you want to see who's linking to you? Do this:</para>
1124 <screen>select count(referer) as num,referer from acc_log_tbl where request_uri='/mod_log_sql/' group by referer order by num desc;</screen>
1125 <table>
1126 <title></title>
1127 <tgroup cols="2">
1128 <colspec colname="1"/>
1129 <colspec colname="2"/>
1130 <thead>
1131 <row>
1132 <entry colname="1">num</entry>
1133 <entry colname="2">referer</entry>
1134 </row>
1135 </thead>
1136 <tbody>
1137 <row>
1138 <entry colname="1">271</entry>
1139 <entry colname="2"></entry>
1140 </row>
1141 <row>
1142 <entry colname="1">96</entry>
1143 <entry colname="2"> </entry>
1144 </row>
1145 <row>
1146 <entry colname="1">48</entry>
1147 <entry colname="2"></entry>
1148 </row>
1149 <row>
1150 <entry colname="1">8</entry>
1151 <entry colname="2"></entry>
1152 </row>
1153 </tbody>
1154 </tgroup>
1155 </table>
1156 <para>As you can see, there are myriad possibilities that can be constructed with the wonderful SQL SELECT statement. Logging to an SQL database can be really quite useful!</para>
1157 </answer>
1158 </qandaentry>
1159 <qandaentry>
1160 <question>
1161 <para>Why use MySQL? Are there alternatives?</para>
1162 </question>
1163 <answer>
1164 <para>MySQL is a robust, free, and very powerful production-quality database engine. It is well supported and comes with detailed documentation. Many 3rd-party software pacakges (e.g. Slashcode, the engine that powers Slashdot) run exclusively with MySQL. In other words, you will belong to a very robust and well-supported community by choosing MySQL.</para>
1165 <para>That being said, there are alternatives. PostgreSQL is probably MySQL's leading "competitor" in the free database world. There is also an excellent module available for Apache to permit logging to a PostgreSQL database, called<ulink url="">pgLOGd</ulink></para>
1166 </answer>
1167 <answer>
1168 <remark>Currently a database abstraction system is in the works to allow any database to be used with mod_log_sql.</remark>
1169 </answer>
1170 </qandaentry>
1171 <qandaentry>
1172 <question>
1173 <para>Is this code production-ready?</para>
1174 </question>
1175 <answer>
1176 <para>By all accounts it is. It is known to work without a problem on many-thousands-of-hits-per-day webservers. Does that mean it is 100% bug free? Well, no software is, but it is well-tested and believed to be fully compatible with production environments. (The usual disclaimers apply. This software is provided without warranty of any kind.)</para>
1177 </answer>
1178 </qandaentry>
1179 <qandaentry>
1180 <question>
1181 <para>Who's using mod_log_sql?</para>
1182 </question>
1183 <answer>
1184 <para>Good question! It would be great to find out! If you are a production-level mod_log_sql user, please contact [(||the maintainer, Chris Powell] so that you can be mentioned here.</para>
1185 </answer>
1186 </qandaentry>
1187 <qandaentry>
1188 <question>
1189 <para>Why doesn't the module also replace the Apache ErrorLog?</para>
1190 </question>
1191 <answer>
1192 <para>There are circumstances when that would be quite unwise -- for example, if Apache could not reach the MySQL server for some reason and needed to log that fact. Without a text-based error log you'd never know anything was wrong, because Apache would be trying to log a database connection error to the database... you get the point.</para>
1193 </answer>
1194 <answer>
1195 <para>Error logs are usually not very high-traffic and are really best left as text files on a web server machine.</para>
1196 </answer>
1197 <answer>
1198 <para>The Error log is free format text.. (no specified formatting what, so ever) which is rather difficult to nicely format for storing in a database.</para>
1199 </answer>
1200 </qandaentry>
1201 <qandaentry>
1202 <question>
1203 <para>Does mod_log_sql work with Apache 2.x?</para>
1204 </question>
1205 <answer>
1206 <para>Yes. A port of mod_log_sql is available for Apache 2.x, and is currently available and maintained at <ulink url="">OutOfOrder.CC</ulink></para>
1207 </answer>
1208 </qandaentry>
1209 <qandaentry>
1210 <question>
1211 <para>Does mod_log_sql connect to MySQL via TCP/IP or a socket?</para>
1212 </question>
1213 <answer>
1214 <para>Quick answer, Yes.</para>
1215 </answer>
1216 <answer>
1217 <para>It depends! This is not determined by mod_log_sql. mod_log_sql relies on a connection command that is supplied in the MySQL API, and that command is somewhat intelligent. How it works:</para>
1218 <itemizedlist>
1219 <listitem>
1220 <simpara>if the specified MySQL database is on the same machine, the connection command uses a socket to communicate with MySQL</simpara>
1221 </listitem>
1222 <listitem>
1223 <simpara>if the specified MySQL database is on a different machine, mod_log_sql connects using TCP/IP. </simpara>
1224 </listitem>
1225 </itemizedlist>
1226 <para>You don't have any control of which methodology is used. You can fine-tune some of the configuration, however. The LogSQLSocketFile runtime configuration directive overrides the default of "/var/lib/mysql/mysql.sock" for socket-based connections, whereas the LogSQLTCPPort command allows to you override the default TCP port of 3306 for TCP/IP connections.</para>
1227 </answer>
1228 </qandaentry>
1229 <qandaentry>
1230 <question>
1231 <para>I have discovered a bug. Who can I contact?</para>
1232 </question>
1233 <answer>
1234 <para>Please contact [(||the maintainer], or post a message to the mod_log_sql mailing list. Your comments, suggestions, bugfixes, bug catches, and usage testimonials are always welcome. As free software, mod_log_sql is intended to be a community effort -- any code contributions or other ideas will be fully and openly credited, of course.</para>
1235 </answer>
1236 </qandaentry>
1237 </qandadiv>
1238 <qandadiv>
1239 <title>Problems</title>
1240 <qandaentry>
1241 <question>
1242 <para>Apache segfaults or has other problems when using PHP and mod_log_sql</para>
1243 </question>
1244 <answer>
1245 <para>This occurs if you compiled PHP with MySQL database support. PHP utilizes its internal, bundled MySQL libraries by default. These conflict with the "real" MySQL libraries linked by mod_log_sql, causing the segmentation fault.</para>
1246 <para>PHP and mod_log_sql can be configured to happily coexist. The solution is to configure PHP to link against the real MySQL libraries: recompile PHP using --with-mysql=/your/path. Apache will run properly once the modules are all using the same version of the MySQL libraries.</para>
1247 </answer>
1248 </qandaentry>
1249 <qandaentry>
1250 <question>
1251 <para>&lt;faq:NothingLogged&gt;Apache appears to start up fine, but nothing is getting logged in the database</para>
1252 </question>
1253 <answer>
1254 <para>If you do not see any entries in the access_log, then something is preventing the inserts from happening. This could be caused by several things:</para>
1255 <itemizedlist>
1256 <listitem>
1257 <simpara>Improper privileges set up in the MySQL database </simpara>
1258 </listitem>
1259 <listitem>
1260 <simpara>You aren't hitting a VirtualHost that has a LogSQLTransferLogTable entry </simpara>
1261 </listitem>
1262 <listitem>
1263 <simpara>You didn't specify the right database host or login information</simpara>
1264 </listitem>
1265 <listitem>
1266 <simpara>Another factor is preventing a connection to the database</simpara>
1267 </listitem>
1268 </itemizedlist>
1269 <remark>It is improper to ask for help before you have followed these steps.</remark>
1270 <para>First examine the MySQL log that you established in step [step:EnaLog] of section [sub:PrepDb]. Ensure that the INSERT statements are not being rejected because of a malformed table name or other typographical error. By enabling that log, you instructed MySQL to log every connection and command it receives -- if you see no INSERT attempts in the log, the module isn't successfully connecting to the database. If you see nothing at all in the log -- not even a record of your administrative connection attempts, then you did not enable the log correctly. If you do see INSERT attempts but they are failing, the log should tell you why.</para>
1271 <para>Second, confirm that your LogSQL* directives are all correct.</para>
1272 <para>Third, examine the Apache error logs for messages from mod_log_sql; the module will offer hints as to why it cannot connect, etc. </para>
1273 <para>The next thing to do is to change the LogLevel directive <emphasis>in the main server config as well as in each VirtualHost config:</emphasis></para>
1274 <programlisting>LogLevel debug
1275ErrorLog /var/log/httpd/server-messages </programlisting>
1276 </answer>
1277 </qandaentry>
1278 <qandaentry>
1279 <question>
1280 <para>Why do I get the message "insufficient configuration info to establish database link" in my Apache error log?</para>
1281 </question>
1282 <answer>
1283 <para>At a minimum, LogSQLDatabase and LogSQLLoginInfo and either LogSQLTableName or LogSQLMassVirtualHosting must be defined in order for the module to be able to establish a database link. If these are not defined or are incomplete you will receive this error message.</para>
1284 </answer>
1285 </qandaentry>
1286 <qandaentry>
1287 <question>
1288 <para>My database cannot handle all the open connections from mod_log_sql, is there anything I can do?</para>
1289 </question>
1290 <answer>
1291 <para>The rule of thumb: if you have n webservers each configured to support y MaxClients, then your database must be able to handle n\times y simultenous connections in the worst case. Certainly you must use common sense, consider reasonable traffic expectations and structure things accordingly.</para>
1292 </answer>
1293 <answer>
1294 <para>Tweaking my.cnf to scale to high connection loads is imperative. But if hardware limitations prevent your MySQL server from gracefully handling the number of incoming connections, it would be beneficial to upgrade the memory or CPU on that server in order to handle the load. </para>
1295 </answer>
1296 <answer>
1297 <para>Jeremy Zawodny, a highly respected MySQL user and contributor to Linux Magazine, has this very helpful and highly appropriate article on tuning MySQL: <ulink url="">MySQL, Linux, and Thread Caching</ulink></para>
1298 </answer>
1299 <answer>
1300 <para><abbrev></abbrev>Please remember that mod_log_sql's overriding principle is performance -- that is what the target audience demands and expects. Other database logging solutions do not open and maintain many database connections, but their performance suffers drastically. For example, pgLOGd funnels all log connections through a separate daemon that connects to the database, but that bottlenecks the entire process. mod_log_sql achieves performance numbers an order of magnitude greater than the alternatives because it dispenses with the overhead associated with rapid connection cycling, and it doesn't attempt to shoehorn all the database traffic through a single extra daemon or proxy process.</para>
1301 </answer>
1302 <answer>
1303 <remark>Currently connection pooling is being implemented as part of the Database Abstraction layer to allow multiple httpd processes to share connections.</remark>
1304 </answer>
1305 </qandaentry>
1306 <qandaentry>
1307 <question>
1308 <para>Why do I occasionally see a "lost connection to MySQL server" message in my Apache error log?</para>
1309 </question>
1310 <answer>
1311 <para>This message may appear every now and then in your Apache error log, especially on very lightly loaded servers. This doesn't mean that anything is necessarily wrong. Within each httpd child process, mod_log_sql will open (and keep open) a connection to the MySQL server. MySQL, however, will close connections that haven't been used in a while; the default timeout is 8 hours. When this occurs, mod_log_sql will notice and re-open the connection. That event is what is being logged, and looks like this:</para>
1312 <screen>[Tue Nov 12 19:04:10 2002] [error] mod_log_sql: first attempt failed,
1313 API said: error 2013, Lost connection to MySQL server during query
1314[Tue Nov 12 19:04:10 2002] [error] mod_log_sql: reconnect successful
1315[Tue Nov 12 19:04:10 2002] [error] mod_log_sql: second attempt successful</screen>
1316 <para>Reference: <ulink url="">MySQL documentation</ulink></para>
1317 </answer>
1318 </qandaentry>
1319 <qandaentry>
1320 <question>
1321 <para>Sometimes a single VirtualHost gets logged to two different tables (e.g. access_foo_com, access_www_foo_com). Or, accesses to an unqualified hostname (e.g. "http://intranet/index.html") get logged in separate tables.</para>
1322 </question>
1323 <answer>
1324 <para>Proper usage of the Apache runtime ServerName directive and the directive UseCanonicalName On (or DNS) are necessary to prevent this problem. "On" is the default for UseCanonicalName, and specifies that self-referential URLs are generated from the ServerName part of your VirtualHost:</para>
1325 <para>With UseCanonicalName on (and in all versions prior to 1.3) Apache will use the ServerName and Port directives to construct the canonical name for the server. With UseCanonicalName off Apache will form self-referential URLs using the hostname and port supplied by the client if any are supplied (otherwise it will use the canonical name, as defined above). [From <ulink url="">the Apache documentation</ulink>]</para>
1326 <para>The module inherits Apache's "knowledge" about the server name being accessed. As long as those two directives are properly configured, mod_log_sql will log to only one table per virtual host while using LogSQLMassVirtualHosting.</para>
1327 </answer>
1328 </qandaentry>
1329 </qandadiv>
1330 <qandadiv>
1331 <title>Performance and Tuning</title>
1332 <qandaentry>
1333 <question>
1334 <para>How well does it perform?</para>
1335 </question>
1336 <answer>
1337 <para>mod_log_sql scales to very high loads. Apache 1.3.22 + mod_log_sql was benchmarked using the "ab" (Apache Bench) program that comes with the Apache distribution; here are the results.</para>
1338 <itemizedlist>
1339 <title>Overall configuration</title>
1340 <listitem>
1341 <simpara>Machine A: Apache webserver</simpara>
1342 </listitem>
1343 <listitem>
1344 <simpara>Machine B: MySQL server</simpara>
1345 </listitem>
1346 <listitem>
1347 <simpara>Machines A and B connected with 100Mbps Ethernet</simpara>
1348 </listitem>
1349 <listitem>
1350 <simpara>Webserver: Celeron 400, 128MB RAM, IDE storage</simpara>
1351 </listitem>
1352 </itemizedlist>
1353 <example>
1354 <title>Apache configuration</title>
1355 <programlisting format="linespecific">Timeout 300
1356KeepAlive On
1357MaxKeepAliveRequests 100
1358KeepAliveTimeout 15
1359MinSpareServers 5
1360StartServers 10
1361MaxSpareServers 15
1362MaxClients 256
1363MaxRequestsPerChild 5000
1364LogSQLTransferLogFormat AbHhmRSsTUuvc
1365LogSQLWhichCookie Clicks
1366CookieTracking on
1367CookieName Clicks</programlisting>
1368 </example>
1369 <example>
1370 <title>"ab" commandline</title>
1371 <screen format="linespecific">./ab -c 10 -t 20 -v 2 -C Clicks=ab_run
1372 </screen>
1373 </example>
1374 <para>( 10 concurrent requests; 20 second test; setting a cookie "Clicks=ab_run"; target = the mod_log_sql homepage. )</para>
1375 <para>Ten total ab runs were conducted: five with MySQL logging enabled, and five with all MySQL directives commented out of httpd.conf. Then each five were averaged. The results:</para>
1376 <itemizedlist>
1377 <listitem>
1378 <simpara>Average of five runs employing MySQL and standard text logging: <emphasis>139.01 requests per second, zero errors.</emphasis></simpara>
1379 </listitem>
1380 <listitem>
1381 <simpara>Average of five runs employing only standard text logging: <emphasis>139.96 requests per second, zero errors.</emphasis></simpara>
1382 </listitem>
1383 </itemizedlist>
1384 <para>In other words, any rate-limiting effects on this particular hardware setup are not caused by MySQL. Note that although this very simple webserver setup is hardly cutting-edge -- it is, after all, a fairly small machine -- 139 requests per second equal over twelve million hits per day.</para>
1385 <orderedlist>
1386 <title>If you run this benchmark yourself, take note of three things:</title>
1387 <listitem>
1388 <simpara>Use a target URL that is on your own webserver :-). </simpara>
1389 </listitem>
1390 <listitem>
1391 <simpara>Wait until all your connections are closed out between runs; after several thousand requests your TCP/IP stack will be filled with hundreds of connections in TIME_WAIT that need to close. Do a "netstat -t|wc -l" on the webserver to see. If you don't wait, you can expect to see a lot of messages like "ip_conntrack: table full, dropping packet" in your logs. (This has nothing to do with mod_log_sql, this is simply the nature of the TCP/IP stack in the Linux kernel.)</simpara>
1392 </listitem>
1393 <listitem>
1394 <simpara>When done with your runs, clean these many thousands of requests out of your database:</simpara>
1395 <screen>mysql&gt; delete from access_log where agent like 'ApacheBench%';
1396mysql&gt; optimize table access_log; </screen>
1397 </listitem>
1398 </orderedlist>
1399 </answer>
1400 </qandaentry>
1401 <qandaentry>
1402 <question>
1403 <para>Do I need to be worried about all the running MySQL children? Will holding open n Apache-to-MySQL connections consume a lot of memory? </para>
1404 </question>
1405 <answer>
1406 <para>Short answer: you shouldn't be worried.</para>
1407 </answer>
1408 <answer>
1409 <para>Long answer: you might be evaluating at the output of "ps -aufxw" and becoming alarmed at all the 7MB httpd processes or 22MB mysqld children that you see. Don't be alarmed. It's true that mod_log_sql opens and holds open many MySQL connections: each httpd child maintains one open database connection (and holds it open for performance reasons). Four webservers, each running 20 Apache children, will hold open 80 MySQL connections, which means that your MySQL server needs to handle 80 simultaneous connections. In truth, your MySQL server needs to handle far more than that if traffic to your website spikes and the Apache webservers spawn off an additional 30 children each...</para>
1410 <para>Fortunately the cost reported by 'ps -aufxw' is deceptive. This is due to an OS memory-management feature called "copy-on-write." When you have a number of identical child processes (e.g. Apache, MySQL), it would appear in "ps" as though each one occupies a great deal of RAM -- as much as 7MB per httpd child! In actuality each additional child only occupies a small bit of extra memory -- most of the memory pages are common to each child and therefore shared in a "read-only" fashion. The OS can get away with this because the majority of memory pages for one child are identical across all children. Instead of thinking of each child as a rubber stamp of the others, think of each child as a basket of links to a common memory area.</para>
1411 <para>A memory page is only duplicated when it needs to be written to, hence "copy-on-write." The result is efficiency and decreased memory consumption. "ps" may report 7MB per child, but it might really only "cost" 900K of extra memory to add one more child. It is not correct to assume that 20 Apache children with a VSZ of 7MB each equals (2 x 7MB) of memory consumption -- the real answer is much, much lower. The same "copy-on-write" rules apply to all your MySQL children: 40 mysqld children @ 22MB each do not occupy 880MB of RAM.</para>
1412 <para>The bottom line: although there is a cost to spawn extra httpd or mysqld children, that cost is not as great as "ps" would lead you to believe.</para>
1413 </answer>
1414 </qandaentry>
1415 <qandaentry>
1416 <question>
1417 <para>My webserver cannot handle all the traffic that my site receives, is there anything I can do?</para>
1418 </question>
1419 <answer>
1420 <para>If you have exhausted all the tuning possibilities on your existing server, it is probably time you evaluated the benefits of clustering two or more webservers together in a load-balanced fashion. In fact, users of such a setup are mod_log_sql's target audience!</para>
1421 </answer>
1422 </qandaentry>
1423 <qandaentry>
1424 <question>
1425 <para>&lt;sub:DelayedInsFAQ&gt;What is the issue with activating delayed inserts?</para>
1426 </question>
1427 <answer>
1428 <para>INSERT DELAYED is a specific syntax to MySQL and is not supported by any other database. Ergo, why is it needed, and what MySQL deficiency is it working around? INSERT DELAYED is a kluge.</para>
1429 </answer>
1430 <answer>
1431 <para>The MySQL documentation is unclear whether INSERT DELAYED is even necessary for an optimized database. It says, "The DELAYED option for the INSERT statement is a MySQL-specific option that is very useful if you have clients that can't wait for the INSERT to complete." But then it goes on to say, "Note that as MyISAM tables supports concurrent SELECT and INSERT, if there is no free blocks in the middle of the data file, you very seldom need to use INSERT DELAYED with MyISAM."</para>
1432 </answer>
1433 <answer>
1434 <para>Because INSERT DELAYED returns without waiting for the data to be written, a hard kill of your MySQL database at the right (wrong?) moment could lose those logfile entries.</para>
1435 </answer>
1436 <answer>
1437 <para>As of MySQL version 3.23.52, the error return functions disagree after a failed INSERT DELAYED: mysql_errno() always returns 0, even if mysql_error() returns a textual error. I have reported this bug to the MySQL folks. However, we have no way of knowing what solution they will adopt to fix this, and with the worst case solution mod_log_sql would not be able to tell if anything went wrong with a delayed insert.</para>
1438 </answer>
1439 <answer>
1440 <para>Instead of delayed inserts, you may wish to utilize InnoDB tables (instead of the standard MyISAM tables). InnoDB tables suppot row-level locking and are recommended for high-volume databases.</para>
1441 </answer>
1442 <answer>
1443 <para>If after understanding these problems you still wish to enable delayed inserts, section [sub:DelayedIns] discusses how.</para>
1444 </answer>
1445 </qandaentry>
1446 </qandadiv>
1447 <qandadiv>
1448 <title>"How do I...?" -- accomplishing certain tasks</title>
1449 <qandaentry>
1450 <question>
1451 <para>How do I extract the data in a format that my analysis tool can understand?</para>
1452 </question>
1453 <answer>
1454 <para>mod_log_sql would be virtually useless if there weren't a way for you to extract the data from your database in a somewhat meaningful fashion. To that end there's a Perl script enclosed with the distribution. That script ( is designed to extract N-many days worth of access logs and provide them in a Combined Log Format output. You can use this very tool right in /etc/crontab to extract logs on a regular basis so that your favorite web analysis tool can read them. Or you can examine the Perl code to construct your own custom tool.</para>
1455 <para>For example, let's say that you want your web statistics updated once per day in the wee hours of the morning. A good way to accomplish that could be the following entries in /etc/crontab:</para>
1456 <programlisting># Generate the temporary apache logs from the MySQL database (for webalizer)
145705 04 * * * root 1 &gt; /var/log/temp01
1458# Run webalizer on httpd log
145930 04 * * * root webalizer -c /etc/webalizer.conf; rm -f /var/log/temp01</programlisting>
1460 <para>Or if you have a newer system that puts files in /etc/cron.daily etc., create a file called "webalizer" in the cron.daily subdirectory. Use the following as the contents of your file, and make sure to chmod 755 it when done.</para>
1461 <programlisting>#!/bin/sh
1462/usr/local/sbin/ 1 &gt; /var/log/httpd/templog
1463/usr/local/bin/webalizer -q -c /etc/webalizer.conf
1464rm -f /var/log/httpd/templog</programlisting>
1465 <para>See? Easy.</para>
1466 </answer>
1467 </qandaentry>
1468 <qandaentry>
1469 <question>
1470 <para>&lt;sec:cookie&gt;How can I log mod_usertrack cookies?</para>
1471 </question>
1472 <answer>
1473 <para>A number of people like to log mod_usertrack cookies in their Apache TransferLog to aid in understanding their visitors' clickstreams. This is accomplished, for example, with a statement as follows:</para>
1474 <programlisting>LogFormat "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\"" \"%{cookie}n\""</programlisting>
1475 <para>Naturally it would be nice for mod_log_sql to permit the admin to log the cookie data as well, so as of version 1.10 you can do this. You need to have already compiled mod_usertrack into httpd -- it's one of the standard Apache modules.</para>
1476 <para>First make sure you have a column called "cookie" in the MySQL database to hold the cookies, which can be done as follows if you already have a working database:</para>
1477 <screen>mysql&gt; alter table acc_log_tbl add column cookie varchar(255);</screen>
1478 <para>Next configure your server to set usertracking cookies as follows, and make sure you include the new 'c' directive in your LogSQLTransferLogFormat, which activates cookie logging. Here's an example:</para>
1479 <programlisting>&lt;VirtualHost;
1480 CookieTracking on
1481 CookieStyle Cookie
1482 CookieName Foobar
1483 LogSQLTransferLogFormat huSUsbTvRAc
1484 LogSQLWhichCookie Foobar
1486 <para>The first three lines configure mod_usertrack to create a COOKIE (RFC 2109) format cookie called Foobar. The last two lines tell mod_log_sql to log cookies named Foobar. You have to choose which cookie to log because more than one cookie can/will be sent to the server by the client.</para>
1487 <para>Recap: the 'c' character activates cookie logging, and the LogSQLWhichCookie directive chooses which cookie to log.</para>
1488 <para>FYI, you are advised NOT to use CookieStyle Cookie2 -- it seems that even newer browsers (IE 5.5, etc.) have trouble with the new COOKIE2 (RFC 2965) format. Just stick with the standard COOKIE format and you'll be fine.</para>
1489 <para>Perform some hits on your server and run a select</para>
1490 <screen format="linespecific">mysql&gt; select request_uri,cookie from access_log where cookie is not null;</screen>
1491 <table>
1492 <title></title>
1493 <tgroup cols="2">
1494 <colspec colname="1"/>
1495 <colspec colname="2"/>
1496 <thead>
1497 <row>
1498 <entry colname="1">request_uri</entry>
1499 <entry colname="2">cookie</entry>
1500 </row>
1501 </thead>
1502 <tbody>
1503 <row>
1504 <entry colname="1">/mod_log_sql/</entry>
1505 <entry colname="2"></entry>
1506 </row>
1507 <row>
1508 <entry colname="1">/mod_log_sql/usa.gif</entry>
1509 <entry colname="2"></entry>
1510 </row>
1511 <row>
1512 <entry colname="1">/mod_log_sql/style_1.css</entry>
1513 <entry colname="2"></entry>
1514 </row>
1515 </tbody>
1516 </tgroup>
1517 </table>
1518 </answer>
1519 </qandaentry>
1520 <qandaentry>
1521 <question>
1522 <para>What if I want to log more than one cookie? What is the difference between LogSQLWhichCookie and LogSQLWhichCookies?</para>
1523 </question>
1524 <answer>
1525 <para>As of version 1.17, you have a choice in how you want cookie logging handled.</para>
1526 <para>If you are interested in logging only one cookie per request, follow the instructions in section [sec:cookie] above. That cookie will be logged to a column in the regular access_log table, and the actual cookie you want to log is specified with LogSQLWhichCookie. Don't forget to specify the 'c' character in LogSQLTransferLogFormat.</para>
1527 <para>If, however, you need to log multiple cookies per request, you must employ the LogSQLWhichCookies (note the plural) directive. The cookies you specify will be logged to a separate table (as discussed in section [secMulTable]), and entries in that table will be linked to the regular access_log entries via the unique ID that is supplied by mod_unique_id. Without mod_unique_id the information will still be logged but you will be unable to correlate which cookies go with which access-requests. Furthermore, with LogSQLWhichCookies, you do not need to include the 'c' character in LogSQLTransferLogFormat.</para>
1528 <para>LogSQLWhichCookie and LogSQLWhichCookies can coexist without conflict because they operate on entireley different tables, but you're better off choosing the one you need.</para>
1529 </answer>
1530 </qandaentry>
1531 <qandaentry>
1532 <question>
1533 <para>What are the SSL logging features, and how do I activate them?</para>
1534 </question>
1535 <answer>
1536 <note>
1537 <para>You do not need to compile SSL support into mod_log_sql in order to simply use it with a secure site. You only need to compile SSL support into mod_log_sql if you want to log SSL-specific data such as the cipher type used, or the keysize that was negotiated. If that information is unimportant to you, you can ignore this FAQ.</para>
1538 </note>
1539 <para>By adding certain characters to your LogSQLTransferLogFormat string you can tell mod_log_sql to log the SSL cipher, the SSL keysize of the connection, and the maximum keysize that was available. This would let you tell, for example, which clients were using only export-grade security to access your secure software area.</para>
1540 <para>You can compile mod_log_sql with SSL logging support if you have the right packages installed. If you already have an SSL-enabled Apache then you by definition have the correct packages already installed: OpenSSL and mod_ssl.</para>
1541 <para>You need to ensure that your database is set up to log the SSL data. Issue the following commands to MySQL if your access table does not already have them:</para>
1542 <screen>mysql&gt; alter table access_log add column ssl_cipher varchar(25);
1543mysql&gt; alter table access_log add column ssl_keysize smallint unsigned;
1544mysql&gt; alter table access_log add column ssl_maxkeysize smallint unsigned;</screen>
1545 <para>Finally configure httpd.conf to activate the SSL fields. Note that this is only meaningful in a VirtualHost that is set up for SSL.</para>
1546 <programlisting>&lt;VirtualHost;
1547 LogSQLTransferLogFormat AbHhmRSsTUuvcQqz
1549 <para>You also need to make sure you have the mod_log_sql_ssl module loaded as well.</para>
1550 <para>The last three characters (Qqz) in the directive are the SSL ones; see section [sub:Frmat] in the directives documentation for details of the LogSQLTransferLogFormat directive.</para>
1551 <para>Restart Apache, then perform some hits on your server. Then run the following select statement:</para>
1552 <screen>mysql&gt; select remote_host,request_uri,ssl_cipher,ssl_keysize,ssl_maxkeysize from access_log where ssl_cipher is not null;</screen>
1553 <table>
1554 <title></title>
1555 <tgroup cols="5">
1556 <colspec colname="1"/>
1557 <colspec colname="2"/>
1558 <colspec colname="3"/>
1559 <colspec colname="4"/>
1560 <colspec colname="5"/>
1561 <thead>
1562 <row>
1563 <entry colname="1">remote_host</entry>
1564 <entry colname="2">request_uri</entry>
1565 <entry colname="3">ssl_cipher</entry>
1566 <entry colname="4">ssl_keysize</entry>
1567 <entry colname="5">ssl_maxkeysize</entry>
1568 </row>
1569 </thead>
1570 <tbody>
1571 <row>
1572 <entry colname="1"></entry>
1573 <entry colname="2">/dir/somefile.html</entry>
1574 <entry colname="3">RC4-MD5</entry>
1575 <entry colname="4">128</entry>
1576 <entry colname="5">128</entry>
1577 </row>
1578 <row>
1579 <entry colname="1"></entry>
1580 <entry colname="2">/dir/somefile.gif</entry>
1581 <entry colname="3">RC4-MD5</entry>
1582 <entry colname="4">128</entry>
1583 <entry colname="5">128</entry>
1584 </row>
1585 <row>
1586 <entry colname="1"></entry>
1587 <entry colname="2">/dir/somefile.jpg</entry>
1588 <entry colname="3">RC4-MD5</entry>
1589 <entry colname="4">128</entry>
1590 <entry colname="5">128</entry>
1591 </row>
1592 </tbody>
1593 </tgroup>
1594 </table>
1595 </answer>
1596 </qandaentry>
1597 </qandadiv>
1598 </qandaset>
1599 </sect1>