<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>all things cbl &#187; MySQL</title>
	<atom:link href="http://blog.cbl.us/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cbl.us</link>
	<description>Just a few random things about cbl</description>
	<lastBuildDate>Fri, 14 May 2010 17:35:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Stored Procedures&#8230;.</title>
		<link>http://blog.cbl.us/2005/12/15/stored-procedures/</link>
		<comments>http://blog.cbl.us/2005/12/15/stored-procedures/#comments</comments>
		<pubDate>Fri, 16 Dec 2005 02:15:45 +0000</pubDate>
		<dc:creator>cbl</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.cbl.us/archives/85</guid>
		<description><![CDATA[Moving from MySQL 4.1 to 5.0 was the best thing I've done this year.. 
I can't believe I have lived without SP's until MySQL 5.0.  I mean, it's like incredibly easy now to make code more efficient and productive.]]></description>
			<content:encoded><![CDATA[<p>&#8230;Are da bomb!</p>
<p>I can&#8217;t believe I have lived without SP&#8217;s until MySQL 5.0.  I mean, it&#8217;s like incredibly easy now to make code more efficient and productive.</p>
<p>Case in point. We have this Radius authentication server.  Where Dialup and DSL users on our system get authenticated. The queries to get people online have been getting more and more complex as we have grown.  First we added content filtering, then we want to add monthly timeouts for users..but then we have to account for users that have purchased extra hours for this month..  It&#8217;s honestly been a nightmare to deal with.  Oh and the biggest problem.. let me tell you..  is the fact that users are inconsistent with their login usernames.  joeuser@infowest.com could login as joeuser@infowest.com or simply joeuser.  So I have to account for that.  Not a biggie, but then we have these @netutah.com users that we got a few years back so it adds even more code to the Radius server config file.  Then to top it all off we have these &#8217;special dialup&#8217; users that are broadband accounts that need to dialup while on the road..but want to keep their DSL still connected. So, that means that there&#8217;s another exception..   ANYWAY, it turned out to be like 6 different super-big queries that the system would run max per user authentication attempt.<br />
<span id="more-85"></span><br />
Example query:</p>
<pre>
SELECT u.password,dialup.idle_timeout,if((monthlyTime > 0),\
  ((dialup.monthlyTime+(if((hours.addedTime IS NULL),0,hours.addedTime)))-\
  (if(SUM(dt.SESSIONTIME) IS NULL,0,SUM(dt.SESSIONTIME)))),dialup.session_timeout) \
  AS newSessionTimeout,dialup.simultaneous_use,if((xstop.class IS NULL),"IW.SUNSET",CONCAT(xstop.class,"")) \
  AS xstop_class, if((dialup.staticIP = 1 AND ips.locationID = 1000 AND ips.protocolID = 100 ),ips.ip,NULL) \
  AS static_ip,dialup.inacl AS acl1,dialup.outacl AS acl2 FROM authenticate.users AS u \
  LEFT JOIN authenticate.service_dialup AS dialup ON u.userID = dialup.userID \
  LEFT JOIN authenticate.service_static_ips AS ips ON u.userID = ips.userID AND ips.protocolID = 100 \
  LEFT JOIN authenticate.service_xstop AS xstop ON xstop.userID = u.userID AND xstop.statusID = 1 \
  LEFT JOIN authenticate.service_dialup_hours AS hours ON \
  (u.userID = hours.userID AND hours.monthYear = date_format(CURDATE(),'%Y-%m')) \
   LEFT JOIN radius.DATATRANSMIT AS dt ON (dt.USERNAME = u.userID AND dt.TYPE = 'DIALUP' AND \
   dt.DATE LIKE date_format(CURDATE(),'%Y-%m-%%')) \
    WHERE u.userID = '%n' AND dialup.statusID = 1 GROUP BY u.userID HAVING (newSessionTimeout >= 0);
</pre>
<p>Can we say nightmarish to maintain and update?  Especially since I have 5 or 6 of these per location with 5-7 locations to update.  Anyway, it&#8217;s been a pain..</p>
<p>Then along came MySQL 5.x with its stored procedure functionality. First I was like, &#8220;How can I reduce my code to be more easier to maintain, yet keep Radius config down to a minimum..??&#8221;</p>
<p>Well, the answer is this:</p>
<pre>
CALL radius.dialup_auth("%n","%P","IW.BRIANHEAD","DIALUP");
</pre>
<p>I have one of those for BrianHead Dialup and one for BrianHead Broadband dialup..  so two function calls PER location!!  6 to 2.. not bad for simplifying. Oh and the code for Radius is much cleaner to read now.</p>
<p>So how does the function look? Is it crazy like before? Multi-database joins and the whole 9 yards? Not really.</p>
<pre>

DROP PROCEDURE dialup_auth;
delimiter $
CREATE PROCEDURE dialup_auth(IN MYuserID VARCHAR(64), IN MYpass VARCHAR(128), IN MYclass VARCHAR(128), IN MYtype VARCHAR(32))
  DETERMINISTIC  CONTAINS SQL
  BEGIN
   DECLARE classAppend VARCHAR(12);
   DECLARE userExists INT;
   DECLARE newUser VARCHAR(64);
   DECLARE returnVal VARCHAR(64);
   DECLARE avPairs VARCHAR(255);
   DECLARE myAVNAS VARCHAR(32);
   DECLARE myCount INT;
   DECLARE hasXstop INT;
   SET userExists = 0;

   SELECT 1 INTO userExists FROM authenticate.users
      WHERE userID=MYuserID AND clearpasswd = MYpass;
   IF (userExists != 1) THEN
     SELECT 1 INTO userExists FROM authenticate.users
        WHERE userID = CONCAT(MYuserID,"@infowest.com") AND clearpasswd = MYpass;
     IF (userExists != 1) THEN
       SELECT 1 INTO userExists FROM authenticate.users
          WHERE userID = CONCAT(MYuserID,"@netutah.com") AND clearpasswd = MYpass;
       IF (userExists = 1) THEN
    	-- We have a match for @netutah.com
        SELECT CONCAT(MYuserID,"@netutah.com") INTO newUser;
        SELECT "#NU" INTO classAppend;
       ELSE
          SELECT NULL INTO returnVal;
       END IF;
     ELSEIF (userExists = 1) THEN
      -- We have a match for @infowest.com
      SELECT CONCAT(MYuserID,"@infowest.com") INTO newUser;
      SELECT "#IW" INTO classAppend;
     ELSE
       SELECT NULL INTO returnVal;
     END IF;
   ELSEIF (userExists = 1) THEN
     -- We have a match for the user as is:
     SELECT MYuserID INTO newUser;
     SELECT "" INTO classAppend;
   ELSE
     SELECT NULL INTO returnVal;
   END IF;

   IF (returnVal = NULL) THEN
     -- We were unable to find a match, so return NULL.
     SELECT NULL;
   ELSE 

     -- We found a match, now do our queries:
     IF (MYtype = "DIALUP") THEN
       SELECT
        u.password,
        d.idle_timeout,
        radius_session_timeout(newUser,MYtype,d.monthlyTime) AS newSessionTimeout,
        d.simultaneous_use,
        if((x.class IS NULL),CONCAT(MYclass,classAppend),CONCAT(xstop_class_build(newUser),classAppend)) AS xstop_class,
        get_static_ip(newUser,MYclass,MYtype) AS static_ip,
        d.inacl AS inacl,
        d.outacl AS outacl
        FROM authenticate.users AS u
        NATURAL JOIN authenticate.service_dialup AS d
        LEFT JOIN authenticate.service_xstop AS x ON (x.userID = u.userID AND x.statusID = 1)
        WHERE u.userID = newUser AND d.statusID = 1
        GROUP BY u.userID
        HAVING (newSessionTimeout >= 0);
     ELSEIF (MYtype = "BBDIAL") THEN
       SELECT
        u.password AS password,
        bb.idle_timeout AS idleTime,
        radius_session_timeout(newUser,MYtype,bb.monthlyTime) AS newSessionTimeout,
        bb.simultaneous_use+1 AS simultaneousUsage,
        if((x.class IS NULL),CONCAT(MYclass,classAppend),CONCAT(xstop_class_build(newUser),classAppend)) AS xstop_class,
        get_static_ip(newUser,MYclass,MYtype) AS static_ip,
        bb.inacl AS inacl,
        bb.outacl AS outacl
        FROM authenticate.users AS u
        NATURAL JOIN authenticate.service_brdbnd_dial AS bb
        LEFT JOIN authenticate.service_xstop AS x ON (x.userID = u.userID AND x.statusID = 1)
        WHERE u.userID = newUser AND bb.statusID = 1
        GROUP BY u.userID
        HAVING (newSessionTimeout >= 0);
     ELSE
       SELECT NULL;
     END IF;
   END IF;
  END $
delimiter ;
</pre>
<p>You can see I made a few extra functions for finding the correct sessiontimeouts, rather than joining multi-databases into the query. Oh and another function for the &#8216;Class&#8217; Attribute for our filtering service. Then there&#8217;s the staticIP function.    So yeah, I reused code. Made code more portable for multi-locations.  And I love it!</p>
<p>Tonight, I&#8217;m going to implement this into the full radius config. Not just for BrianHead anymore.. I&#8217;m excited.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cbl.us/2005/12/15/stored-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>It&#8217;s alive!</title>
		<link>http://blog.cbl.us/2005/11/18/its-alive/</link>
		<comments>http://blog.cbl.us/2005/11/18/its-alive/#comments</comments>
		<pubDate>Fri, 18 Nov 2005 17:21:57 +0000</pubDate>
		<dc:creator>cbl</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.cbl.us/archives/77</guid>
		<description><![CDATA[5.0.17 snapshot is working just fine with no crashes or errors.. yet.
I dumped my databases from my 4.1.x server, scp&#8217;d them over to the new box, and restored. All looks good. Had I done this originally (tuesday night), I would have saved myself a bit of heartache and not had to deal with the InnoDB [...]]]></description>
			<content:encoded><![CDATA[<p>5.0.17 snapshot is working just fine with no crashes or errors.. yet.</p>
<p>I dumped my databases from my 4.1.x server, scp&#8217;d them over to the new box, and restored. All looks good. Had I done this originally (tuesday night), I would have saved myself a bit of heartache and not had to deal with the InnoDB problem nor the INSERT DELAYED. But, oh well, at least now it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cbl.us/2005/11/18/its-alive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Almost there.. (more on MySQL 5.0)</title>
		<link>http://blog.cbl.us/2005/11/17/almost-there-more-on-mysql-50/</link>
		<comments>http://blog.cbl.us/2005/11/17/almost-there-more-on-mysql-50/#comments</comments>
		<pubDate>Fri, 18 Nov 2005 01:01:37 +0000</pubDate>
		<dc:creator>cbl</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.cbl.us/archives/76</guid>
		<description><![CDATA[I think I have it figured out. It was a problem with the InnoDB from my 4.1.x installation when I copied it over to my 5.0.15 install.  I dumped my InnoDB data files, logs, and recreated them on startup (on the new box), then did a restore from a dump of the RT (ticketing [...]]]></description>
			<content:encoded><![CDATA[<p>I think I have it figured out. It was a problem with the InnoDB from my 4.1.x installation when I copied it over to my 5.0.15 install.  I dumped my InnoDB data files, logs, and recreated them on startup (on the new box), then did a restore from a dump of the RT (ticketing system) database.  This seemed to fix the problem.</p>
<p>Weird thing was, before I dumped my InnoDB data files, I tried dumping the database and restoring it from dump. No go. I couldn&#8217;t get it to insert the first row in the first table..  So I did the above.  Oh and then I tweaked some settings on my InnoDB part of my.cnf. Here&#8217;s some of them for future reference. Oh and I was able to recreate my InnoDB files correctly (i had forgot about the auto-extend file so one of my innodb data files before got up to 1102MB before I created a new one.. so now I have 3&#215;1024MBs and a 512MB that can autoextend to 1024MB max..  much cleaner)</p>
<pre>
innodb_log_files_in_group = 3
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_log_archive = 0
innodb_buffer_pool_size = 1G
innodb_additional_mem_pool_size = 32M
innodb_file_io_threads = 4
innodb_thread_concurrency = 16
innodb_lock_wait_timeout = 60
</pre>
<p>Anyway, tonight will be the test. Shall it work?  Shall everything work? I sure hope so.  I&#8217;m still running 5.0.17 (CVS snapshot from last night).. I think I&#8217;ll keep it around for the time being.</p>
<p>Stay tuned.. More later..</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cbl.us/2005/11/17/almost-there-more-on-mysql-50/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I lied..</title>
		<link>http://blog.cbl.us/2005/11/17/i-lied/</link>
		<comments>http://blog.cbl.us/2005/11/17/i-lied/#comments</comments>
		<pubDate>Thu, 17 Nov 2005 16:44:18 +0000</pubDate>
		<dc:creator>cbl</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.cbl.us/archives/75</guid>
		<description><![CDATA[More issues with MySQL 5.0.15 crashing&#8230; not the same as before.. but a different type of crashing. 
Time to try the daily snapshot and enable debugging.  
]]></description>
			<content:encoded><![CDATA[<p>More issues with MySQL 5.0.15 crashing&#8230; not the same as before.. but a different type of crashing. </p>
<p>Time to try the daily snapshot and enable debugging. <img src='http://blog.cbl.us/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cbl.us/2005/11/17/i-lied/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More on MySQL 5 bug</title>
		<link>http://blog.cbl.us/2005/11/16/more-on-mysql-5-bug/</link>
		<comments>http://blog.cbl.us/2005/11/16/more-on-mysql-5-bug/#comments</comments>
		<pubDate>Wed, 16 Nov 2005 20:18:44 +0000</pubDate>
		<dc:creator>cbl</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.cbl.us/archives/74</guid>
		<description><![CDATA[Found out the bug is reproducable on tables that I copied over from 4.1.x days.  It only seems to crash on &#8216;INSERT DELAYED&#8217; queries.  A quick look of the Bugs DB on MySQL&#8217;s site reveals this bug:
http://bugs.mysql.com/bug.php?id=13707 &#8211; Server crash with INSERT DELAYED on MyISAM table
I applied the patch and things work beautifully [...]]]></description>
			<content:encoded><![CDATA[<p>Found out the bug is reproducable on tables that I copied over from 4.1.x days.  It only seems to crash on &#8216;INSERT DELAYED&#8217; queries.  A quick look of the Bugs DB on MySQL&#8217;s site reveals this bug:</p>
<p><a href="http://bugs.mysql.com/bug.php?id=13707">http://bugs.mysql.com/bug.php?id=13707</a> &#8211; Server crash with INSERT DELAYED on MyISAM table</p>
<p>I applied the patch and things work beautifully now on my FreeBSD 6 box and MySQL5.0.15.</p>
<p>Now to apply this patch to my other MySQL 5.0.15 boxes&#8230; easier than waiting for 5.0.16 and hoping I dont get a crash.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cbl.us/2005/11/16/more-on-mysql-5-bug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More MySQL 5.0 woes</title>
		<link>http://blog.cbl.us/2005/11/16/more-mysql-50-woes/</link>
		<comments>http://blog.cbl.us/2005/11/16/more-mysql-50-woes/#comments</comments>
		<pubDate>Wed, 16 Nov 2005 17:43:50 +0000</pubDate>
		<dc:creator>cbl</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.cbl.us/archives/73</guid>
		<description><![CDATA[Last night (or was it this morning?), I had the same issues as the night preceeding.  MySQL 5.0.15 kept crashing over and over and over again.   However, this only happened when I allowed network connections in.  When I kept a &#8216;deny tcp from any to me 3306&#8242; in the firewall table, [...]]]></description>
			<content:encoded><![CDATA[<p>Last night (or was it this morning?), I had the same issues as the night preceeding.  MySQL 5.0.15 kept crashing over and over and over again.   However, this only happened when I allowed network connections in.  When I kept a &#8216;deny tcp from any to me 3306&#8242; in the firewall table, mysql worked just happy as can be.</p>
<p>Todays project.  Recompile with debug enabled, figure out what in the heck is causing it to crash on network connectivity to the daemon.</p>
<p>At least last night I knew enough not to screw up replication when I moved back to 4.1.15 on the old box.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cbl.us/2005/11/16/more-mysql-50-woes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL 5.0 upgrading woes</title>
		<link>http://blog.cbl.us/2005/11/15/mysql-50-upgrading-woes/</link>
		<comments>http://blog.cbl.us/2005/11/15/mysql-50-upgrading-woes/#comments</comments>
		<pubDate>Tue, 15 Nov 2005 21:38:19 +0000</pubDate>
		<dc:creator>cbl</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.cbl.us/archives/72</guid>
		<description><![CDATA[What a pain to get all of my MySQL 4.1.x installations up to MySQL version 5.0.15.  First I have to upgrade all my client libraries, then upgrade any dependencies on the 4.1.x libraries (popper, popproxy, php-mysql, pure-ftp, etc) to reference the new library.  Then I get to upgrade the MySQL server installations on [...]]]></description>
			<content:encoded><![CDATA[<p>What a pain to get all of my MySQL 4.1.x installations up to MySQL version 5.0.15.  First I have to upgrade all my client libraries, then upgrade any dependencies on the 4.1.x libraries (popper, popproxy, php-mysql, pure-ftp, etc) to reference the new library.  Then I get to upgrade the MySQL server installations on my mail servers and other places to be 5.0.15 so they can slave from my &#8216;main&#8217; master correctly.</p>
<p>Needless to say, yesterday, was a day full of upgrading MySQL. I got all of my slaves and clients done by 11pm. Then I started the move to the new MySQL 5.0.15 box running FreeBSD 6. Unfortunately, after all 30GB were copied (45 sec per gig, not bad), my new 5.0.15 process kept signal 11ing and restarting itself.  After all that, my replication was screwed up when I moved back to a 4.1.11 master on the original server.  Anyway, it was a pain to get slaves back up. I had to dump the data and copy to the slaves and load manually &#8216;load data from master&#8217; didnt work right on 5.0.15 slave talking to a 4.1.x master.</p>
<p>However, I think I got my settings on my new &#8216;master&#8217; correctly set.  I missed some kernel stack size variables set in /boot/loader.conf</p>
<p>kern.maxdsiz=2147483648<br />
kern.maxssiz=268435456</p>
<p>I tried setting maxdsiz to 3GB..but my machine crashed on boot. So i&#8217;ll keep it at 2gb for now.</p>
<p>Now I&#8217;m able to successfully run my InnoDB tests on my new master.. no crashing.. all seems to be good.  Here&#8217;s hoping that tonight wont bring the same bad luck as last night.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cbl.us/2005/11/15/mysql-50-upgrading-woes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

