<?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>dan.thoughts &#187; MySQL</title>
	<atom:link href="http://blog.sosedoff.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sosedoff.com</link>
	<description>Web-development, PHP, Ruby, Sinatra, Merb, Rails, MySQL, SQLite, Web Services.</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:54:45 +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>Capistrano MySQL Backups for Rails</title>
		<link>http://blog.sosedoff.com/2011/08/10/capistrano-mysql-backups-for-rails/</link>
		<comments>http://blog.sosedoff.com/2011/08/10/capistrano-mysql-backups-for-rails/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 00:34:50 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[backups]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=395</guid>
		<description><![CDATA[If you need to backup your application before each deployment, here is the small manual.
Capistrano versions: >= 2.5
Configuration
First, you need to define application environment:

# Define server-side rails environment
set :rails_env, &#34;production&#34;
&#160;
# Primary deployment location
set :deploy_to, &#34;/home/#{user}/#{application}&#34;
&#160;
# Place where all backups will be dumped
set :backup_to, &#34;/home/#{user}/#{application}/backups&#34;

Also, add this function. It allows capistrano to check if remote file [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to backup your application before each deployment, here is the small manual.</p>
<p>Capistrano versions: >= 2.5</p>
<h2>Configuration</h2>
<p>First, you need to define application environment:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Define server-side rails environment</span>
set <span style="color:#ff3333; font-weight:bold;">:rails_env</span>, <span style="color:#996600;">&quot;production&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Primary deployment location</span>
set <span style="color:#ff3333; font-weight:bold;">:deploy_to</span>, <span style="color:#996600;">&quot;/home/#{user}/#{application}&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Place where all backups will be dumped</span>
set <span style="color:#ff3333; font-weight:bold;">:backup_to</span>, <span style="color:#996600;">&quot;/home/#{user}/#{application}/backups&quot;</span></pre></div></div>

<p>Also, add this function. It allows capistrano to check if remote file exists:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> remote_file_exists?<span style="color:#006600; font-weight:bold;">&#40;</span>full_path<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#996600;">'true'</span> ==  capture<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;if [ -e #{full_path} ]; then echo 'true'; fi&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">strip</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, we need to add a backup task:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">namespace <span style="color:#ff3333; font-weight:bold;">:utils</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#996600;">'Backup database before deploy'</span>
  task <span style="color:#ff3333; font-weight:bold;">:backup</span>, <span style="color:#ff3333; font-weight:bold;">:roles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:db</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:primary <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run <span style="color:#996600;">&quot;mkdir -p #{backup_to}&quot;</span> <span style="color:#008000; font-style:italic;"># Create a backup folder unless exists</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Primary backup filename</span>
    filename = <span style="color:#996600;">&quot;#{backup_to}/#{application}_predeploy_#{Time.now.strftime(&quot;</span><span style="color:#006600; font-weight:bold;">%</span>m<span style="color:#006600; font-weight:bold;">%</span>d<span style="color:#006600; font-weight:bold;">%</span>Y<span style="color:#006600; font-weight:bold;">%</span>H<span style="color:#006600; font-weight:bold;">%</span>I<span style="color:#006600; font-weight:bold;">%</span>S<span style="color:#996600;">&quot;)}.sql.gz&quot;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Check if we've got database config</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> remote_file_exists?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{deploy_to}/current/config/database.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>    
      text = capture<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;cat #{deploy_to}/current/config/database.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      config = <span style="color:#CC00FF; font-weight:bold;">YAML</span>::<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span>rails_env<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
      on_rollback <span style="color:#006600; font-weight:bold;">&#123;</span> run <span style="color:#996600;">&quot;rm #{filename}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      run <span style="color:#996600;">&quot;mysqldump -u #{config['username']} -p #{config['database']} | gzip --best &gt; #{filename}&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ch, stream, out<span style="color:#006600; font-weight:bold;">|</span>
        ch.<span style="color:#9900CC;">send_data</span> <span style="color:#996600;">&quot;#{config['password']}<span style="color:#000099;">\n</span>&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> out =~ <span style="color:#006600; font-weight:bold;">/</span>^Enter password:<span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      logger.<span style="color:#9900CC;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;[Backup] No configuration file was found.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And finally, add the capistrano before hook:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">before <span style="color:#996600;">&quot;deploy&quot;</span>, <span style="color:#996600;">&quot;utils:backup&quot;</span></pre></div></div>

<h2>Testing</h2>
<p>To test if the task works, run:</p>
<pre>cap utils:backup</pre>
<p>On the server side you should see a backup file:</p>
<pre>/home/USER/APP/backups/APP_predeploy_MMDDYYHHMMSS.sql.gz.</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2011/08/10/capistrano-mysql-backups-for-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom field aggregations in Sphinx using SphinxQL</title>
		<link>http://blog.sosedoff.com/2010/09/06/custom-field-aggregations-in-sphinx-using-sphinxql/</link>
		<comments>http://blog.sosedoff.com/2010/09/06/custom-field-aggregations-in-sphinx-using-sphinxql/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 01:52:23 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[aggregations]]></category>
		<category><![CDATA[indexing]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query language]]></category>
		<category><![CDATA[sphinx]]></category>
		<category><![CDATA[sphinx ql]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=270</guid>
		<description><![CDATA[Sphinx is a really powerful tool for a full-text database search. It is the perfect option as a search engine on your website&#8217;s data.
In default mode it works as a regular tcp server and has multiple native language bindings for php, ruby, c, etc. But its another outstanding feature is MySQL Protocol Connectoin and SphinxQL, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.sphinxsearch.com/">Sphinx</a> is a really powerful tool for a full-text database search. It is the perfect option as a search engine on your website&#8217;s data.<br />
In default mode it works as a regular tcp server and has multiple native language bindings for php, ruby, c, etc. But its another outstanding feature is MySQL Protocol Connectoin and SphinxQL, which is similar to native mysql query language. </p>
<p>So, ok. Lets say we have N documents with M attributes. Attributes could be different: string, integer, double, boolean. Out objective is to perform attribute aggregation based on specified search term (user-defined, etc). That will give us full information on data selected only by search term. Its only use-case when you really need to get these aggregate fields.  Next part is tricky and not really efficient. </p>
<p>First of all, you have to setup Sphinx search daemon instance using different configuration file (it could not run both). Another problem &#8211; you have to setup another data sources and index files, Sphinx puts a lock on all used-right-now files. </p>
<p>Lets assume we have a database of books. We need to build a form with sliders which could be used as user-friendly search filter. All we need is to get a list of min and max attributes values. But there is a problem: sometimes, while working with sphinx you might find yourself trying to use it like you usually do with regular RDMS. Unfortunately, sphinx has a different design. Basically, sphinx has one primary field which presents in each search request &#8211; DocumentID. Its an unique id that represents your data ID, which makes it harder to product aggregate data. And there is no way to get rid of that field.<br />
The whole idea of our aggregation &#8211; using boolean match mode with no weighting performed at all. In that case all results will have weight field = 1. That will give us ability to group all the results by weight field, rejecting the DocumentID field. </p>
<p>Here is the sample query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
  MIN<span style="color: #66cc66;">&#40;</span>reviews<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> min_reviews<span style="color: #66cc66;">,</span> MAX<span style="color: #66cc66;">&#40;</span>reviews<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> max_reviews<span style="color: #66cc66;">,</span>
  MIN<span style="color: #66cc66;">&#40;</span>pages<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> min_pages<span style="color: #66cc66;">,</span> MAX<span style="color: #66cc66;">&#40;</span>pages<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> max_pages<span style="color: #66cc66;">,</span>
  MIN<span style="color: #66cc66;">&#40;</span>pub_year<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> min_date<span style="color: #66cc66;">,</span> MAX<span style="color: #66cc66;">&#40;</span>pub_year<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> max_date<span style="color: #66cc66;">,</span>
  @weight <span style="color: #993333; font-weight: bold;">AS</span> w
<span style="color: #993333; font-weight: bold;">FROM</span> 
  INDEX_NAME
<span style="color: #993333; font-weight: bold;">WHERE</span>
  MATCH<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SEARCH_TERM'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span> pages <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">30</span>
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> w <span style="color: #993333; font-weight: bold;">OPTION</span> ranker <span style="color: #66cc66;">=</span> none</pre></div></div>

<p>The result of this query will be one row with field alias names. Thats&#8217;s it. </p>
<p>All statements are fully customizable. Just check <a href="http://www.sphinxsearch.com/docs/current.html#sphinxql-reference">full SphinxQL reference</a> for details.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/09/06/custom-field-aggregations-in-sphinx-using-sphinxql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disable auto-incremental field in Rails Migrations</title>
		<link>http://blog.sosedoff.com/2010/08/12/disable-auto-incremental-field-in-rails-migrations/</link>
		<comments>http://blog.sosedoff.com/2010/08/12/disable-auto-incremental-field-in-rails-migrations/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 03:16:05 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=262</guid>
		<description><![CDATA[Since i`ve been using both DataMapper (Merb/Sinatra) and ActiveRecord (Rails) a lot i noticed that AR acts weight when i manually set PK key, particularly ID field, which you dont have to define by default. In DM you have to define it as &#8216;Serial&#8217;. 
So, the task is to create/update records in your database which [...]]]></description>
			<content:encoded><![CDATA[<p>Since i`ve been using both DataMapper (Merb/Sinatra) and ActiveRecord (Rails) a lot i noticed that AR acts weight when i manually set PK key, particularly ID field, which you dont have to define by default. In DM you have to define it as &#8216;Serial&#8217;. </p>
<p>So, the task is to create/update records in your database which is supposes to represent data from primary database. In such cases all ID`s should be unique and equal to each other. </p>
<p>To disable autoincremental ID field in your AR models just use this option:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">create_table <span style="color:#ff3333; font-weight:bold;">:foo</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
  t.<span style="color:#CC0066; font-weight:bold;">integer</span>   <span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:options</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'PRIMARY KEY'</span>
  <span style="color:#008000; font-style:italic;"># .... rest of columns</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/08/12/disable-auto-incremental-field-in-rails-migrations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date separated MySQL backups</title>
		<link>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/</link>
		<comments>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 14:04:59 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=163</guid>
		<description><![CDATA[Here is the bash shell script that makes archived dumps of your database server. All databases are separated from each other and stored into date based folders. 

#!/bin/bash

MyUSER="root"
MyPASS=""
MyHOST="localhost"
NOW="$(date +"%d-%m-%Y")"
STOREDIR="/home/storage/backup/database/by_dates/$NOW"
DBLIST="$(mysql -u $MyUSER -h $MyHOST -Bse 'show databases')"

[ ! -d $STOREDIR ] &#038;&#038; mkdir -p $STOREDIR &#124;&#124; :

for db in $DBLIST
do
	FILE="$STOREDIR/$db.gz"
	mysqldump -u $MyUSER -h $MyHOST $db &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the bash shell script that makes archived dumps of your database server. All databases are separated from each other and stored into date based folders. </p>
<pre>
#!/bin/bash

MyUSER="root"
MyPASS=""
MyHOST="localhost"
NOW="$(date +"%d-%m-%Y")"
STOREDIR="/home/storage/backup/database/by_dates/$NOW"
DBLIST="$(mysql -u $MyUSER -h $MyHOST -Bse 'show databases')"

[ ! -d $STOREDIR ] &#038;&#038; mkdir -p $STOREDIR || :

for db in $DBLIST
do
	FILE="$STOREDIR/$db.gz"
	mysqldump -u $MyUSER -h $MyHOST $db | gzip -9 > $FILE
done
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useless filesystem over MySQL</title>
		<link>http://blog.sosedoff.com/2009/04/06/useless-filesystem-over-mysql/</link>
		<comments>http://blog.sosedoff.com/2009/04/06/useless-filesystem-over-mysql/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 04:10:28 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[fuse]]></category>
		<category><![CDATA[mysqlfuse]]></category>
		<category><![CDATA[over]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=121</guid>
		<description><![CDATA[Here is a completely useless filesystem based on MySQL database storage &#8211; mysqlfuse, implemented with Fuse.
I didnt find any way how i can use it, but meanwhile, this fs working. Not perfect of course, in that case its not maintained for a long time. Doesnt support information about free drive space, so any filemanager keeps [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a completely useless filesystem based on MySQL database storage &#8211; mysqlfuse, implemented with Fuse.<br />
I didnt find any way how i can use it, but meanwhile, this fs working. Not perfect of course, in that case its not maintained for a long time. Doesnt support information about free drive space, so any filemanager keeps saying &#8216;Error: No space left on device&#8217;. Such case making it more useless. </p>
<p>It`s really easy to set it up. </p>
<p>First, we need to install developer headers for fuse:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libfuse-dev</pre></div></div>

<p>Next, getting sources (32bit only, not working in 64bit):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>voxel.dl.sourceforge.net<span style="color: #000000; font-weight: bold;">/</span>sourceforge<span style="color: #000000; font-weight: bold;">/</span>mysqlfs<span style="color: #000000; font-weight: bold;">/</span>mysqlfs-0.4.0-rc1.tar.bz2</pre></div></div>

<p>Unpack it, and compile:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xjvf</span> mysqlfs-0.4.0-rc1.tar.bz2
$ <span style="color: #7a0874; font-weight: bold;">cd</span> mysqlfs-0.4.0-rc1
$ .<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Next, we need to setup the database</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> mysqlfs<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">SELECT</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">INSERT</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">UPDATE</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">DELETE</span> <span style="color: #990099; font-weight: bold;">ON</span> mysqlfs.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> mysqlfs@<span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">%</span>&quot;</span> IDENTIFIED BY <span style="color: #008000;">'password'</span><span style="color: #000033;">;</span>
FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span></pre></div></div>

<p>And create database schema. SQL file located in root folder of the sources</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span> mysqlfs <span style="color: #000000; font-weight: bold;">&lt;</span> schema.sql</pre></div></div>

<p>And finally, mount filesystem to some folder:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqlfs <span style="color: #660033;">-ohost</span>=MYSQLHOST <span style="color: #660033;">-ouser</span>=MYSQLUSER <span style="color: #660033;">-opassword</span>=MYSQLPASS <span style="color: #660033;">-odatabase</span>=mysqlfs MOUNT_DIR</pre></div></div>

<p>Now, its gonna be working. To use automatic configuration parameters you can create section [mysqlfs] in your mysql configuration file (my.cnf)</p>
<p>Parameters:</p>
<pre>
-ohost=<hostname>
    MySQL server host

  -ouser=<username>
    MySQL username

  -opassword=
<password>
    MySQL password

  -odatabase=<db>
    MySQL database name
</pre>
<p>That`s it. Anyway, using FUSE there is a way to create so weird filesystems proxy. For example, there is SQLite over FUSE. And it is too old. Next time i`ll write about Amazon S3 over FUSE projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/04/06/useless-filesystem-over-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Updating order fields in table with MySQL</title>
		<link>http://blog.sosedoff.com/2009/02/05/updating-order-fields-in-table-with-mysql/</link>
		<comments>http://blog.sosedoff.com/2009/02/05/updating-order-fields-in-table-with-mysql/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 04:00:54 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[auto]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[order]]></category>
		<category><![CDATA[transaction]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=50</guid>
		<description><![CDATA[This is a simple piece of code which describing how to update special order field in table. For example, you have table &#8216;photos&#8217;:

+ --------------------------------- +
&#124; ID    &#124; UserID &#124; OrderID &#124; ImgURL &#124;
+ --------------------------------- +
&#124; 1132  &#124; 1      &#124; 0       [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple piece of code which describing how to update special order field in table. For example, you have table &#8216;photos&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">+ --------------------------------- +
| ID    | UserID | OrderID | ImgURL |
+ --------------------------------- +
| 1132  | 1      | 0       | ...    |
| 2124  | 1      | 0       | ...    |
| 3456  | 1      | 0       | ...    |
.....................................
| N     | 1      | 0       | ...    |
+ --------------------------------- +</pre></div></div>

<p>OrderID field value by default is 0. If some images has been deleted order lose its consistency.<br />
To repair order we can use single transaction:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN;
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @<span style="color: #993333; font-weight: bold;">ORDER</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> photos
<span style="color: #993333; font-weight: bold;">SET</span> order_id <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">ORDER</span> :<span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> user_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
COMMIT;</pre></div></div>

<p>After executing this on table &#8216;images&#8217; we`ll get such result:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">+ --------------------------------- +
| ID    | UserID | OrderID | ImgURL |
+ --------------------------------- +
| 1132  | 1      | 1       | ...    |
| 2124  | 1      | 2       | ...    |
| 3456  | 1      | 3       | ...    |
.....................................
| N     | 1      | M       | ...    |
+ --------------------------------- +</pre></div></div>

<p>Some developers instead of using resources of mysql script language trying to solve this problem by executing numerous call, such &#8216;UPDATE table SET order_id = [value] WHERE id = [id] LIMIT 1;&#8217;. It`s wrong. MySQL language have a lot of ways to make it easy. At least UDF`s.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/02/05/updating-order-fields-in-table-with-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple MySQL backup script</title>
		<link>http://blog.sosedoff.com/2009/01/15/simple-mysql-backup-script/</link>
		<comments>http://blog.sosedoff.com/2009/01/15/simple-mysql-backup-script/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 13:02:33 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=27</guid>
		<description><![CDATA[There is a small useful ruby script to backup your MySQL databases in small projects, where speed of backup not so important.
Source:

#!/usr/bin/ruby
# MySQL Backup Utility
# Usage: ./mysql_backup.rb or ruby mysql_backup.rb
&#160;
$backup_archive = true # gzip files after processing
$backup_dir = &#34;/home/storage/backup/&#34; # output directory
$backup_template = &#34;project-%s-%s.sql&#34; # text-%dbname-%timestamp.sql
$backup_cmd = &#34;mysqldump -u local_backup --add-drop-table --databases %s &#38;gt; %s&#34;
$backup_dblist [...]]]></description>
			<content:encoded><![CDATA[<p>There is a small useful ruby script to backup your MySQL databases in small projects, where speed of backup not so important.<br />
Source:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/ruby</span>
<span style="color: #666666; font-style: italic;"># MySQL Backup Utility</span>
<span style="color: #666666; font-style: italic;"># Usage: ./mysql_backup.rb or ruby mysql_backup.rb</span>
&nbsp;
<span style="color: #007800;">$backup_archive</span> = <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #666666; font-style: italic;"># gzip files after processing</span>
<span style="color: #007800;">$backup_dir</span> = <span style="color: #ff0000;">&quot;/home/storage/backup/&quot;</span> <span style="color: #666666; font-style: italic;"># output directory</span>
<span style="color: #007800;">$backup_template</span> = <span style="color: #ff0000;">&quot;project-%s-%s.sql&quot;</span> <span style="color: #666666; font-style: italic;"># text-%dbname-%timestamp.sql</span>
<span style="color: #007800;">$backup_cmd</span> = <span style="color: #ff0000;">&quot;mysqldump -u local_backup --add-drop-table --databases %s &amp;gt; %s&quot;</span>
<span style="color: #007800;">$backup_dblist</span> = <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #666666; font-style: italic;"># list of databases to backup</span>
  <span style="color: #ff0000;">'main'</span>,
  <span style="color: #ff0000;">'users'</span>,
  <span style="color: #ff0000;">'admin'</span>,
  <span style="color: #ff0000;">'cards'</span>
<span style="color: #7a0874; font-weight: bold;">&#93;</span> 
&nbsp;
def backup_database<span style="color: #7a0874; font-weight: bold;">&#40;</span>database<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">time</span> = Time.now<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  time_str = sprintf<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;%02i-%02i-%04i-%02i%02i%02i&quot;</span>,time.day, time.month, time.year, time.hour, time.min, time.sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  filename = sprintf<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$backup_template</span>,database,time_str<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  filename = <span style="color: #ff0000;">&quot;#{<span style="color: #007800;">$backup_dir</span>}#{filename}&quot;</span>
&nbsp;
  cmd = sprintf<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$backup_cmd</span>,database,filename<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">if</span> system<span style="color: #7a0874; font-weight: bold;">&#40;</span>cmd<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span>
    system<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;gzip --best #{filename}&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #007800;">$backup_archive</span>
  end
end
&nbsp;
<span style="color: #007800;">$backup_dblist</span>.each <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold;">|</span>db<span style="color: #000000; font-weight: bold;">|</span>
  puts <span style="color: #ff0000;">&quot;Processing database... #{db}&quot;</span>
  backup_database<span style="color: #7a0874; font-weight: bold;">&#40;</span>db<span style="color: #7a0874; font-weight: bold;">&#41;</span>
end</pre></div></div>

<p>Paste: <a href="http://pastie.org/341839">http://pastie.org/341839</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/01/15/simple-mysql-backup-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

