目标
建立一个 Rails 新项目,使用 Mysql 数据库
1、确认操作环境
进入终端页面
ruby -v
rails -v
git status # 查看 git 状态
rake routes # 查看路由
2、建立新 rails 专案
rails new rails001 #建立名为 rails01 的新项目
cd rails001 #进入 rails01 项目
git init #安装 git
git add .
git commit -m "First Commit"`
3、修改 Gemfile 文件
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
- # Use sqlite3 as the database for Active Record
- gem 'sqlite3'
+ # Use mysql2 as the database for Active Record
+ gem 'mysql2'
4、修改 config/database.yml(数据库配置文件)
修改为下面形式
default: &default
adapter: mysql2 #数据库类型
pool: 5 #链接线路数
timeout: 5000 #超时限制
development:
<<: *default
database: hunter_blog #数据库名字
host: localhost #ip地址
username: root #用户名
password: #用户密码
# 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: #测试环境
# <<: *default
# database: db/test.mysql2
#
# production: #生产环境
# <<: *default
# database: db/production.mysql2
# 启动 Mysql 数据库
mysql.server start
# 建立数据库
rake db:create
git status
git add .
git commit -m "install mysql"