{"id":272,"date":"2008-05-27T14:52:57","date_gmt":"2008-05-27T14:52:57","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=272"},"modified":"2008-05-27T16:19:58","modified_gmt":"2008-05-27T16:19:58","slug":"accessing-mysql-from-perl-on-slugos","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=272","title":{"rendered":"Accessing MySQL from Perl on SlugOS"},"content":{"rendered":"<p>I&#8217;ve written about my <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=265\" target=\"_blank\">CurrentCost meter<\/a> that I&#8217;m using to monitor my home electricity usage, and the <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=267\" target=\"_blank\">small home server<\/a> that I&#8217;ve set up to collect the data. <\/p>\n<p>Yesterday, I decided to make a start on collecting the data. My plan was to <a href=\"http:\/\/knolleary.net\/2008\/05\/05\/power-graphing\/\" target=\"_blank\">copy what Nick had done<\/a> and create a MySQL database to store the info, with a table to store a timestamp and the watt reading from the CurrentCost meter.  <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">CREATE TABLE currentcostdl (  \r\n    time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,  \r\n    power FLOAT NOT NULL,  \r\n    primary key(time));<\/pre>\n<p>I started with Nick&#8217;s Python scripts, but didn&#8217;t get very far.  <\/p>\n<p>The server, a NSLU-2 (or &#8220;slug&#8221;) is running <a href=\"http:\/\/www.nslu2-linux.org\/wiki\/SlugOS\/SlugOSLE\" target=\"_blank\">SlugOS<\/a>.  <\/p>\n<p>The problem was that there is no python-mysqldb package in the SlugOS repositories. I did try downloading the <a href=\"http:\/\/sourceforge.net\/project\/showfiles.php?group_id=22307&#038;package_id=15775\" target=\"_blank\">source for it from sourceforge<\/a> to build it myself, but struggled to get the dependencies I needed to make it &#8211; urllib in particular was a big pain.  <\/p>\n<p>I also tried the <a href=\"http:\/\/packages.debian.org\/etch\/arm\/python-mysqldb\/download\" target=\"_blank\">debian package<\/a> but again dependencies on SlugOS got in the way. <\/p>\n<p>So I gave up on that and decided to do it myself using Perl &#8211; Perl and MySQL had to be easier, right? <\/p>\n<p>Erm&#8230; not so much \ud83d\ude42   <\/p>\n<p><!--more-->In short, I ran into the same problem. There isn&#8217;t a MySQL module for Perl in the SlugOS repositories.<\/p>\n<p>And building the standard Perl ones runs into dependency problems. <\/p>\n<p>Then I found a <a href=\"http:\/\/search.cpan.org\/dist\/Net-MySQL\/MySQL.pm\" target=\"_blank\">pure Perl implementation<\/a> on CPAN. From the module description: <\/p>\n<blockquote><p>This module implements network protocol between server and client of MySQL, thus you don&#8217;t need external MySQL client library like libmysqlclient for this module to work. It means this module enables you to connect to MySQL server from some operation systems which MySQL is not ported.\n<\/p><\/blockquote>\n<p>Hurrah, this sounded like what I needed. <\/p>\n<p>So I <a href=\"http:\/\/search.cpan.org\/CPAN\/authors\/id\/O\/OY\/OYAMA\/Net-MySQL-0.09.tar.gz\" target=\"_blank\">downloaded the archive<\/a>, and unzipped it.  <\/p>\n<p>There was a Makefile.PL perl script to run. Before I could run it, I needed to install a few pre-requisite Perl modules: <\/p>\n<pre style=\"white-space: -moz-pre-wrap; border: thin solid silver; background-color: #eeeeee; padding: 0.7em; \">ipkg install perl-module-io perl-module-extutils-makemaker perl-module-extutils-makemaker-config perl-module-vars perl-module-config-heavy perl-module-autosplit perl-module-cwd perl-module-extutils-install perl-module-extutils-packlist perl-module-extutils-command perl-module-extutils-command-mm perl-module-extutils-my perl-module-extutils-manifest perl-module-extutils-mm perl-module-extutils-mm-unix perl-module-file-spec-unix perl-module-exporter-heavy perl-module-carp-heavy perl-module-file-glob perl-dev perl-module-pod-man perl-module-getopt-long perl-module-test perl-module-test-harness <\/pre>\n<p>Once run, this creates the make files you need &#8211; so it&#8217;s <code>make<\/code> and <code>make install<\/code> as normal. <\/p>\n<p>It seemed to build okay, but my first attempt to use it failed with a runtime dependency: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">dalelane@minipooter:~$ perl -e 'use Net::MySQL;' \r\nCan't locate Digest\/SHA1.pm in @INC  \r\nBEGIN failed--compilation aborted at \/usr\/local\/share\/perl\/5.8.8\/Net\/MySQL.pm line 681. \r\nCompilation failed in require at -e line 1. \r\nBEGIN failed--compilation aborted at -e line 1.<\/pre>\n<p>I needed Digest\/SHA1 &#8211; but that&#8217;s not available in the SlugOS repositories: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">dalelane@minipooter:~$ ipkg list perl-module-digest* \r\nperl-module-digest - 5.8.8-r25 - perl module digest \r\nperl-module-digest-base - 5.8.8-r25 - perl module digest-base \r\nperl-module-digest-file - 5.8.8-r25 - perl module digest-file \r\nperl-module-digest-md5 - 5.8.8-r25 - perl module digest-md5 \r\ndalelane@minipooter:~$<\/pre>\n<p>I started looking for a digest-sha1 module. I found the source for an Optware one on <a href=\"http:\/\/ipkgfind.nslu2-linux.org\/details.phtml?package=perl-digest-sha1&#038;id=26840296&#038;official=\" target=\"_blank\">ipkgfind<\/a>.  <\/p>\n<p>First attempt to build it failed because of missing dependency: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">root@minipooter:\/var\/tmp\/Digest-SHA1-2.11$ make \r\ncp SHA1.pm blib\/lib\/Digest\/SHA1.pm \r\nmake: *** No rule to make target &#96;\/usr\/share\/perl\/5.8\/ExtUtils\/typemap', needed by &#96;SHA1.c'.  Stop.<\/pre>\n<p>There is no perl-module-extutils-typemap for SlugOS, so I got my own copy of the two files I needed from CPAN: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">wget http:\/\/ftp.funet.fi\/pub\/CPAN\/src\/perl-5.8.8.tar.gz \r\ntar -xzvf perl-5.8.8.tar.gz \r\ncp perl-5.8.8\/lib\/ExtUtils\/typemap \/usr\/lib\/perl5\/5.8.8\/ExtUtils\/typemap \r\ncp perl-5.8.8\/lib\/ExtUtils\/xsubpp \/usr\/lib\/perl5\/5.8.8\/ExtUtils\/xsubpp<\/pre>\n<p>Then I edited the Digest-SHA1 Makefile so that it wouldn&#8217;t try and build them itself &#8211; replacing the XSUBPP line with: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">XSUBPP = \/usr\/share\/perl\/5.8\/ExtUtils\/xsubpp<\/pre>\n<p>Running the make for Digest-SHA1 needed one more pre-req module:  <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">ipkg install perl-module-re<\/pre>\n<p>Unfortunately, it then failed due to missing executable: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">\/bin\/sh: ccache: not found<\/pre>\n<p>I needed to find <code>ccache<\/code> for SlugOS. I couldn&#8217;t find a pre-compiled version anywhere, so I downloaded the source for <code>ccache<\/code> from <a href=\"http:\/\/ccache.samba.org\/\" target=\"_blank\">samba.org<\/a>. <\/p>\n<p>Initial attempts to compile it failed because it was missing &#8216;as&#8217; (<em>assembler?<\/em>) <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">arm-linux-gcc: error trying to exec 'as': execvp: No such file or directory<\/pre>\n<p>I hadn&#8217;t done any compiling on the slug before, and a quick attempt to compile a &#8216;Hello World&#8217; C app showed that I hadn&#8217;t got a working C compiler. From what I&#8217;ve read, SlugOS-Native is a good way to get your system set up for this. <\/p>\n<p>I downloaded SlugOS-Native from <a href=\"http:\/\/ipkgfind.nslu2-linux.org\/details.phtml?package=slugos-native&#038;official=&#038;format=\" target=\"_blank\">ipkgfind<\/a> and tried installing it. It is a tiny package that includes a ton of pre-reqs &#8211; the idea is that installing this forces <code>ipkg<\/code> to go and install everything you need for native development on the Slug. <\/p>\n<p><em>Note: I had to ignore a dependency on the monotone source control package, but that didn&#8217;t seem to be a problem &#8211; it continued to successfully install <u><strong>a lot of<\/strong><\/u> development\/compiler packages. In fact, so many that I Ctrl-C&#8217;d it after a while, as it was installing a ton of perl modules that I didn&#8217;t think I&#8217;d want.<\/em> <\/p>\n<p>With a proper development environment now set up, I was back to trying to build <code>ccache<\/code>: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">.\/configure \r\nmake \r\nmake install<\/pre>\n<p>This worked fine, so I was back to trying to make digest-sha1. It failed on another perl module dependency: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">ipkg install perl-module-extutils-mkbootstrap<\/pre>\n<p>But with that out of the way, I was then able to: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">make \r\nmake install<\/pre>\n<p>So I was back to trying out the Perl MySQL module &#8211; which had failed on the runtime dependency on Digest\/SHA1 in the first place! <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">perl -e 'use Net::MySQL;'<\/pre>\n<p>Success! <\/p>\n<p>Or so I thought. A quick perl test script to connect to my currentcost database failed: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">Can't locate auto\/Digest\/SHA1\/reset.al in @INC <\/pre>\n<p> The reset method in SHA1 called the method in Digest base.pm, so this was easily fixed by installing  <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">ipkg install perl-module-digest-base<\/pre>\n<p>And it now works fine.  <\/p>\n<p>My code to insert an update into my MySQL table looks something like this: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">use Net::MySQL; \r\n\r\nmy $mysql = Net::MySQL-&#62;new( \r\n   database =&#62; 'currentcostdb', \r\n   user     =&#62; 'dalelane', \r\n   password =&#62; 'myPassw0rd' \r\n); \r\n\r\nmy $insertquery = \"INSERT INTO currentcostdl (power) VALUES ($watts)\"; \r\n\r\n$mysql-&#62;query($insertquery); \r\n\r\nprintf \"Affected row: %d\\en\", $mysql-&#62;get_affected_rows_length; \r\n\r\n$mysql-&#62;close;<\/pre>\n<p>Hurrah &#8211; it works. <\/p>\n<p>Okay, as <a href=\"http:\/\/twitter.com\/garethj\/statuses\/820933595\" target=\"_blank\">Gareth pointed out<\/a> this is really overkill. The reason why API support for MySQL on the Slug isn&#8217;t great is probably because MySQL isn&#8217;t the best choice of DB to run! Support for other less resource-intensive databases, such as SQLite is much better, and I could have got up and running in a fraction of the time with a couple of &#8216;ipkg install&#8217; commands: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">root@minipooter:\/var\/tmp$ ipkg list | grep -i sqlite \r\nlibsqlite-bin - 2.8.17-r2 - An Embeddable SQL Database Engine \r\nlibsqlite-dbg - 2.8.17-r2 - An Embeddable SQL Database Engine \r\nlibsqlite-dev - 2.8.17-r2 - An Embeddable SQL Database Engine \r\nlibsqlite0 - 2.8.17-r2 - An Embeddable SQL Database Engine \r\nlibsqlite3-0 - 3.5.2-r0 - An Embeddable SQL Database Engine \r\nlibsqlite3-dev - 3.5.2-r0 - An Embeddable SQL Database Engine \r\npython-sqlite3 - 2.5.1-ml5 - Python Sqlite3 Database Support \r\nsqlite3 - 3.5.2-r0 - An Embeddable SQL Database Engine \r\nsqlite3-dbg - 3.5.2-r0 - An Embeddable SQL Database Engine<\/pre>\n<p>But that wouldn&#8217;t have been nearly as much fun \ud83d\ude09 <\/p>\n<p>The slug is now sending updates to the <a href=\"http:\/\/realtime.ngi.ibm.com\/powermeter\/\" target=\"_blank\">realtime server<\/a> here, and storing them in a local MySQL database.  <\/p>\n<p>Now I&#8217;ve just gotta think of something to do with the data!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve written about my CurrentCost meter that I&#8217;m using to monitor my home electricity usage, and the small home server that I&#8217;ve set up to collect the data. Yesterday, I decided to make a start on collecting the data. My plan was to copy what Nick had done and create a MySQL database to store [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[194,105,213,197,211,212,199,200],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-code","category-tech","tag-currentcost","tag-mysql","tag-nslu","tag-nslu2","tag-perl","tag-python","tag-slug","tag-slugos"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=272"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/272\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}