Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect to MySQL in Ruby on Rails?

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.

like image 443
jesper Avatar asked Mar 25 '13 11:03

jesper


People also ask

Is Ruby a MySQL connector?

The mysql2 Ruby gem provides an API for connecting to MySQL, performing queries, and iterating through results; it is intended to support MySQL 5.6, 5.7, and 8.0.


2 Answers

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.

like image 57
wintermeyer Avatar answered Sep 26 '22 15:09

wintermeyer


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

like image 36
Stobbej Avatar answered Sep 22 '22 15:09

Stobbej