1.創建Heroku賬號以及Heroku app
2.將Heroku app與自己Github下的的項目進行連接
4.下載配置Heroku CLI命令行工具
5.在本機終端中使用heroku login
命令行登陸,會要求heroku的賬號密碼
6.登陸成功後,可以用heroku create
在當前目錄下創建新的heroku app, 若已經有了heroku app ,請使用heroku git:remote -a app_name
切換到現在即將要部署那個heroku app裏
7.使用git remote -v查看與heroku的遠程倉庫連接,輸出如下:
heroku https://git.heroku.com/house-pricing.git (fetch)
heroku https://git.heroku.com/house-pricing.git (push)
origin https://github.com/PENGZhaoqing/HousePricing.git (fetch)
origin https://github.com/PENGZhaoqing/HousePricing.git (push)
8.命令git push heroku master
將當前目錄下的項目push到heroku服務器中,提交前請將所有的修改都commit,輸出如下:
PENG-MacBook-Pro:HousePricing PENG-mac$ git push heroku master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 2.32 MiB | 160.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.2.4
remote: -----> Installing dependencies using bundler 1.15.2
remote: Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote: Fetching gem metadata from https://rubygems.org/..........
remote: Fetching version metadata from https://rubygems.org/..
remote: Fetching dependency metadata from https://rubygems.org/.
remote: Using rake 11.2.2
remote: Using i18n 0.7.0
remote: Using json 1.8.3
remote: Using minitest 5.9.0
remote: Using thread_safe 0.3.5
remote: Using builder 3.2.2
remote: Using erubis 2.7.0
remote: Using mini_portile2 2.0.0
remote: Using rack 1.6.4
remote: Using mime-types-data 3.2016.0521
remote: Using arel 6.0.3
remote: Using execjs 2.7.0
remote: Using htmlentities 4.3.4
remote: Using rubyzip 1.1.7
remote: Using bcrypt 3.1.11
remote: Using sass 3.4.22
remote: Using will_paginate 3.1.0
remote: Using bundler 1.15.2
remote: Using coffee-script-source 1.10.0
remote: Using thor 0.19.1
remote: Using concurrent-ruby 1.0.2
remote: Using multi_json 1.12.1
remote: Using mimemagic 0.3.2
remote: Using pg 0.18.4
remote: Using rails_serve_static_assets 0.0.5
remote: Using rails_stdout_logging 0.0.5
remote: Using ruby-ole 1.2.12
remote: Using tilt 2.0.5
remote: Using tzinfo 1.2.2
remote: Using rdoc 4.2.2
remote: Using nokogiri 1.6.7.2
remote: Using rack-test 0.6.3
remote: Using mime-types 3.0
remote: Using autoprefixer-rails 6.4.0.2
remote: Using uglifier 3.0.0
remote: Using bootstrap-will_paginate 0.0.10
remote: Using coffee-script 2.4.1
remote: Using sprockets 3.6.0
remote: Using rails_12factor 0.0.3
remote: Using spreadsheet 1.1.3
remote: Using activesupport 4.2.5.2
remote: Using loofah 2.0.3
remote: Using axlsx 2.1.0.pre
remote: Using roo 2.4.0
remote: Using bootstrap-sass 3.3.7
remote: Using mail 2.6.4
remote: Using sdoc 0.4.1
remote: Using rails-deprecated_sanitizer 1.0.3
remote: Using globalid 0.3.6
remote: Using activemodel 4.2.5.2
remote: Using climate_control 0.0.3
remote: Using jbuilder 2.4.1
remote: Using rails-html-sanitizer 1.0.3
remote: Using roo-xls 1.0.0
remote: Using activejob 4.2.5.2
remote: Using activerecord 4.2.5.2
remote: Using cocaine 0.5.8
remote: Using rails-dom-testing 1.0.7
remote: Using paperclip 5.1.0
remote: Using actionview 4.2.5.2
remote: Using actionpack 4.2.5.2
remote: Using actionmailer 4.2.5.2
remote: Using axlsx_rails 0.5.0
remote: Using railties 4.2.5.2
remote: Using sprockets-rails 3.0.4
remote: Using coffee-rails 4.1.1
remote: Using jquery-rails 4.1.1
remote: Using rails 4.2.5.2
remote: Using sass-rails 5.0.4
remote: Using turbolinks 2.5.3
remote: Bundle complete! 23 Gemfile dependencies, 70 gems now installed.
remote: Gems in the groups development and test were not installed.
remote: Bundled gems are installed into ./vendor/bundle.
remote: The latest bundler is 1.15.4, but you are currently running 1.15.2.
remote: To update, run `gem install bundler`
remote: Bundle completed (3.21s)
remote: Cleaning up the bundler cache.
remote: The latest bundler is 1.15.4, but you are currently running 1.15.2.
remote: To update, run `gem install bundler`
remote: -----> Installing node-v6.11.1-linux-x64
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: Asset precompilation completed (2.14s)
remote: Cleaning assets
remote: Running: rake assets:clean
remote:
remote: ###### WARNING:
remote: You have not declared a Ruby version in your Gemfile.
remote: To set your Ruby version add this line to your Gemfile:
remote: ruby '2.2.4'
remote: # See https://devcenter.heroku.com/articles/ruby-versions for more information.
remote:
remote: ###### WARNING:
remote: No Procfile detected, using the default web server.
remote: We recommend explicitly declaring how to boot your server process via a Procfile.
remote: https://devcenter.heroku.com/articles/ruby-default-web-server
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> console, rake, web, worker
remote:
remote: -----> Compressing...
remote: Done: 53.3M
remote: -----> Launching...
remote: Released v15
remote: https://house-pricing.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/house-pricing.git
421c4c2..413eb1f master -> master
9.在服務器中建表,運行heroku run rake db:migrate
,然後也可以寫入種子數據heroku run rake db:seed
, 但是若要重構整個數據庫,heroku run rake db:migrate:reset
會失敗,因爲沒有權限,解決方法是使用heroku pg:reset
來重構
10.數據庫建好之後,就可以進行正常訪問了,使用heroku ps
查看運行情況,使用heroku open
訪問網頁
遷移本地數據庫postgres至heroku服務器
1.使用pg_dump your_databse_name > mydb.dump
,將數據庫導出到當前目錄的 mydb.dump文件中,若pg_client與pg_server版本不符合,會報以下錯誤:
pg_dump: server version: 9.5.4; pg_dump version: 9.4.5
pg_dump: aborting because of server version mismatch
解決方案:使用find / -name pg_dump -type f 2>/dev/null
查找所有的pg_dump安裝版本,我的輸出如下(Mac OS):
/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_dump
/usr/local/Cellar/postgresql/9.4.5_2/bin/pg_dump
顯然這裏pg_dump環境變量目錄是在9.4.5_2下,所以纔會提示錯誤,我們只需要使用上面那個路徑下的pg_dump就可以,因此用以下命令解決,:
/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_dump -Fc --no-acl --no-owner -h localhost -U postgres housepricing_development > mydb2.dump
也可以創建新的symlink,這樣以後每次調用就不用加上目錄了:
sudo ln -s /Applications/Postgres.app/Contents/Versions/9.5/bin/pg_dump /usr/local/Cellar/postgresql/9.4.5_2/bin/pg_dump --force
2.將mydb.dump上傳至一個能用HTTP訪問位置,heroku官方推薦使用Amazon的S3,登陸Amazon S3控制檯,新建bucket,在bucket中上傳這個文件,上傳後點擊Make Public讓這個資源能被公共訪問,你也可以使用dropbox等分享這個文件。然後使用 heroku pg:backups:restore dump_url DATABASE_URL
在huroku服務器中恢復本地的數據庫,命令如下:
heroku pg:backups:restore "https://s3-ap-southeast-1.amazonaws.com/campus-portal/mydb1.dump" DATABASE_URL
成功後的輸出:
▸ WARNING: Destructive Action
▸ This command will affect the app house-pricing
▸ To proceed, type house-pricing or re-run this command with --confirm house-pricing
> house-pricing
Starting restore of https://s3-ap-southeast-1.amazonaws.com/campus-portal/mydb1.dump to postgresql-concave-31360... done
Use Ctrl-C at any time to stop monitoring progress; the backup will continue restoring.
Use heroku pg:backups to check progress.
Stop a running restore with heroku pg:backups:cancel.
Restoring... done