新建以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!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章