首页 > 代码库 > 新建以MySql为数据库的Rails项目
新建以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 RubyThis 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 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 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::ErrorAccess denied for user ‘root‘@‘localhost‘ (using password: NO)
七、提示说接入被拒绝,显然是没有配置rails中的数据库配置文件,进入rails项目中的config目录,打开database.yml文件
可以看到第6行中的password为空,故添加本机中的mysql下的root用户的password,打开终端,按Ctrl+c终止rails项目,接着重启项目
1 default: &default2 adapter: mysql23 encoding: utf84 pool: 55 username: root6 password: 7 socket: /var/run/mysqld/mysqld.sock
八、重新启动项目:
1)重启项目:
$ rails server
2)访问项目:
http://localhost:3000
3)页面报错:
ActiveRecord::NoDatabaseErrorUnknown database ‘weibo_development‘Run `$ bin/rake db:create db:migrate` to create your database
4)显然还是数据库配置文件中的错误,提示没有‘weibo_development‘数据库,导致数据库迁移失败,还是到项目中的config下打开database.yml文件
1 development:2 <<: *default3 database: weibo_development
九、可以看到,测试环境下用的是weibo_development数据库,这才想起本机的mysql还没有新建该数据库,故进入终端执行:
1)进入MySql:
$ mysql -u root -pEnter 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> exitBye
十、重新启动项目,并访问项目:
1)重启项目:
$ rails server
2)访问项目:
http://localhost:3000
3)成功提示:
Welcome aboardYou’re riding Ruby on Rails!
新建以MySql为数据库的Rails项目