How can I connect to MySQL in Ruby on Rails?

62,713

Solution 1

You don't have to do those things manually, check this: http://guides.rubyonrails.org/configuring.html#configuring-a-database

Solution 2

Have a look into the configuration file config/database.yml

You need to setup your configuration there. Here is an example for the production environment:

production: 
   adapter: mysql2
   encoding: utf8 
   database: example 
   pool: 10 
   username: example 
   password: secure 
   socket: /var/run/mysqld/mysqld.sock 
   reconnect: true

In addition to that you have to add gem 'mysql2' in your Gemfile and run bundle install.

Solution 3

Contents of my config/database.yml file:

# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# Install MySql gem if not already there.
# Below command installs some pre-requisites for the installation:
#   sudo apt-get install libmysqlclient-dev mysql-client
# After above, this to finish gem installation:
#   gem install mysql2
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: YOUR_DATABASE_HERE
  pool: 5
  username: root
  password: root

As the comments above the configurations say, you might need to install the mysql2 gem first via the terminal. After installation is finished, do a bundle install and rake db:migrate and can then access the database via phpmyadmin too.

I had just stumbled upon this question an hour ago, more than 2 years later since the question was asked. Although I understand this is very late and for sure OP must have solved this, for the sake of other beginner users like me who might be coming here for a solution, I thought of writing my solution here. Hope it helps.

Share:
62,713
jesper
Author by

jesper

Updated on June 13, 2020

Comments

  • jesper
    jesper almost 4 years

    I am really new in Ruby on Rails. I have read this tutorial and it sounds really easy.

    But how can I connect to my database (MySQL) or what does Rails use? In php I'd use...

    mysql_connect("...","...","...");
    mysql_select_db("...");
    

    I have searched google and cannot find any useful tips.

  • jesper
    jesper about 11 years
    Ok i am used to use phpmyadmin, so when can i access phpmyadmin? And how do i know what my password etc is? in database.yml?
  • Stobbej
    Stobbej about 11 years
    Well, when you created your own database, you configured it with credentials, right? If you didn't create it by yourself, you should ask the db admin. There is no default substitution for phpmyadmin, there are some engines/gems that minic that behaviour, though: stackoverflow.com/questions/8554676/… I dont use it though, I prefer the mysql cmd line, or when your on a Mac: Sequel Pro
  • jesper
    jesper about 11 years
    Ok my current database is "db/development.sqlite3" . I can use that, but how can i create tables like i did in phpmyadmin? Is there somekind of administrationpanel?
  • wintermeyer
    wintermeyer about 11 years
    You want to do that with migrations. When ever you use rails generate model xyz name:string xyz:string the generator script automatically generates a migration for you. You can start the migrations with rake db:migrate. Have a look at xyzpub.com/en/ruby-on-rails/3.2/… for a step by step description.
  • jesper
    jesper about 11 years
    Ok i have done rake db:migrate, but ... whats next step? How can i create tables?
  • wintermeyer
    wintermeyer about 11 years
    Please read xyzpub.com/en/ruby-on-rails/3.2/… Ruby on Rails is not easy to understand. You'll have to invest the time to read this stuff. It is very different to PHP.
  • Bruce Sun
    Bruce Sun about 6 years
    It looks like a mysql client is required, after adding gem 'mysql2' to my Gemfile and bundle install, it tells me to brew install mysql. Is there a way to connect to mysql without installing whole mysql homebrew formula?