新建以MySql为数据库的Rails项目

本文主要记录如何新建以MySql为数据库的Rails项目,以及过程中出现错误的解决方案

 

一、新建以MySql为数据库的Rails项目:

$ rails new weibo -d mysql

 

二、发现报错,查看终端中错误信息如下:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
Gem files will remain installed in /home/kolbe/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/mysql2-0.3.17 for inspection.

 

三、试着安装mysql2的gem:

$ gem install mysql2

 

四、发现还是报错,错误信息如下:

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.  You may need configuration options.

 

五、错误提示可能缺少相关必要的libraries,于是去stackoverflow中找答案(Error installing mysql2: Failed to build gem native extension)里面的解决方案是:

$ sudo apt-get install libmysql-ruby libmysqlclient-dev

注:上述两个包的详情可到,ubuntu网站查看(http://packages.ubuntu.com)

1)libmysql-ruby的简介信息:

MySQL module for Ruby

This is an API module that allows to access MySQL database from programs in Ruby programming language. Usually, it will be pulled in automatically by packages providing Ruby programs which need this capability, you only need to install it directly if you intend to write such programs yourself.

This package is a dependency package, which depends on the package containing actual Ruby MySQL module for the default Ruby version (currently 1.8).

2)libmysqlclient-dev的简介:

MySQL database development files

MySQL is a fast, stable and true multi-user, multi-threaded SQL database server. SQL (Structured Query Language) is the most popular database query language in the world. The main goals of MySQL are speed, robustness and ease of use.

This package includes development libraries and header files.

 

六、按解决方案中执行 $ sudo apt-get install libmysql-ruby libmysqlclient-dev 后:

1)重建项目:

$ rails new weibo -d mysql

2)启动项目:

$ rails server

3)访问项目:

http://localhost:3000

4)页面报错:

Mysql2::Error
Access denied for user 'root'@'localhost' (using password: NO)

 

七、提示说接入被拒绝,显然是没有配置rails中的数据库配置文件,进入rails项目中的config目录,打开database.yml文件

可以看到第6行中的password为空,故添加本机中的mysql下的root用户的password,打开终端,按Ctrl+c终止rails项目,接着重启项目

复制代码
1 default: &default
2   adapter: mysql2
3   encoding: utf8
4   pool: 5
5   username: root
6   password: 
7   socket: /var/run/mysqld/mysqld.sock
复制代码

 

八、重新启动项目:

1)重启项目:

$ rails server

2)访问项目:

http://localhost:3000

3)页面报错:

ActiveRecord::NoDatabaseError
Unknown database 'weibo_development'Run `$ bin/rake db:create db:migrate` to create your database

4)显然还是数据库配置文件中的错误,提示没有'weibo_development'数据库,导致数据库迁移失败,还是到项目中的config下打开database.yml文件

1 development:
2   <<: *default
3   database: weibo_development

 

九、可以看到,测试环境下用的是weibo_development数据库,这才想起本机的mysql还没有新建该数据库,故进入终端执行:

1)进入MySql:

$ mysql -u root -p
Enter password:

2)新建数据库:

mysql> create database weibo_development;

3)提示信息:

Query OK, 1 row affected (0.00 sec)

4)执行show databases查看已有的数据库:

复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| weibo_development  |
+--------------------+
4 rows in set (0.00 sec)
复制代码

5)可以看到数据库已经新建成功,接着输入exit退出mysql

mysql> exit
Bye

 

十、重新启动项目,并访问项目:

1)重启项目:

$ rails server

2)访问项目:

http://localhost:3000

3)成功提示:

Welcome aboard
You’re riding Ruby on Rails!

 

你可能感兴趣的:(新建以MySql为数据库的Rails项目)