Wednesday, March 12, 2008

MySQL on Leopard for rails


SQLite is a great little tool and very easy to use and setup… gem install sqlite3-ruby and you are done!! But now I’m creating a rails application that needs foreign keys and that’s something that SQLite does not do. So MySQL it is…(I’m a MySQL guy if you haven’t figured)

My choice to install MySQL on the macbook was using macports… I’ll assume that macports is installed correctly for the next exercises…  (port version should give you the version number)
Installing MySQL 5.0 with port:

sudo port install mysql5 +server

When the installation is done you have to create the run directory

sudo mkdir /opt/local/var/run
sudo mkdir /opt/local/var/run/mysql5
cd /opt/local/var/run
sudo chown _mysql mysql5
ls -al
total 0
drwxr-xr-x  4 _mysql  admin   136B Mar 12 10:37 mysql5/


setup the config file for MySql. (use the template so you can see where Mysql puts stuff)

sudo /opt/local/share/mysql5/mysql/my-small.cnf /opt/local/etc/mysql5/my.cnf

Testing if MySQL Starts…

sudo /opt/local/lib/mysql5/bin/mysqld_safe5

(you will need crtl-Z and then type bg. This will put the MySQL process in the background. type jobs to see the process)

now connect to mysql with the client

mysql5 -uroot

If you want to stop MySQL you have to kill the process. ps -ax | grep mysqld… there should be one mysqld and one mysqld_sfae5 process get BOTH pids and run kill pid pid.

ps -ax | grep mysqld 70170   ??  S      0:00.01 /bin/sh /opt/local/lib/mysql5/bin/mysqld_safe --datadir=/opt/local/var/db/mysql5 --pid-file=/opt/local/var/db/mysql5/new-host-2.home.pid
70195   ??  S      0:01.22 /opt/local/libexec/mysqld --basedir=/opt/local --datadir=/opt/local/var/db/mysql5 --user=mysql --pid-file=/opt/local/var/db/mysql5/new-host-2.home.pid --port=3306 --socket=/opt/local/var/run/mysql5/mysqld.sock kill  70170 70195

If you only kill one the server will auto restart!!
Running MySQL using MySQL launchctl

This is where I need more help BUT this is what I did and it works:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist  
PS(make sure you kill mysqld_safe5 before doing this...)

Now the part I don’t udnerstand… the server just started but I don’t know how to CONTROL the server like stop, start and restart???

sudo launchctl stop org.macports.mysql5

This did "restart" MySQL but using start/restart did nothing. Looks like when you stop MySQL it aut restart from lunched. 

Password for MySQL root user?

mysqladmin5 -u password 'secret-password'

connecting with password

mysql5 -uroot -p

Monday, January 21, 2008

Mysql driver for rails



When installing the mysql gem you might get this:

Building native extensions.  This could take a while... ERROR:  Error installing mysql: ERROR: Failed to build gem native extension.

the fix is easy but you are going to need the mysql-devel package if you are on redhat then do this:

yum install mysql-devel

Now you can install the mysql driver like this:

gem install mysql -- --with-mysql-config=`which mysql_config`

To test the driver us the rails console:

# ruby script/console
>> require ‘mysql’
>> puts Mysql::VERSION

This should give you the 20700 version. (As of Jan 2007 thats the latest version.)Now enjoy the speed.

Saturday, December 29, 2007

Create a subversion repository for rails



First you need a directory to house your repository. Since I work on multiple projects at home and in the office I need a master directory first. And just to make it simple I use /repos. This could be a link or a real directory.
(To clean-out a old svn directory use the following find . -name .svn -exec ‘rm -rf {}\;‘)
Step 1:

# create /repos

Step 2 (Create a repository):

# svnadmin create /repos/rails_project

Step 3 (Create rails specific directories):

# export REPOS=file:///repos/rails_project
# svn mkdir --message="Initial project layout" $REPOS/trunk $REPOS/tags $REPOS/branches

Step 4 (Initial Import of project):

# cd /rails_project
# svn import . file:///repos/rails_project/trunk -m "Initial Import..." */ This command import everything from the current directory to the svn server running locally using the file:/// structure */

Step 5 (check-out):

# svn co file:///repos/rails_project/trunk rails_project
# svn co svn://subversion_server/rails_project/trunk rails_project

Both of the above command will checkout the trunk of rails_project into a directory called rails_project.

Step 6 (Rails clean-up):

Go into the newly checked-out rails project

*/ Remove and Ignore the log files */  

# svn remove log/* 
# svn commit -m 'removing all log files from subversion' 
# svn propset svn:ignore "*.log" log/ 
# svn update log/ 
# svn commit -m 'Ignoring all files in /log/ ending in .log */ Remove and Ignore tmp directory */   
# svn remove tmp/* 
# svn commit -m 'removing all tmp artifacts from subversion' 
# svn propset svn:ignore "*" tmp/ 
# svn update tmp/ 
# svn commit -m "ignore tmp/ content from now on" */ Move the database config file to an example file and ignore */   
# svn move config/database.yml config/database.example 
# svn commit -m "Moving database.yml to database.example to provide a template for anyone who checks out the code" 
# svn propset svn:ignore "database.yml" config/ 
# svn update config/ 
# svn commit -m "Ignoring database.yml"

 

Tuesday, November 27, 2007

MySQL Dump shell script


I found this script over at http://crazytoon.com/ just thought I would put it on my post-it site to remember.

#!/bin/bash
db=$1
if [ “$db” = “” ]; then
echo “Usage: $0 db_name”
exit 1
fi
mkdir $$
cd $$
clear
for table in `mysql $db -e ’show tables’ | egrep -v ‘Tables_in_’ `; do
echo “Dumping $table”
mysqldump –opt $db $table > $table.sql
done
if [ “$table” = “” ]; then
echo “No tables found in db: $db”
fi

Wednesday, November 21, 2007

Working with Dell RAC

Well again I’m having issues with something. This time is the awesome easy to use greatly improved, best in class super administration toll RAC (Remote access console)from Dell. Because of the way that I’m located in the world, and my vpn connection to the datacenter I can’t get the rac browser plugin to work but there is supposed to be a ssh connection available also.

Got ssh working (enabled by default) just ssh root@server_ip and used the same password as the web client.

Now we need to enable the serial console features:

    $ racadm config -g cfgSerial -o cfgSerialConsoleEnable 1
    $ racadm config -g cfgSerial -o cfgSerialTelnetEnable 1
    $ racadm getconfig -g cfgSerial

    cfgSerialBaudRate=57600
    cfgSerialConsoleEnable=1
    cfgSerialConsoleQuitKey=^\
    cfgSerialConsoleIdleTimeout=300
    cfgSerialConsoleNoAuth=0
    cfgSerialConsoleCommand=
    cfgSerialHistorySize=8192
    cfgSerialCom2RedirEnable=1
    cfgSerialTelnetEnable=1
    cfgSerialSshEnable=1

Modify the Grand Unified Bootloader (GRUB) configuration.
In the Linux configuration file /etc/grub.conf, add the following
two lines in the general settings section of the file:
serial -–unit=0 -–speed=57600
terminal -–timeout=10 serial
Next, append the kernel and console=ttyS0,57600 options
to the kernel line. For example:
kernel /vmlinuz-2.4.20-8smp ro root=LABEL=/
console=ttyS0,57600
If a splashimage directive such as splashimage=(hd0,2)/
grub/splash.xpm.gz appears, comment it out.

Enable login to the console after boot. In the Linux configurationfile /etc/inittab, add a line to configure a getty on the
COM1 serial port as follows:
co:2345:respawn:/sbin/agetty -h -L 57600 ttyS0
vt100

Grant permission to initiate the session. In the Linux
configuration file /etc/securetty, add ttyS0 to the list of
supported ports.

Monday, November 19, 2007

Working with Linux LVM

Growing your lvm partition.

  • Find free space:
  • lvm vgs (VFree is amount of free space)
  • Find the the logical volum:
  • lvdisplay (LV Name is what you want)
  • Extend the drive (Add 50GB to current size) : 
  • lvextend –size +50G /dev/drive
  • unmount the drive (If drive is busy use lsof | grep /mountpoint to find the process that is using the mountpoint)
  • e2fsck -f /dev/drive
  • resize2fs /dev/drivemount
  • done

Its best if the drive is umounted before the lvextend command runs. (I have never had issues with this but you never know. ) 

Thursday, November 8, 2007

hobo 0.64 with rails



ok I know I’m newbo and this is lame BUT here are the steps to get hobo installed and running.There are NO documentation except blogs and new groups for the latests version so here goes…Install latest rails and hobo gems

$ gem install rails –source http://gems.rubyonrails.org$ gem install hobo 

Now get the rails version (its going to be rails 1.99.seomthing) and set the env variable:

$ gem list rails$ export RAILS_GEM_VERSION=1.99.0.8178 
$ hobo myapp
$ cd myapp
$ script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

update the myapp/config/database.yml with the correct database info and your are good-to-go.This is mine:

    development:  adapter: mysql  encoding: utf8  database: myapp_development  username: hobo  password: hobo_password  host: 10.0.0.200 

we also need to create the users table and admin user.

$ ./script/generate hobo_migration create_initial_tables$ g (for [g]enerate migration, don’t migrate yet.)

Now open the migration file db/migrate/001_creat_initial_tables and add the following after the create table end:

def self.up    
create_table :users do |t|      
t.string   :crypted_password, :limit => 40      
t.string   :salt, :limit => 40      
t.string   :remember_token      
t.datetime :remember_token_expires_at      
t.string   :username      
t.datetime :created_at      
t.datetime :updated_at    
end   

# create the admin user now because we can’t do it    
# through the web UI as it would fail validation    
admin = User.new    
admin.crypted_password = ‘ea87f9851d923a9a287631dd082799b83c0fa473′    
admin.salt = ‘7d8b386a7dfdcd0f268794038104791112cd4acd’    
admin.username = ‘admin’    
admin.save_without_validation  
end 

This will create a user ‘admin’ with password => password.
Now start the server

$ ./script/server

go to http://localhost:3000DONE!