完成一個Rails項目的新建,及創建數據庫。
步驟:
環境搭建參考:
http://ruby-china.org/wiki/install_ruby_guide
1.創建項目,指定使用到數據庫
$ rails new depot --database=mysql
看下生成的文件:
$ ls
app config.ru doc Gemfile.lock log Rakefile script tmp
config db Gemfile lib public README.rdoc test vendor
查看數據配置文件:
$ cat config/database.yml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: depot_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: depot_test
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: depot_production
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| depot_development |
| depot_test |
| mysql |
| performance_schema |
| test |
+----------------------+
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
1.set used ruby version
$rvm list
rvm rubies
ruby-1.8.7-p370 [ x86_64 ]
* ruby-1.9.3-p194 [ x86_64 ]
# => - current
# =* - current && default
# * - default
$rvm 1.9.3 --default
2.mysql
To use mysql ,u must change config/database.yml,
and also change Gemfile.
3.`resolve': Could not find gem 'mysql (>= 0) ruby
$ rake db:create RAILS_ENV='development'
`resolve': Could not find gem 'mysql (>= 0) ruby...
$ gem install mysql
4.Could not find a JavaScript runtime
$ sudo apt-get install nodejs
5.also mysql
$rake db:create RAILS_ENV='development'
$rake db:migrate
then use mysql command:
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| db/depot_development |
| db/depot_test |
| mysql |
| performance_schema |
| test |
+----------------------+
6 rows in set (0.04 sec)
it's obviously we have made some misktake.
$gedit config/database.yml
del db/
do rake command again.
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| db/depot_development |
| db/depot_test |
| depot_development |
| depot_test |
| mysql |
| performance_schema |
| test |
+----------------------+
8 rows in set (0.00 sec)
depot_development
depot_test
this is what we want.