<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Tag mysql</title>
    <link>http://www.robbyonrails.com/articles/tag/mysql</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Master/Slave Databases with Ruby on Rails</title>
      <description>&lt;p&gt;Not terribly long ago, I &lt;a href="http://www.robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails"&gt;announced Active Delegate&lt;/a&gt;, which was a really lightweight plugin that I developed to allow models to talk to multiple databases for specific methods. The plugin worked great for really simple situations, like individual models.. but when it came time to test with associations it fell apart. I haven&amp;#8217;t had a chance to work on any updates and knew that it was going to take more work to get it going.&lt;/p&gt;


	&lt;p&gt;Earlier this week, we helped one of our bigger clients launch their new web site&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;. For the deployment, we needed to send all writes to a master database and a reads to slaves (initial deployment is talking to almost 10 slaves spread around the globe!). We needed something to get integrated quickly and decided to ditch Active Delegate for the time being and began looking at the following options.&lt;/p&gt;


	&lt;p&gt;I spoke with Rick Olson&lt;sup&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt; and he pointed me to a new plugin that he hasn&amp;#8217;t really released yet. So, I&amp;#8217;m going to do him a favor and announce it for him. Of course&amp;#8230; I got his permission first&amp;#8230; ;-)&lt;/p&gt;


	&lt;h2&gt;Announcing Masochism!&lt;/h2&gt;


	&lt;p&gt;Masochism&lt;sup&gt;&lt;a href="#fn3"&gt;3&lt;/a&gt;&lt;/sup&gt; is a new plugin for Ruby on Rails that allows you to delegate all writes to a master database and reads to a slave database. The configuration process is just a few lines in your environment file and the plugin takes care of the rest.&lt;/p&gt;


	&lt;h3&gt;Installing Masochism&lt;/h3&gt;


	&lt;p&gt;With &lt;a href="http://piston.rubyforge.org/usage.html"&gt;piston&lt;/a&gt;, you can import Masochism with:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  $ cd vendor/plugins
  $ piston import http://ar-code.svn.engineyard.com/plugins/masochism/
&lt;/code&gt;&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;To learn more about piston, read &lt;a href="http://www.robbyonrails.com/articles/2007/01/16/every-second-counts-with-a-piston-in-your-trunk"&gt;Every Second Counts with a Piston in your trunk&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;You can also install it with the old-fashioned way:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  $ ./script/plugin install -x http://ar-code.svn.engineyard.com/plugins/masochism/
&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;Configuring Masochism&lt;/h3&gt;


	&lt;p&gt;The first thing that you&amp;#8217;ll need to do is add another database connection in &lt;code&gt;config/database.yml&lt;/code&gt; for &lt;code&gt;master_database&lt;/code&gt;. By default, Masochism expects you to have a production database, which will be the read-only/slave database. The &lt;code&gt;master_database&lt;/code&gt; will be the connection details for your (you guessed it&amp;#8230;) master database.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# config/database.yml  
production:
  database: masochism_slave_database
  adapter: postgresql
  host: slavedb1.hostname.tld
  ...

master_database:
  database: masochism_master_database
  adapter: postgresql
  host: masterdb.hostname.tld
  ...
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The idea here is that replication will be handled elsewhere and your application can reap the benefits of talking to the slave database for all of it&amp;#8217;s read-only operations and let the master database(s) spend their time writing data.&lt;/p&gt;


	&lt;p&gt;The next step is to set this up in your environment file. In our scenario, this was &lt;code&gt;config/environments/production.rb&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# Add this to config/environments/production.rb
config.after_initialize do 
  ActiveReload::ConnectionProxy.setup!    
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Voila, you should be good to go now. As I mentioned, we&amp;#8217;ve only been using this for this past week and we&amp;#8217;ve had to address a few problems that the initial version of the plugin didn&amp;#8217;t address. One of our developers, &lt;a href="http://andy.delcambre.com/"&gt;Andy Delcambre&lt;/a&gt;, just posted an article to show how we had a problem with &lt;a href="http://andy.delcambre.com/2007/11/15/masochistic-connection-proxy-with-observers"&gt;using ActiveRecord observers and masochism&lt;/a&gt;, which we&amp;#8217;re sending over a patch for now.&lt;/p&gt;


	&lt;p&gt;As we continue to monitor how this solution works, we&amp;#8217;ll report any findings on our blog. In the meantime, I&amp;#8217;d be interested in knowing what you&amp;#8217;re using to solve this problem. :-)&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; &lt;a href="http://contiki.com"&gt;Contiki&lt;/a&gt;, a cool travel company we&amp;#8217;re working with&lt;/p&gt;


	&lt;p id="fn2"&gt;&lt;sup&gt;2&lt;/sup&gt; Rick just moved to Portland&amp;#8230; welcome to stump town!&lt;/p&gt;


	&lt;p id="fn3"&gt;&lt;sup&gt;3&lt;/sup&gt; &lt;a href="http://ar-code.svn.engineyard.com/plugins/masochism/README"&gt;The Masochism plugin &lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
</description>
      <pubDate>Thu, 15 Nov 2007 16:02:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:3420b2e3-a80c-43c9-a136-a58040069607</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/11/15/master-slave-databases-with-ruby-on-rails</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PLANET ARGON</category>
      <category>plugins</category>
      <category>masochism</category>
      <category>database</category>
      <category>mysql</category>
      <category>postgresql</category>
      <category>client</category>
      <category>development</category>
      <category>activerecord</category>
      <category>replication</category>
      <category>deployment</category>
    </item>
    <item>
      <title>Starting MySQL after upgrading to OS X Leopard</title>
      <description>&lt;p&gt;If you upgraded to &lt;span class="caps"&gt;OS X&lt;/span&gt; Leopard and are running &lt;a href="http://mysql.com"&gt;MySQL&lt;/a&gt; from the MySQL.com installer&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;, you might be having some problems with starting it from the &lt;span class="caps"&gt;GUI&lt;/span&gt; interface. There isn&amp;#8217;t a fix from MySQL yet, so to get around that&amp;#8230; you can run it from the command-line.&lt;/p&gt;


	&lt;h2&gt;Start MySQL from the command line&lt;/h2&gt;


	&lt;p&gt;&lt;code&gt;cd /usr/local/mysql; ./bin/mysqld_safe &amp;#38;&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;This should get MySQL up and running for you. If someone wants to share a tip on how to get this to start automatically on reboot, please post a comment and I&amp;#8217;ll help get the word out.&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; I didn&amp;#8217;t have this problem as I &lt;a href="http://www.robbyonrails.com/articles/2007/06/19/installing-ruby-on-rails-and-postgresql-on-os-x-second-edition"&gt;installed MySQL via MacPorts&lt;/a&gt;... but this came up for a few members of &lt;a href="http://planetargon.com"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt; after they upgraded to Leopard.&lt;/p&gt;
</description>
      <pubDate>Sat, 27 Oct 2007 06:13:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1fbb7a95-3045-4e65-a416-c33efbf8bbd1</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/10/27/starting-mysql-after-upgrading-to-os-x-leopard</link>
      <category>apple</category>
      <category>leopard</category>
      <category>osx</category>
      <category>mysql</category>
      <category>tip</category>
    </item>
    <item>
      <title>One of those mornings</title>
      <description>&lt;p&gt;Well, I made it about eight months since I got &lt;a href="http://www.robbyonrails.com/articles/2006/08/11/isight-magnet-is-teh-suck"&gt;my new hard drive&lt;/a&gt;... and today&amp;#8230; I had to run&amp;#8230;&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;sudo port -v install mysql5 +server&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;Why? I have to migrate some data from &lt;a href="http://www.robbyonrails.com/articles/2005/10/29/migrating-from-mysql-to-postgresql-in-60-seconds-with-rails"&gt;MySQL to PostgreSQL&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Tue, 17 Apr 2007 11:09:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e5dcf0b0-81de-4353-b859-85485a2bf542</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/04/17/one-of-those-mornings</link>
      <category>Ruby on Rails</category>
      <category>Programming</category>
      <category>PostgreSQL</category>
      <category>migration</category>
      <category>mysql</category>
      <category>morning</category>
    </item>
    <item>
      <title>Just the beginning...</title>
      <description>&lt;p&gt;This week&amp;#8230; &lt;a href="http://www.planetargon.com"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt; will be making a series of announcements. The last time that we did this we found ourselves with a few new employees. What will be happening this week?&lt;/p&gt;


	&lt;p&gt;...the rumors in &lt;code&gt;#caboose&lt;/code&gt;... are likely to be &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;In other news&amp;#8230; Jeremy &lt;a href="http://www.jvoorhis.com/articles/2006/02/13/just-a-friendly-reminder"&gt;had to install MySQL on his PowerBook last week.&lt;/a&gt;..&lt;/p&gt;


	&lt;p&gt;...the things you do for clients. ;-)&lt;/p&gt;


	&lt;p&gt;Have a great week!&lt;/p&gt;
</description>
      <pubDate>Mon, 13 Feb 2006 16:21:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:de730afd-4e80-4548-97c0-c301c5d1c4d0</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/02/13/just-the-beginning</link>
      <category>rails</category>
      <category>mysql</category>
      <category>planetargon</category>
    </item>
    <item>
      <title>(more) Migrating from MySQL to PostgreSQL</title>
      <description>&lt;p&gt;&lt;a href="http://people.planetpostgresql.org/chriskl/"&gt;Christopher Kings-Lynne&lt;/a&gt; is getting &lt;a href="http://people.planetpostgresql.org/chriskl/index.php?/archives/50-MySQL-Compatibility-Project-First-Beta-Release.html"&gt;ready to release&lt;/a&gt; 1.0 of his cool project on PgFoundry called, &lt;a href="http://pgfoundry.org/projects/mysqlcompat"&gt;MySQL Compatibility Functions&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;What does this provide?&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;&amp;#8220;A reimplemenation of as many MySQL functions as possible in PostgreSQL, as an aid to porting.&amp;#8221;&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;Basically&amp;#8230; a bunch of pg/sql functions&amp;#8230; to account for all those mysql functions that you all love so much. :-)&lt;/p&gt;


	&lt;p&gt;For those who feel like they are stuck with MySQL&amp;#8230; &lt;a href="http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/mysqlcompat/mysqlcompat/"&gt;check this out&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;...it&amp;#8217;s not too late!&lt;/p&gt;


	&lt;p&gt;Next Week&amp;#8230; I&amp;#8217;ll discuss my progress with &lt;a href="http://postgis.refractions.net/"&gt;PostGIS&lt;/a&gt; and &lt;a href="http://www.rubyonrails.org"&gt;Ruby on Rails&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Thu, 01 Dec 2005 20:59:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:1eb6821aa2b89e9113d0e636553afbdf</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/12/01/more-migrating-from-mysql-to-postgresql</link>
      <category>Programming</category>
      <category>PostgreSQL</category>
      <category>postgesql</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Interviewed by CRN regarding Oracle, MySQL, and PostgreSQL</title>
      <description>&lt;p&gt;I was interviewed by &lt;span class="caps"&gt;CRN&lt;/span&gt; regarding my personal thoughts on how the release of Oracle Express might compete with MySQL and &lt;a href="http://www.postgresql.org"&gt;PostgreSQL&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.crn.com/sections/software/software.jhtml?articleId=173403216"&gt;Read the short article&amp;#8230;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Oracle Express&amp;#8230; &lt;strong&gt;{yawn}&lt;/strong&gt;&lt;/p&gt;
</description>
      <pubDate>Sat, 12 Nov 2005 21:49:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:ecb4ed773f2bb0c870d62a4c0178dd1a</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/11/12/interviewed-by-crn-regarding-oracle-mysql-and-postgresql</link>
      <category>Off-Topic</category>
      <category>PostgreSQL</category>
      <category>postgresql</category>
      <category>oracle</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Migrating from MySQL to PostgreSQL in 60 seconds (with Rails)</title>
      <description>&lt;p&gt;I do quite a few MySQL to PostgreSQL migrations for clients of &lt;a href="http://www.planetargon.com"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt; and over the years I have gone through different methods of handling this procedure. Typically, it involved exporting data, re-importing it through some shell script that does its best to take into consideration the slight differences between the two databases.&lt;/p&gt;


	&lt;p&gt;While working on a recent project that was to be migrated from &lt;a href="http://sql-info.de/mysql/gotchas.html"&gt;MySQL&lt;/a&gt; to &lt;a href="http://www.postgresql.org"&gt;PostgreSQL&lt;/a&gt;, but after some &lt;a href="http://www.refactoringrails.com"&gt;Refactoring&lt;/a&gt; had been done to the code base. I had been meaning to spend a few hours coming up with some simplified process of taking data from MySQL and replicating it to a new PostgreSQL database, while maintaining  referential integrity.&lt;/p&gt;


	&lt;p&gt;My original idea was to build a script that referenced two seperate databases and then copied data out and inserted it into the new db after performing a few data changes. This seemed like too much work and I knew that I should be able to harness the power of Ruby and Rails in my process. My next thought? &lt;a href="http://redhanded.hobix.com/inspect/railsfsAfterACoupleMinutesOfToolingWithFuseWhoa.html"&gt;RailsFS&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;In a &lt;strong&gt;perfect&lt;/strong&gt; world, I would mount one instance of the application with a mysql database and another with a postgresql database&amp;#8230; and just copy the yaml files to the postgresql and bam, it&amp;#8217;d work, right? Well, it doesn&amp;#8217;t quite work that way. So, I ruled out that idea.&lt;/p&gt;


	&lt;p&gt;Then I had another idea. I would build a task in my Rakefile that&amp;#8230; imported each of those &lt;span class="caps"&gt;YAML&lt;/span&gt; files into PostgreSQL!&lt;/p&gt;


	&lt;p&gt;We can now type in the following commands:&lt;/p&gt;


This first creates my PostgreSQL database tables using Migrations.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;rake&lt;/span&gt; &lt;span class="ident"&gt;migrate&lt;/span&gt; &lt;span class="constant"&gt;VERSION&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;then&amp;#8230;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;rake&lt;/span&gt; &lt;span class="ident"&gt;mysql2pgsql&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;code&gt;mysql2pgsql&lt;/code&gt; runs&amp;#8230;and approx 60 seconds later, I had an exact copy of the MySQL database in PostgreSQL. 

But wait! PostgreSQL has &lt;span class="caps"&gt;SEQUENCES&lt;/span&gt;&amp;#8230; so I then run:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;rake&lt;/span&gt; &lt;span class="ident"&gt;migrate&lt;/span&gt; &lt;span class="constant"&gt;VERSION&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;...and my &lt;span class="caps"&gt;SEQUENCES&lt;/span&gt; are &lt;span class="caps"&gt;ALTER&lt;/span&gt;&amp;#8217;d.&lt;/p&gt;


	&lt;p&gt;Yes, I will post some code in the near future. But all I am doing is basically running through all the &lt;span class="caps"&gt;YAML&lt;/span&gt; files that are available from RailsFS and adding them into the new database. It automatically figures out the model name&amp;#8230;and is fairly generic at the moment. However, I&amp;#8217;d like to spend a bit more time stress-testing it before I post it. :-)&lt;/p&gt;


	&lt;p&gt;I want to try to work on a RailsFS-less version as well so that I can run this on my PowerBook.&lt;/p&gt;


	&lt;p&gt;Until next time&amp;#8230; have fun!&lt;/p&gt;
</description>
      <pubDate>Sat, 29 Oct 2005 14:23:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b1064070f97315072ad3be65a29dc575</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/10/29/migrating-from-mysql-to-postgresql-in-60-seconds-with-rails</link>
      <category>Ruby on Rails</category>
      <category>PostgreSQL</category>
      <category>rails</category>
      <category>activerecord</category>
      <category>postgresql</category>
      <category>mysql</category>
      <category>fusefs</category>
    </item>
  </channel>
</rss>
