Mac as a Development platform

I sometimes feel as if I spend half my time making fixes caused by Apple upgrades.

This morning I found my previously hand-crafted php.ini had been removed.

So, each time Apple does an upgrade I have to go back in and update php.ini with various customizations (like max post size, etc).

This also happened to Apache. Apple did an upgrade and I lost my virtualhosts.

And other things change too – like I now have to use mysql.server start rather than using mysqld. I can understand that things need to change but why not provide a graphic tool and just change things under the hood.

Now I try restarting MySQL and I get:

So clearly another update has happened that I’ll need to fix.

My experience on Linux has always been much smoother. I’m starting to wonder if I should try using it again as a development platform.

Ubuntu: start/stop mysqld

If you use the init.d script on Ubuntu, e.g. 

sudo /etc/init.d/mysql restart

you may get this message:

Rather than invoking init scripts through /etc/init.d, use the service(8)

utility, e.g. service mysql restart

Since the script you are attempting to invoke has been converted to an

Upstart job, you may also use the stop(8) and then start(8) utilities,

e.g. stop mysql ; start mysql. The restart(8) utility is also available.

mysql stop/waiting

mysql start/running, process 25974

http://askubuntu.com/questions/82374/how-do-i-start-stop-mysql-server

Latin vs UTF-8

Latin is a single-byte encoding.

So, non-Latin characters will break if you try to display them using a Latin encoding.

UTF-8 is potentially a 3 byte encoding and can encode a great deal more. 

Here’s a Unicode/UTF-8 character table:

http://www.utf8-chartable.de/unicode-utf8-table.pl?start=256&unicodeinhtml=dec

If you’re using MySQL, here’s how to ALTER the table to convert to the UTF-8 character set. Note there’s a slight subtlety here in that, because of the extra bytes used by UTF-8, unless you specify the column type it might roll over to the next larger column type size. E.g. for VARCHAR:

ALTER TABLE table_name MODIFY column_name VARCHAR(255) CHARACTER SET utf8;

http://dev.mysql.com/doc/refman/5.0/en/alter-table.html

MySQL: tips

List Users

select * from mysql.user;

Update password

UPDATE mysql.user SET Password=PASSWORD(‘password-here’) WHERE User=’your-user’ AND Host=’localhost’;

FLUSH PRIVILEGES; 

Import data

mysql -u<username> -p<password> -h<hostname> <database_name> < dump.sql

ActiveRecord without Rails

Say you want ORM with a console script.

Here’s how you could do it using ActiveRecord without Rails.

1. create a database YAML file (a la Rails) 

2. Use the ActiveRecord::Base.establish_connection

Super simple. E.g. 

require ‘rubygems’

require ‘active_record’

require ‘yaml’

require ‘mysql2’

require ‘logger’

dbconfig = YAML::load(File.open(‘database.yml’))

ActiveRecord::Base.establish_connection(dbconfig)

class People < ActiveRecord::Base

end

puts People.count

(assuming you had a MySQL database table called ‘people’) with a database.yml like this:

adapter:  mysql2

database: your_database

username: user

password: your_pass

host:     localhost

and you could log it using:

ActiveRecord::Base.logger = Logger.new(STDERR)

See also:

http://blog.aizatto.com/2007/05/21/activerecord-without-rails/

https://adaptivekanban.com/blog/2012/05/dead-simple-activerecord-migrations-without-rails/

Bundler Groups

From Katz’ great post:

Specifying groups allows you to do two things. First, you can install the gems in your Gemfile, minus specific groups. For instance, Rails puts mysql and pg in a database group so that if you’re just working on ActionPack, you can bundle install –without db and run the ActionPack tests without having to worry about getting the gems installed.

Second, you can list specific groups to autorequire using Bundler.require. By default, Bundler.require requires all the gems in the default group (which is all the gems that have no explicit group). You can also say Bundler.require(:default, :another_group) to require specific groups.

http://yehudakatz.com/2010/05/09/the-how-and-why-of-bundler-groups/

and

http://stackoverflow.com/questions/3645968/groups-in-a-gemfile-in-rails-3

Ruby on Rails with Apache and MySQL on Mac OS X Snow Leopard

Snow Leopard is a gnarly old beast.

However, I left my Mountain Lion laptop with someone when visiting and needed to use my old backup Mac running Snow Leopard.

Some hints on how to get stuff (MySQL / Rails) working:

http://geryit.com/blog/installing-mysql-with-rails-on-mac-os-x-snow-leopard/

and

http://hivelogic.com/articles/compiling-mysql-on-snow-leopard