Topic: Tutorial: Installing MySQL/MySQL rubygem and phpmyadmin on Mac OS X
July 27th, 2010
Having to do this recently, I realized this can become a beginners worst nightmare. After 2 days, a complete operating system uninstall/reinstall, multiple MySQL install/uninstall/reinstalls and gem uninstall/reinstalls, I finally have apache/php/MySQL/phpMyAdmin/ and MySQL gems configured, installed and running on my Mac OS X Snow Leopard.
Here is a tutorial that will hopefully come in handy to beginners or anyone else that has the same problems I had (I will also provide various resources at the end of this tutorial). However, it’s mostly oriented towards beginners.
This tutorial does not cover rails installation.
First of all, this tutorial is intended for people using Mac OS X Snow Leopard looking to download the 64 bit versions of MySQL and the 2.8.1 MySQL gem. Here is my current set up, so I can’t guarantee it will work for people with older set ups, but if you have a very similar set up this should work just fine:
Mac OS X Snow Leopard 10.6.4
Ruby version: 1.8.7 (64 bit, comes with Snow Leopard)
Rails version: 2.3.8
MySQL: 5.1.49 (64 bit)
MySQL gem: 2.8.1
You shouldn't have MySQL, phpMyAdmin, or MySQL gem yet. The above is to show you what I have at the end of this tutorial and what rails version I intend to use. The ruby version should be a prerequisite though, you need that 64 bit version (the 64 bit 1.8.7 version comes with snow leopard).
This will require use of the terminal/command line, found in Applications/Utilities/Terminal.
Since pretty much all the command line commands and configuring requires root permissions or using the ‘sudo’ (superuser do) command, It will be easier to just open the terminal and type ‘sudo –s’ which will start a terminal session acting as root user. You will have to enter your password to do so.
From here on, I’m assuming that you will type ‘sudo –s’ to start a session as root user, otherwise most of these commands will have to be prefixed with ‘sudo’.
If at any point you don’t know where you are when using terminal, type ‘pwd’ and it will tell you the path to where you are currently in.
Here’s how to find what versions you have if any (go to terminal and type these commands):
Unless you know you downloaded phpMyAdmin before, you don’t have it.
Ruby: ‘ruby –v’
Rails: ‘rails –v’
MySQL: ‘mysql –version’
MySQL gem and any other gems: ‘gem list’
We are going to type commands, like:
to open files in the command prompt(terminal).This assumes you have installed the Unix command for your text editor. Usually, when installing the editor it will ask you if you want to install the editor name command (at least TextMate did) For Instance, I use text mate, so I would do:
Before we begin, one big problem I ran into was that I had previously tried to compile and install mysql, and I wasn’t able to install the mysql package installer because of it. If you have mysql installed which you wish to get rid of and have a new, clean install, this is what you need to do to remove it:
Stop the database server if it’s running.
rm –rf /usr/local/mysql*
rm –rf /Library/StartupItems/MySQLCOM
rm –rf /Library/PreferencePanes/My*
edit /etc/hostconfig (for me, ‘mate hostconfig’) and remove the line ‘MYSQLCOM=-YES-‘
rm –rf ~/Library/PreferencePanes/My*
rm –rf /Library/Receipts/mysql*
rm –rf /Library/Receipts/MySQL*
‘rm’ means remove, and –r means recursively, since these are directories it’ll have to recursively remove all files in directories, and –rf, the ‘f’ stands for force, meaning remove write-protected files without prompting.
Thanks to Rob Allen for the info on removing mysql.
At this point I’m assuming there is no trace of MySQL anywhere on your system. We want to install this from scratch. We are going to download the MySQL package installer, not install and compile it from source, if you want to do that, here is a good tutorial for that: http://hivelogic.com/articles/compiling ow-leopard
Now, we need to download the package installer, which can be found here.
Download the ‘Mac OS X ver. 10.6 (x86 64-bit), DMG Archive’. You have to either login or create a user name and login I believe, and then it will bring you to a page where you have options of where to install from. This doesn’t really matter, select either http or FTP from locations near you.
When it’s done downloading, the package should open, if not, click on the package from my downloads. You need to install everything in the package in this order: ‘mysql-5.*.*-osx10.6-x86_64.pkg’, then ‘MySQLStartupItem.pkg’, then ‘MySQL.prefPane’.
MySQL should be installed in /usr/local/
Open the terminal and type (be in a root user session, remember 'sudo -s'):
cd /usr/local ls
You want to cd (change directory) to /usr/local then type 'ls' (list) to see the contents of that directory.
Ok, we need to fix the mysql.sock location in php.ini, which is found in /etc/php.ini
We need to replace the three occurrences of ‘/var/mysql/mysql.sock’ with ‘/tmp/mysql.sock’
They look like this:
pdo_mysql.default_socket=/var/mysql/mysql.sock mysql.default_socket=/var/mysql/mysql.sock mysqli.default_socket=/var/mysql/mysql.sock
Like I said, this needs to be change to:
pdo_mysql.default_socket=/tmp/mysql.sock mysql.default_socket=/tmp/mysql.sock mysqli.default_socket=/tmp/mysql.sock
Typically, ‘command+f’ will bring up a utility to help you find(f) something. I typed ‘command+f’ in textmate then entered:
to find where the locations of this line were in the file. Then I replaced it with ‘/tmp/mysql.sock’.
To get an idea, the lines that you need to change look like:
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
Change '/var/mysql/mysql.sock' to ‘/tmp/mysql.sock’. After this, there are still two more locations in the php.ini file that need to be changed.
*** Very Important: We now need to create the path to mysql before mysql commands will register and before we can use mysql.
To do this we have to use terminal to open a file named ‘.bash_login’. It is found in your home directory, or ‘/Users/your_user_name’. I’m still assuming you’re in a root user session (accomplished with typing ‘sudo –s’ at the start of working with terminal. If you aren’t do so, other wise you have to prefix things with the ‘sudo’ command).
Now that you’re in the home directory, type ‘ls’. See if a file named ‘.bash_login’ is there. Note the preceding ‘.’, that’s important. If it’s not there, that’s ok, we can make the file.
Now inside the file we need to insert this line, to set the path variable:
put that at the end of the file and save it as ‘.bash_login’ to the home directory (remember the preceding period!).
if it’s there, it may be empty or have a line or two in it, that’s fine. If it’s not there, open up a file just like I did above anyways, we will save it to the home directory and then it will be there.
Now the path should be set.
You can now start MySQL by going to the apple icon on your tasks bar then to system preferences. At the bottom of system preferences you should see a tab that says ‘other’ with a mysql icon. Click on that and you will see where you can start/stop mysql.
It should be up, running and working just fine!
To check it out, let’s create a user named root with a password:
mysqladmin –u root password ‘a_password’ mysql –u root –p
The first line created a user named root with the password you gave it. The second will prompt you for the password you just created. Enter the password and you will start a new mysql session.
Testing this, we could drop the database named ‘test’ which is automatically created when mysql is installed.
DROP DATABASE test;
And add a new one:
CREATE DATABASE your_test_db;
Now we are going to install the mysql gem. Here is the easy part, just type this command in and it should be all good:
sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
The sudo only needs to be there if you’re not in a root user session. The above is just one line, no line breaks.
The gem should install, and you should be ready to use MySQL with rails.
For more info on installing mysql, using it, and installing the mysql ruby gem, go here
*Note: The link above takes you to the wiki for installing mysql 32 bit and the mysql 32 bit gem. We just installed the 64bit mysql and gem, so don’t use that gem install command, use the one I provided above. You can use this wiki for some of the mysql commands under the heading ‘Securing your installation’.
Lastly, we are going to install and configure phpMyAdmin, which is a graphical user interface (GUI) for MySQL.
First thing we’re going to do is download phpMyAdmin from http://www.phpmyadmin.net/home_page/downloads.php
You should download the 3.3.5 zip version (at the time of writing this).
Once you download it and it’s unzipped, we can use some Unix commands to move the contents of the phpmyadmin download into a folder that we will create.
The easier way would be to click on your Macintosh HD icon and drag the phpMyAdmin folder into '/Library/WebServer/Documents' folder. But I believe in the importance of becoming familiar with Unix/Linux shell commands, which this tutorial covers a little of to also help beginners. So I will provide you with that way as well.
Say you drag the phpMyAdmin folder to the desktop. From here, we go to Terminal and first create a new directory named ‘pma’ to put the contents of the phpmyadmin folder. (note: we don’t need to create a name for the new folder, but it will be easier when typing in the url. Plus you will need a directory to specifically put it in.)
We can type ‘ls’ (list) to view the contents.
Remember, if at any point you forgot where you are, just type ‘pwd’ to see the path to where you are. Now to create a new directory, use the make directory command (mkdir)
Type ls again to make sure it’s there. Now we need to go to desktop
cd /Users/your_user_name/Desktop cp phpMyAdmin-3.3.5-english /Library/WebServer/Documents/pma
This copies the contents of phpMyAdmin-3.3.5-english into the ‘pma’ directory found in this path
Go back to the pma directory
cd /Library/WebServer/Documents/pma ls
Make sure the contents are there.
Ok now we need to alter and rename a file. While still in the ‘/Library/WebServer/Documents/pma’ directory, there is a file named ‘config.sample.inc.php’ , Let’s open it:
Thanks to Johan Nyberg, I was informed you should also rename the above "config.sample.inc.php" to just "config.inc.php"... seems obvious enough but I never use phpMyAdmin so I didn't even think twice about it.
We need to enter a string for blowfish encryption. It looks like this:
$cfg['blowfish_secret'] = '';
enter random letters and digits (a string):
$cfg['blowfish_secret'] = '82xnTsI9jk98uq';
Also make sure this is set like this
$cfg['Servers'][$i]['host'] = '127.0.0.1'; # localhost is default, but didn't work for me ! $cfg['Servers'][$i]['connect_type'] = 'socket'; # tcp is default, but did not work for me ! $cfg['Servers'][$i]['auth_type'] = 'cookie'; . $cfg['Servers'][$i]['user'] = ''; cfg['Servers'][$i]['password'] = '';
Make sure all these are there (they probably won't be in that order, that's ok). Some of them will already be there, you just may need to change a thing or two. I think you will have to add the 'user' and 'password' ones yourself though.
thanks to studioTips for this info: http://www.studiotips.net/docs/tips_sql admin.html
Now go to your browser and type ‘localhost/pma’ in your url bar. You should see a phpmyadmin login page. If not, try '127.0.0.1/pma'
Remember that login we created for mysql earlier? You can use this to log in to phpmyadmin.
You should now be presented with a welcome page on phpmyadmin and can take it from here.
That's it! You are ready to use mysql with or without rails now, and with or without php my admin.
Note, there are changes in database.yml (in your rails app) that need to be changed when using mysql with rails. That rails mysql wiki I provided talks about that
If there are any mistakes or corrections, please comment, I will change it.
Last edited by RailsRhino (2011-03-29 21:43:38)