Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mysql with rails - how do I set this up?

I want to use mysql with a rails app (I have never used mysql before). My experience has been solely with sqlite3 and postgresql, which are really easy to use.

I am now creating a new app to learn to use mysql. I installed mysql with homebrew (brew install mysql), and created a new rails app that uses mysql rather than sqlite3. I have included the right gem in my gemfile (gem 'mysql2', '~> 0.2.6').

However, I don't know how to proceed. I have not set up anything more in mysql other than to install it on my system, I don't understand anything to do with how you set it up to run, where it stores the database for my app, and so on.

Please could anyone let me know either the next steps, or a tutorial that would get me understanding enough to develop my app to function just as it would with a simpler (sqlite) database system?

like image 904
jay Avatar asked Mar 25 '12 17:03

jay


1 Answers

I believe the next step would be create the database in "your system" and let Rails know that you want to use that database.

If you want to create the database using the MySQL server directly, you want to log in by using this command from your console:

mysql -u root

By default, mysql root user doesn't need a password, otherwise you would specify it with -p, so it would be:

mysql -u root -p

Once you are logged in your MySQL server, you want to create the database by doing this command:

CREATE DATABASE my_project_database;

You might want to look deeper into users and permissions in MySQL, but this is just a starting point :).

If you want to avoid going into the MySQL server and do it the Rails way, you can create the database using rake. You want to use this command: rake db:create. This will do the same that we did before, but notice that before doing that command you need to create the database.yml file. So let's do that:

You want to have a database.yml file like this:

development:
    host: localhost
    adapter: mysql2
    database: my_project_database
    username: root (notice that you might want to change this user later)
    password:

I suppose that next step would be to create the Migrations that would generate the tables you want to use and so on.

One of the things that I love the most about Rails is its ORM called Active Record. It will abstract all database operations, so you don't have to worry either if you are calling a MySQL or a SQLite...

like image 145
Nobita Avatar answered Oct 16 '22 08:10

Nobita