h1

Bashing up MongoDB on Mac OS X (10.6)

2010/09/18

UPDATE:: Some spacing on the bash files is off; sorry

I’m new to MongDB and was reading through their documentation and tutorials on their website. I came across these directions for starting in Mac (context: open a terminal and type the first line, then open another terminal and type in the next line):

$ ./mongodb-xxxxxxx/bin/mongod
$ ./mongodb-xxxxxxx/bin/mongo

Are you kidding me!! I’m not typing that every time. Luckily someone posted directionsfor bash aliasing (here). His method seems a little unique to him. So I’ll  share mine. Essentially, we don’t need to setup any real bash aliases, we only need to copy the binaries from ‘mongodb-osx-i386-x.x.x/bin’ (or for me personally I have the mongodb-1.6.2-x86_64/bin’ version) to the bin file in my home directory ~/bin. I just need to make sure my path variables are set in .profile to read from ~/bin and then I’m set.

I will spend the rest of this post discussing bash files and relating that discussion to setting path variables for mongo.

Simple mongd and mongo commands in bash

I like by bash environment structured in certain way in order to take advantage of certain customizations I like to see in my bash shell. In this way I have my .bash_profile, .bash_aliases, .profile and .bashrc files. This may seem overly modular but I assure you that if you customize bash terminals to a small degree you know that .bashrc can get unwieldy and unorganized.

My .bash_profile simply redirects to .profile and .bashrc. This is because .bash_profile is the first thing bash “sees” so you can either throw code in their or direct to where the code is more organized.  The next file read is .profile and this is a nice place to define path variables. I’ll show the whole file here for the fun of it

========begin .profile============

### ~/.bash_profile is read first, then ~/.profile, then ~/.bashrc

export PATH=~/bin:/usr/local/bin:/usr/local/sbin/:/usr/local/jruby/bin:$PATH

### color
export CLICOLOR
export LSCOLORS=”dxfxfxfxbxfgedabagacad”
#            dx(1)-fx(2)-fx(3)-fx(4)-bx(5)-fg(6)-ed(7)-ab(8)-ag(9)-ac(10)-ad(11)

ruby –version
#rails –version
/Users/jbowles/.rvm/bin/rvm list

function parse_git_branch {
git branch 2> /dev/null | sed -e ‘/^[^*]/d’ -e ‘s/* \(.*\)/(\1)/’
}

RED=”\[33[0;31m\]”
GREEN=”\[33[0;32m\]”
WHITE=”\[33[0;37m\]”
CYAN=”\[33[0;36m\]”

PS1=”$RED\$(date +%H:%M) \w$GREEN \$(parse_git_branch)$CYAN[\$(~/.rvm/bin/rvm-prompt i v g)]$WHITE\$ “

[[ -s “$HOME/.rvm/scripts/rvm” ]] && source “$HOME/.rvm/scripts/rvm”

### the following are from
### http://stefaanlippens.net/my_bashrc_aliases_profile_and_other_stuff

### Add some more custom software to PATH.
# PATH=$PATH:~/usr/bin
# export PATH

# ### Make sure pkg-config can find self-compiled software
# ### and libraries (installed to ~/usr)
# PKG_CONFIG_PATH=$PKG_CONFIG_PATH:~/usr/lib/pkgconfig
# export PKG_CONFIG_PATH

# ### Add custom compiled libraries to library search path.
# LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/usr/lib
# export LD_LIBRARY_PATH

# ### Add custom compiled libraries to library run path.
# LD_RUN_PATH=$LD_RUN_PATH:~/usr/lib
# export LD_RUN_PATH

### Add custom Python modules to the Python path.
# PYTHONPATH=$PYTHONPATH:~/slippenspythonlib
# PYTHONPATH=$PYTHONPATH:~/usr/lib/python2.5/site-packages
# export PYTHONPATH

### Java’s CLASSPATH customization
#CLASSPATH=$CLASSPATH:~/foo/bar.jar
#export CLASSPATH

### rvm path variables
[[ -s ~/.rvm/scripts/rvm ]] && source ~/.rvm/scripts/rvm

==========end of .profile=========

I have this set up to tell me what version of ruby I am running at the moment from rvm.  Most important here, I have the path variables set up for and ~/bin, which is the bin directory in my home, which is also where I copied all the mongo binaries. This is all I need to run mongo as : mongod, mongo.

The next file to be read is the .bashrc, which is a good place to define things like color schemes and to direct bash to actual aliases. Notice also that since I have rvm (Ruby Version Manger) installed on my system I need to tweek the interactive session definitions.

===========begin .bashrc==============

### ~/.bash_profile is read first, then ~/.profile, then .bashrc

### Check for an interactive session
##if [ -z $PS1 ] && return  ###this must be changed for rvm (look in .profile also):
if [[ -n $PS1 ]] ;  then

### my custom prompt with time and date
PS1=’\[\e[1;37m\]\[\e[0;36m\] \T \d \[\e[1;33m\][\u]\[\e[m\]\[\e[1;31m\]\w\[\e[m\]$ \[\e[1;33m\]\[\e[m\]\[\e[0m\]’

###completion (http://stefaanlippens.net/my_bashrc_aliases_profile_and_other_stuff)
if [ -f /etc/bash_completion ]; then
source /etc/bash_completion
fi

### avoids successive duplication
export HISTCONTROL=ignoredups

## append commands to ~/.bash_history (instead of oeverwritting)
shopt -s histappend

### appends history after every prompt is shown
### instead of after the session
PROMPT_COMMAND=’history -a’

### separate aliases file if needed
if [ -f ~/.bash_aliases ]; then
source ~/.bash_aliases
fi

################################################################
#           defines colors for prompt                          #
################################################################
txtblk=’\e[0;30m’ # Black – Regular
txtred=’\e[0;31m’ # Red
txtgrn=’\e[0;32m’ # Green
txtylw=’\e[0;33m’ # Yellow
txtblu=’\e[0;34m’ # Blue
txtpur=’\e[0;35m’ # Purple
txtcyn=’\e[0;36m’ # Cyan
txtwht=’\e[0;37m’ # White
bldblk=’\e[1;30m’ # Black – Bold
bldred=’\e[1;31m’ # Red
bldgrn=’\e[1;32m’ # Green
bldylw=’\e[1;33m’ # Yellow
bldblu=’\e[1;34m’ # Blue
bldpur=’\e[1;35m’ # Purple
bldcyn=’\e[1;36m’ # Cyan
bldwht=’\e[1;37m’ # White
unkblk=’\e[4;30m’ # Black – Underline
undred=’\e[4;31m’ # Red
undgrn=’\e[4;32m’ # Green
undylw=’\e[4;33m’ # Yellow
undblu=’\e[4;34m’ # Blue
undpur=’\e[4;35m’ # Purple
undcyn=’\e[4;36m’ # Cyan
undwht=’\e[4;37m’ # White
bakblk=’\e[40m’   # Black – Background
bakred=’\e[41m’   # Red
badgrn=’\e[42m’   # Green
bakylw=’\e[43m’   # Yellow
bakblu=’\e[44m’   # Blue
bakpur=’\e[45m’   # Purple
bakcyn=’\e[46m’   # Cyan
bakwht=’\e[47m’   # White
txtrst=’\e[0m’    # Text Reset

### end if statement from rvm custom at top
fi

========================

Lastly come my actual aliases, which are shortcuts for bash commands

======================

###Aliases ##

### looking and moving around
alias ..=”cd ..”
alias ls=”ls -G” # adds color to ls
alias la=”ls -a” # list all, includes dot files
alias ll=”ls -l” # long list, excludes dot files
alias lla=”ls -la” # long list all, includes dot files

======================

That is it. If alias means shortcut, then we have setup MongoDB aliases in bash. If, as I think, alias is a shortcut by way of redefining a command, then we haven’t really set up an alias for MongoDB, but simply changed our path variables to read the binaries from ~/bin.

Advertisements

2 comments

  1. […] Bashing up MongoDB on Mac OS X (10.6) September 2010 4 […]


  2. Like the ‘modularization’ of bash files! Why not just add a couple of aliases e.g.:

    alias mongod=”/Users/yourusername/src/mongodb-osx-x86_64-1.8.1/bin/mongod”
    alias mongo=”/Users/yourusername/src/mongodb-osx-x86_64-1.8.1/bin/mongo”

    I found that worked fine.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: