標籤:
本文主要記錄如何建立以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項目