{"id":388,"date":"2012-03-18T17:24:30","date_gmt":"2012-03-18T22:24:30","guid":{"rendered":"http:\/\/www.linuxpoweruser.com\/?p=388"},"modified":"2012-03-18T17:24:30","modified_gmt":"2012-03-18T22:24:30","slug":"enabling-mysql-logging-on-archlinux","status":"publish","type":"post","link":"https:\/\/www.linuxpoweruser.com\/?p=388","title":{"rendered":"Enabling MySQL Logging on Archlinux"},"content":{"rendered":"<p>This morning I was doing some troubleshooting on a PHP program that couldn&#8217;t seem to access the locally running MySQL database server. Upon searching for some logging to figure out what was going on, I came across the fact the MySQL as installed from the Arch repositories, doesn&#8217;t enable much along theses lines.<\/p>\n<p>MySQL supports error logging to \/var\/lib\/mysql directory by default.  The file is usually in the form <hostname>.err.  This file shows mysqld startup errors and other basic troubleshooting information.  I was looking for something a little more in depth, such as client connections, and SQL statement logging.  Apparently this type of logging is referred to as general logging.  After some experimentation, it appears that enabling this log requires a few simple system changes.<\/p>\n<p><strong>Enabling general logging for MySQL<\/strong><\/p>\n<p>The first step is to figure out where you want MySQL to store its general log.  In our example, we will put it in the \/var\/log\/mysql directory, in file called general.log.  This directory and file must be created, and permissions set so that mysql can write to it.  MySQL will <em>not<\/em> create this file on its own.  So lets create the directory and file with the following commands:<\/p>\n<p><code>sudo mkdir \/var\/log\/mysql<br \/>\nsudo touch \/var\/log\/mysql\/general.log<br \/>\nsudo chown mysql:mysql -R \/var\/log\/mysql<br \/>\n<\/code><\/p>\n<p>Then, in order tell MySQL to enable general logging, a couple of lines so be added to the \/etc\/mysql\/my.cnf file in the [mysqld] section &#8211; see last two lines in the example my.cnf file below:<br \/>\n<code><br \/>\n# The MySQL server<br \/>\n[mysqld]<br \/>\nport            = 3306<br \/>\nsocket          = \/var\/run\/mysqld\/mysqld.sock<br \/>\ndatadir         = \/var\/lib\/mysql<br \/>\nskip-external-locking<br \/>\nkey_buffer_size = 16M<br \/>\nmax_allowed_packet = 1M<br \/>\ntable_open_cache = 64<br \/>\nsort_buffer_size = 512K<br \/>\nnet_buffer_length = 8K<br \/>\nread_buffer_size = 256K<br \/>\nread_rnd_buffer_size = 512K<br \/>\nmyisam_sort_buffer_size = 8M<br \/>\n<strong>general_log = 1<br \/>\ngeneral_log_file = \/var\/log\/mysql\/general.log<\/code><br \/>\n<\/strong><\/p>\n<p>That&#8217;s all it takes!  Save the my.cnf file, and restart mysqld with:<\/p>\n<p><code>\/etc\/rc.d\/mysqld restart<\/code><\/p>\n<p>You should now the general.log file in \/var\/log\/mysql.  An example might look like this (showing some client connections and select statements):<\/p>\n<p><code>\/usr\/bin\/mysqld, Version: 5.5.21-log (Source distribution). started with:<br \/>\nTcp port: 3306  Unix socket: \/var\/run\/mysqld\/mysqld.sock<br \/>\nTime                 Id Command    Argument<br \/>\n120318 10:10:26     1 Connect   UNKNOWN_USER@localhost as  on<br \/>\n                    1 Quit<br \/>\n120318 10:10:39     2 Connect   root@localhost on<br \/>\n                    2 Init DB   webcalendar<br \/>\n                    2 Init DB   webcalendar<br \/>\n                    2 Query     SELECT COUNT( cal_value ) FROM webcal_config<br \/>\n                    2 Query     SELECT cal_permissions FROM webcal_access_function<br \/>\n      WHERE cal_login = 'admin'<br \/>\n                    2 Query     SELECT cal_permissions FROM webcal_access_function<br \/>\n      WHERE cal_login = '__default__'<br \/>\n                    2 Init DB   webcalendar<br \/>\n                    2 Query     SELECT cal_login, cal_lastname, cal_firstname, cal_is_admin, cal_email, cal_passwd FROM webcal_user ORDER BY cal_lastname, cal_firstname, cal_login<br \/>\n<\/code><\/p>\n<p>The above snip shows MySQL starting and an application hitting a database.<\/p>\n<p><strong>Configuring MySQL to log errors to the system log<\/strong><\/p>\n<p>That last thing I want to cover is getting MySQL to log to the system log, instead of its own log in \/var\/lib\/mysql.  I mentioned above that MySQL logs errors to \/var\/lib\/mysql\/<hostname>.err.  We will make a simple change to log those same messages to the system log, named to the daemon.log.  <\/p>\n<p>This one is pretty simple &#8211; add the following section to the \/etc\/mysql\/my.cnf file:<\/p>\n<p><code><br \/>\n[mysqld_safe]<br \/>\nsyslog<\/code><\/p>\n<p>And then restart MySQL with:<\/p>\n<p><code>\/etc\/rc.d\/mysqld restart<\/code><\/p>\n<p>You should see messages in the daemon.log file that look something like this:<\/p>\n<p><code>Mar 18 10:14:54 localhost mysqld_safe: Starting mysqld daemon with databases from \/var\/lib\/mysql<br \/>\nMar 18 10:14:54 localhost mysqld: 120318 10:14:54 InnoDB: The InnoDB memory heap is disabled<br \/>\nMar 18 10:14:54 localhost mysqld: 120318 10:14:54 InnoDB: Mutexes and rw_locks use GCC atomic builtins<br \/>\nMar 18 10:14:54 localhost mysqld: 120318 10:14:54 InnoDB: Compressed tables use zlib 1.2.6<br \/>\nMar 18 10:14:54 localhost mysqld: 120318 10:14:54 InnoDB: Initializing buffer pool, size = 128.0M<br \/>\nMar 18 10:14:54 localhost mysqld: 120318 10:14:54 InnoDB: Completed initialization of buffer pool<br \/>\nMar 18 10:14:54 localhost mysqld: 120318 10:14:54 InnoDB: highest supported file format is Barracuda.<br \/>\nMar 18 10:14:54 localhost mysqld: 120318 10:14:54  InnoDB: Waiting for the background threads to start<br \/>\nMar 18 10:14:55 localhost mysqld: 120318 10:14:55 InnoDB: 1.1.8 started; log sequence number 2218816<br \/>\nMar 18 10:14:55 localhost mysqld: 120318 10:14:55 [ERROR] \/usr\/bin\/mysqld: Can't find file: '.\/performance_schema\/events_waits_current.frm' (errno: 13)<br \/>\n<\/code><\/p>\n<p>In summary, the above changes will allow you to &#8220;see&#8221; what MySQL is doing both in terms of the daemon itself, and the actual activity being processed by the database server.  These changes could also work on other distros, but in particular ArchLinux by default enables only error logging to the MySQL directory only.      <\/p>\n<p>Up next &#8211; configuring syslog-ng as syslog server for network devices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This morning I was doing some troubleshooting on a PHP program that couldn&#8217;t seem to access the locally running MySQL database server. Upon searching for some logging to figure out what was going on, I came across the fact the MySQL as installed from the Arch repositories, doesn&#8217;t enable much along theses lines. MySQL supports [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[13,24,44,46,61],"_links":{"self":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/388"}],"collection":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=388"}],"version-history":[{"count":0,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/388\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}