Helping TextMate find MySQL
February 11th, 2007
A short while ago I started looking into the integration between TextMate, simply the best Mac text editor in my opinion, and MySQL. TextMate has many built-in functions that will allow you to run MySQL queries right from the TextMate window, saving you from having to switch between either the Terminal or local install of phpMyAdmin and TextMate (I’ll use TM from here on). My problem was that each time I tried to run a query from TM, it couldn’t find my install of MySQL. Here’s how I solved the problem.
The first thing I did was send an email to the TM email list. My original email didn’t receive any attention and neither did a follow up. When TM creator Allan Odgaard, seemingly, returned from vacation, low and behold there was my answer.
Allan replied that I needed to set the PATH in the environment.plist file and pointed me to some information on shell commands from the TM manual, paying special attention to the paragraph marked “important”.
Finding environment.plist
Now my goal was to find this elusive environment.plist file. If I couldn’t find it I was determined to make one, the thought of using TM for quick MySQL query checks was just too damn convenient for me. The exact location of this file is ~/.MacOSX/environment.plist.
Because the file is located in an invisible folder, denoted by the “.” (period or dot) at the beginning of the folder, I tried a few different approaches. First I used Terminal and tried cd ~/MacOSX with a directory not found result. Second, I used the “Go to Folder…” command in the Finder’s “Go” menu. Again, nothing found. My final attempt was using Transmit, which I’ve set to always show invisible files, to look through my home directory. As with the others, this returned no results. It was obvious I was going to have to make this directory and file.
Making a Hidden Directory on MacOS X
After a quick search on Google, I found Technical Q&A #1067: Setting environment variables for user processes on the Apple Developers website. In it there was this little tidbit of information:
You will have to create the
.MacOSXdirectory yourself using terminal (by typingmkdir .MacOSX). You will also have to create the environment file yourself. The environment file is actually in XML/plist format (make sure to add the.plistextension to the end of the filename or this won’t work).
So I fired up Terminal again and typed the path of the directory I wanted to create, ~/.MacOSX, making sure I used the correct case for each letter as it’s very important. Also, including the ~/ ensured that no matter where I currently was in the folder structure the new folder would be placed in my home directory.
Creating environment.plist
Now that I had the proper hidden folder I needed to create the file so loginwindow could find it next time I logged in. There are a couple different ways you can do this.
The route that I chose was to use Property List Editor.app which comes bundled with the Xcode developer tools—a free download to ADC members but it also comes with your original DVDs for your Mac.
I opened up Property List Editor and created a new Root node. Then I created a Child node and gave it the name PATH, a class of String, and a value of /opt/local/bin:/usr/bin:/usr/sbin:/sbin. I then save the file as “environment.plist” on my Desktop.
The other option would have been to open up TM, or another text editor, and create a new XML file called “environment.plist”. Then, copy and paste the following code into the file and save it—onto the Desktop for starters, it can be moved later.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>PATH</key> <string>/opt/local/bin:/usr/bin:/usr/sbin:/sbin</string> </dict> </plist>
Moving environment.plist into ~/.MacOSX
At this point I had the appropriate PATH defined in the environment.plist file. The only thing left to do was move the file to the correct location and log out and back in to see my changed take effect.
In order to get the currently visible environment.plist into the hidden ~/.MacOSX directory I had to open up Terminal again. Moving the file was easy, I just typed mv ~/Desktop/environment.plist ~/.MacOSX (if this command doesn’t work, add sudo followed by a space before mv) into the Terminal window and hit Return. Instantly the icon for my environment.plist file disappeared from my Desktop.
Using the ls command in Terminal on the ~/.MacOSX directory it showed one file, environment.plist. Success! Now I just needed to log out and log back in for the changes to actually take effect.
Wrapping it up
Hopefully this will help anyone else having troubles with TextMate finding their MySQL installation. I’ve testing a few different queries through TextMate using the built-in SQL bundle and it seems to be running, now I just need to look into how the SQL bundle itself deals with SELECT queries—because it would be nice to get more than “Query Completed” when I’m testing a SELECT statement, especially a malformed one—and why the Table Browser doesn’t seem to work—another article for another day.
So, as some host on some TV show used to say, good luck and good hunting.
February 12th, 2007 at 3:28 pm
Hey Mike!
How are you doing!?
I’m so happy to be back….
Thanks for stopping by my blog to pay me a visit. =)
I use Text Mate now that I have an iMac. I love it.
*hehe*
February 12th, 2007 at 3:51 pm
Yay! Ana’s back! I’m doing good doll.
TextMate rocks, does it not?
February 12th, 2007 at 8:57 pm
It do’, it do’!
Hey, I requested you as a friend on my9rules!
You haven’t logged in lately, I take it!
I want to add you to my top 10, dawg! LOL
=)
February 12th, 2007 at 9:09 pm
No I haven’t logged in lately. I’m going right now!
(so much for this thread being on topic ;^) *jk Ana)
February 13th, 2007 at 6:44 am
LOL!
Leave it up to me to do that…
Well… anybody’s welcome to leave a comment about your blog entry! =P
July 12th, 2007 at 9:16 pm
I had the same problem and your post helped a lot. However, I still ran into trouble after following everything you said. I think it is because I don’t have any file named mysql (just mysql5), so I executed the command ‘ln mysql5 mysql’ to create a link. After that the database browser finally started working.
Thanks
Z
May 3rd, 2008 at 12:38 pm
I was trying to make it work, when i came by your article. I had a connection problem with ‘/tmp/mysql.sock’ which is the ENV value by default. I use MAMP, which set it in ‘/Applications/MAMP/tmp/mysql/mysql.sock’. If you have the problem, put in your application preferences, in the shell variables, MYSQL_UNIX_PORT=/Applications/MAMP/tmp/mysql/mysql.sock
regards,
E
March 1st, 2009 at 3:41 pm
thanks indeed for your step by step instructions. really helped me out trying to get textmate to find a path!
piaras