Create a new rails project with MySQL as a database

Source: Internet
Author: User
Tags most popular database ruby on rails

This article focuses on how to create a new rails project with MySQL as a database and a solution for errors in the process

A. Create a new Rails project with MySQL database:

$ rails New weibo-d MySQL

Second, found an error, check the terminal errors are as follows:

Gem::ext::builderror:error:failed to build gem native extension. Gem files would remain installed in/home/kolbe/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/mysql2-0.3.17 for Inspection.

Third, try to install MYSQL2 gem:

$ gem Install MYSQL2

Four, the discovery or error, the wrong message is as follows:

Error:  error installing MYSQL2:    error:failed to build gem native extension. Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers.  Check the Mkmf.log file for more details.  Need configuration options.

Five, error hints may be missing relevant necessary libraries, so go to StackOverflow find the answer (error installing mysql2:failed to build gem native extension The solution is:

$ sudo apt-get install Libmysql-ruby Libmysqlclient-dev

Note: The details of the above two packages can be reached, Ubuntu website view (http://packages.ubuntu.com)

1) Libmysql-ruby profile information:

MySQL module for rubythis are an API module, allows to access MySQL database from programs in Ruby programming language . Usually, it'll be pulled in automatically by packages providing Ruby programs which need this capability, you only need To install the IT directly if you intend to write such programs yourself. This was a dependency package, which depends on the package containing actual Ruby MySQL module for the default Rub Y version (currently 1.8).

2) Introduction of Libmysqlclient-dev:

MySQL database Development Filesmysql is a fast, stable and true multi-user, multi-threaded SQL database server. SQL (structured Query Language) is the most popular database query Language. The main goals of MySQL is speed, robustness and ease of use. This package includes development libraries and header files.

Six, follow the solution in execute $ sudo apt-get install Libmysql-ruby Libmysqlclient-dev after:

1) Reconstruction Project:

$ rails New weibo-d MySQL

2) Start the project:

$ Rails Server

3) access to the project:

http://localhost:3000

4) Page Error:

Mysql2::erroraccess denied for user ' root ' @ ' localhost ' (using Password:no)

Seven, it is suggested that access is denied, it is clear that the database configuration file in Rails is not configured, enter the Config directory in the Rails project, open the Database.yml file

You can see that the password in line 6th is empty, so add the password of the root user under MySQL in this machine, open the terminal, press CTRL + C to terminate the Rails project, and then restart the project

1 default: &default2   adapter:mysql23   encoding:utf84   pool:55   username:root6   password:7   Socket:/var/run/mysqld/mysqld.sock

Viii. Restart the project:

1) Restart the project:

$ Rails Server

2) access to the project:

http://localhost:3000

3) Page Error:

Activerecord::nodatabaseerrorunknown database ' weibo_development ' Run ' $ bin/rake db:create db:migrate ' to create your Database

4) It is clear that the error in the database configuration file does not indicate a ' weibo_development ' database, which causes the database migration to fail or to open the Database.yml file under config in the project

1 development:2   <<: *default3   database:weibo_development

Nine, you can see that the test environment is using the Weibo_development database, this just think of this machine MySQL has not created the database, so enter the terminal execution:

1) Enter MySQL:

$ mysql-u root-penter Password:

2) Create a new database:

mysql> CREATE DATABASE weibo_development;

3) Prompt Information:

Query OK, 1 row Affected (0.00 sec)

4) Execute Show databases to view the existing database:

Mysql> Show databases;+--------------------+| Database           |+--------------------+| information_schema | | mysql              | | performance_schema | | weibo_development  |+--------------------+4 rows in Set (0.00 sec)

5) You can see that the database has been created successfully, then enter exit to exit MySQL

Mysql> Exitbye

Ten. Restart the project and visit the project:

1) Restart the project:

$ Rails Server

2) access to the project:

http://localhost:3000

3) Tips for success:

Welcome Aboardyou ' re riding Ruby on rails!

Create a new rails project with MySQL as a database

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.