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

作者:kolbe  来源:IT165收集  发布日期:2015-01-12 22:27:42

本文主要记录如何新建以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!
Tag标签: 数据库   项目  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规