Friday, December 27, 2013

Part 1: How to install Rails and MySQL in Mavericks OSX


Install Rails

My buddy Mark Moser sent me to Andrew Kennedy's helpful blog so I could install Rails on my new MacBook (OSX Mavericks). My install did not quite follow Kennedy's documentation exactly so I've detailed below what works for me.

Install Xcode from the App store.

Run Xcode to initialize stuff.  I failed to do this the first time and I think it caused my Homebrew install (we do this later) to fail.

Install the command line tools:

xcode-select --install

Install Homebrew.  "Homebrew installs the stuff you need that Apple didn’t."  

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Install RVM.  RVM is Ruby Version Manager.  I think this lets me install and run multiple environments (Ruby versions and collections of gems).  The first time I tried to install rvm, the script complained and told me:

RVM PATH line not found for Bash, run the installer with '--auto-dotfiles' to fix it.

So I installed RVM like this:

curl -L https://get.rvm.io | bash -s stable --auto-dotfiles

The script gives this warning:

  * WARNING: You have '~/.profile' file, you might want to load it,
    to do that add the following line to '/Users/<username>/.bash_profile':

      source ~/.profile

I could not install the requirements (the next step) without sourcing the .profile (above).  

[Note to self: next time, insert this into my .bash_profile, exit the terminal, and then open a new terminal before proceeding.]

Install rvm requirements.  This figures out additional stuff needed and installs it - autoconf, automake, libtool, pkg-config, gcc46, libyaml, readline, libksba, openssl

rvm requirements

Note that this took a very, very long time and seemed stuck like this for an hour (crazy, right?) or so:

Installing required packages: autoconf, automake, libtool, pkg-config, gcc46, libyaml, readline, libksba, openssl........

The man page for rvm says this about requirements:

Show additional OS specific dependencies/requirements for building various rubies.

The thing that's weird about this is I think it installs these requirements; I don't think it only shows (displays) them.

Since we made changes, now we need to reload rvm:

rvm reload

Make sure we are running the most current stable version of rvm:

rvm get stable

Kennedy's blog guides us to install Ruby version 2.0.0.  I was already running ruby 2.0.0p247.  We can determine version info by running

ruby -v

I don't know if this version came with OSX or if I installed it during a previous session of tinkering.  Regardless, I went ahead and ran Kennedy's suggestion:

rvm install 2.0.0

RVM went out and found 2.0.0-p353 -- cool!  Notice it is slightly newer than the version I already had (p247).

I have no interest in legacy versions or enterprise edition, so I skipped those parts of Kennedy's blog.

Install mysql

Also, I chose to install mysql from MySQL.com at Mark's suggestion instead of installing it via Homebrew.

Get mysql from here to download the dmg

Open the disk image and run the mysql package.  I had to right-click and choose "Open" to get around the security feature that prevents some programs from running.

Mysql is now installed but not configured.  Time to do that.  I followed some of the documentation here and here

Add the mysql binaries to our environment path so bash can find them:

Insert the following line in ~/.bash_profile

export PATH=$PATH:/usr/local/mysql/bin

Exit the terminal session and open a new one.  The path environment variable is now set.

For the following commands to work, we need to change our directory:

cd /usr/local/mysql

Change user and group ownership so mysql is accessible to the mysql user.  Run these commands with sudo:

sudo chown -R mysql .
sudo chgrp -R mysql .

Start mysql:

sudo bin/mysqld_safe --user=mysql &

Any time you append an ampersand (&) to a bash line, you are telling bash to run this command indefinitely in the background.  So the above command runs a mysql daemon (service) called mysqld_safe as user "mysql" and runs it in the background.

This command tests mysql to see if it is running:

mysqladmin version

The system should respond with version information.

Gently shut down mysql:

bin/mysqladmin -u root shutdown

Restart mysql using the same command we used above:

sudo bin/mysqld_safe --user=mysql &

Make sure mysql is running.  The following command will display available databases:

mysqlshow

We now have mysql running but we are using default passwords.  Time to change that.  Connect to mysql as root:

mysql -u root

Note: the mysql "root" user is not the same as the operating system's "root" user. 

Display a list of users:

SELECT User, Host, Password FROM mysql.user;

System replies:

+------+-------------+----------+
| User | Host        | Password |
+------+-------------+----------+
| root | localhost   |          |
| root | myosx.local |          |
| root | 127.0.0.1   |          |
| root | ::1         |          |
|      | localhost   |          |
|      | myosx.local |          |
+------+-------------+----------+

Change the root password with the following command.  

UPDATE mysql.user SET Password = PASSWORD('YourNewPassword')
WHERE User = 'root';

Tell mysql to re-read grant (permission) tables so it will notice the password change:

FLUSH PRIVILEGES;

Confirm passwords were updated:

SELECT User, Host, Password FROM mysql.user;

The Password column will no longer be blank.

Sidenote: We could have changed the password by issuing four SET PASSWORD commands but I was lazy.  I list it here for documentation purposes - don't need to do this now:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'::1' = PASSWORD('newpwd');
SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd');

Leave the mysql session:

exit

Now when we want to connect as root, we have to supply a password.  The command below starts up a mysql session as user "root" and tells the system to prompt us for a password:

mysql -u root -p

The documentation goes over removing anonymous accounts and securing test databases.  You can read that documentation here.  I am not following it because this is my play/dev machine and I want to leave these accounts as-is.  If you are connecting a system to a network, you should probably follow the documentation and remove them.

Now we have mysql running and configured, but we started it manually.  We now need to configure mysql to start automatically.

First, leave the mysql session and shut mysql down gently:

exit
bin/mysqladmin -u root -p shutdown

Notice this time we had to tell mysql to prompt for a password (the "-p" option).  Previously, we omitted that flag because there was no password assigned to root.

I followed the documentation here.  Open up the contents of the mysql disk image file that we previously downloaded.

Run MySQLStartupItem.pkg (right-click | Open)

The installer will guide you through the process.

Once done, look in the mysql disk image file one more time.  Run (right-click | Open) MySQL.prefPane.  This will install a MySQL utility in System Preferences.  Go there to start/stop MySQL and to tell MySQL whether or not to start automatically.


[To be continued in Part 2]

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.