完成一个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
查看GemFile:
$cat Gemfile
.......
gem 'mysql2'
......
2.创建数据库
$ rake db:create RAILS_ENV='development'
$ rake db:migrate
$ mysql -u root
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| depot_development |
| depot_test |
| mysql |
| performance_schema |
| test |
+----------------------+
在执行rake的时候,由于在config/database.yml中指定使用的密码。
因此,需要让你输入密码。
但是,当命令执行完毕后,登录mysql的时候,如果使用mysql -u root -p:
$ mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
而是,直接使用mysql -u root,可以登录到mysql。
菜鸟还不明白....
另外,在生成到工程中到script文件里,没有generate。研究中。
在其中遇到了一些问题,有点乱,暂记录如下:
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.