Ubuntu14.04 use Rails to connect to the MySQL database

Source: Internet
Author: User
Rails comes with sqlite3 in all aspects, but the free version lacks a fatal function: Add a password! Although the third party has a compiled binary version of the encrypted version, let's not go to mysql first. it is very easy to install mysql In Ubuntu. I will not talk about it first. I will first talk about how to change rails to the mysql driver and use the following command: railsnewxxx-dmysql because the new version of rails supports mysql using the gem mysql2, so in Gemf

Rails comes with sqlite3 in all aspects, but the free version lacks a fatal function: Add a password! Although the third party has a compiled binary version of the encrypted version, let's not go to mysql first.

It is very easy to install mysql In Ubuntu. I will not talk about it first. I will first talk about how to change rails to the mysql driver and use the following command:

Rails new xxx-d mysql


Because the new version of rails supports mysql using the gem mysql2, you need to add the 'mysql2' gem in Gemfile; however, with the above command, rails will automatically add this sentence in Gemfile, so you don't need to worry about it.

Rails naturally needs to download and install the mysql2 package, but an error will be reported here, prompting that the mysql header file cannot be found. It seems that you need to download the mysql dev package and other things. You can run the following command to install it:

Sudo apt-get install libmysqlclient-dev


This time rails new is okay. Next we will create a model:

Rails g scaffold book name: string count: integer price: integer remark: string


Create a new database as follows:

Rake db: migrate

An error occurred. The system prompts that mysql cannot be connected. The mysql service is not in the listening status! Now let's start to install the mysql server:

Sudo apt-get install mysql-server


Next, the mysql client:

Apt-get isntall mysql-client


When installing the server, you will be prompted to enter the root password. Here I enter abc. If it is a pure number (such as 123), In the rails database. yml configuration should be included in '123456' (I have not tried it yet, and other people's experience on the Internet ). after the server is installed, run the following command to determine whether to start listening in the mysql Background:

Sudo netstat-tap | grep mysql


If the mysql server is installed and listening, you can create a database in rails:

Rake db: create-v

Rake db: migrate

In linux, the path of the mysql database is/var/lib/mysql.


We can confirm the created mysql database. Open the terminal and enter the command:

Mysql-u root-p

Mysql will prompt you to enter the password to log on, enter the previously set password abc, and successfully log on to mysql; then you can use show databases; to view the existing database:

Mysql> show databases;
+ --------------------- +
| Database |
+ --------------------- +
| Information_schema |
| Db_test_development |
| Db_test_test |
| Mysql |
| Performance_schema |
+ --------------------- +
5 rows in set (0.00 sec)


Use the use mysql command to select the current database:

Mysql> use db_test_development;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with-

Database changed


Let's take a look at all the tables in the database:

Mysql> show tables;
+ ------------------------------- +
| Tables_in_db_test_development |
+ ------------------------------- +
| Books |
| Schema_migrations |
+ ------------------------------- +
2 rows in set (0.00 sec)


Finally, let's take a look at the structure of the books table:

Mysql> show columns from books;
+ ------------ + -------------- + ------ + ----- + --------- + ---------------- +
| Field | Type | Null | Key | Default | Extra |
+ ------------ + -------------- + ------ + ----- + --------- + ---------------- +
| Id | int (11) | NO | PRI | NULL | auto_increment |
| Name | varchar (255) | YES | NULL |
| Count | int (11) | YES | NULL |
| Price | int (11) | YES | NULL |
| Remark | varchar (255) | YES | NULL |
| Created_at | datetime | NO | NULL |
| Updated_at | datetime | NO | NULL |
+ ------------ + -------------- + ------ + ----- + --------- + ---------------- +
7 rows in set (0.00 sec)

Install Apache server in Ubuntu and use the Passenger plug-in to deploy the Rails application http://www.linuxidc.com/Linux/2012-12/75230.htm

CentOS system build Ruby On Rails platform http://www.linuxidc.com/Linux/2014-11/109590.htm

Build Ruby On Rails http://www.linuxidc.com/Linux/2012-06/61981.htm in Ubuntu

Web development agile way of application Rails agile Web development (the original book version 4th). (US) Sam Ruby). [PDF] + Source Code http://www.linuxidc.com/Linux/2014-09/106975.htm

Rails details: Click here
Rails: Click here

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-01/112443.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.