allstufftech August 16, 2017

Using Joomla and other CMS systems, I have been feeling comfortable with the GUI of the MAMP tool (because I have a Mac but same exists for Windows and Linux named as XAMPP and LAMP). Just one-click start and stop of the Apache and mySQL servers. Also, sometimes restart of Mac systems doesn’t start the mySQL causing some conflicts, etc. So, I wished that such GUI simplicity should exists for me even while working with Rails apps as was there with MAMP.

As you might have already realized that setting up mySQL in any *NIX (even Mac) based system is not so easy, I thought to try out to use my mySQL from MAMP server to be used as Rails app Development Environment DB for testing in local machine. That way you can catch lot of errors and problems in your local machine itself (specially Precision and Scale, etc), which might occur using mySQL DB in Prod environment.

Here is what I did and it worked flawlessly for me.

1. Download and install the latest MAMP server. Install the same in my machine. I never actually preferred MAMP Pro and just like to use MAMP alone itself since it is FREE and who doesn’t like free.

2. Launch the MAMP app and start the servers. This will automatically open a webpage consisting of details about this server and the ports used here.

3. Note the details of the socket and the username and password as provided in the start page. These credentials will be used to connect to the mySQL DB of the MAMP server using our Rails app.

4. Open your rails app file config/database.yml file and add the details for the development environment in the  following manner.

 adapter: mysql2
 database: db_name
 host: localhost
 username: root
 password: root
 socket: /Applications/MAMP/tmp/mysql/mysql.sock

5. Add the mysql2 gem to your Gemfile and run a bundle install

6. To create the database, run RAILS_ENV=development bundle exec rake db:create

7. To create the tables of your app in the database, run RAILS_ENV=development bundle exec rake db:migrate

8. From the MAMP start page, you can click the phpMyAdmin link and verify whether the DB and the tables got created or not. For my case, they were created properly.

Do you feel there is any other smaller shortcut to achieve this OR have you accomplished a better way to handle the mySQL in rails app, feel free to share it here.