如何使用Heroku(Express Server)进行部署时创建MySQL架构

时间:2015-06-18 06:02:08

标签: mysql heroku express cleardb

假设我有以下server.js文件:

server.js

var express = require('express');
var app = express();
var mysql = require('mysql');
var dbhelpers = require('./public/database_helpers.js')
var bodyParser = require('body-parser')



app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public/views'));
app.use(express.static(__dirname + '/public/controlers'));
app.use(express.static(__dirname + '/public/lib'));

app.use(bodyParser())


var connection = mysql.createConnection({
   **Correct info**
});


connection.connect(function(err){
if(!err) {
    console.log("Database is connected ... \n\n");  
} else {
    console.log("Error connecting database ... \n\n");  
}
});


app.post('/signup',function(req,res){
  var newUser = req.body;


  connection.query('INSERT INTO users SET ?',newUser, function(err, rows,fields){
    if (!err){
      console.log("posted to database")
      res.sendStatus(200);
    } else{
      console.log('Error while performing Query.');
      res.sendStatus(500);
    }
  }); 
})

app.get('/artistsearch',dbhelpers.checkDbArtist)

app.post('/artistsearch', dbhelpers.insertDb)

app.post('/reviews',dbhelpers.insertReviewDb)


app.listen(process.env.PORT || 3000);

console.log("Listening at 3000")

我最近部署到heroku并使用了clearDB插件,因为我正在使用MySQL。日志表明我已经能够连接到数据库,但问题是我相信Heroku创建了一个空数据库。如何在clearDB中创建以下模式:

schema.sql文件

CREATE TABLE users(
  users_id INT NOT NULL AUTO_INCREMENT,
  user_username VARCHAR(100) NOT NULL,
  user_password VARCHAR(40) NOT NULL,
  PRIMARY KEY ( users_id )
);

CREATE TABLE artist(
  artist_id INT NOT NULL AUTO_INCREMENT,
  artist_name VARCHAR(100) NOT NULL,
  artist_genre VARCHAR(100) NOT NULL,
  artist_imageurl VARCHAR(100) NOT NULL,
  artist_bio VARCHAR(1000) NOT NULL,
  PRIMARY KEY ( artist_id )
);

CREATE TABLE reviews (
  review_id INT NOT NULL AUTO_INCREMENT,
  user_name  VARCHAR(100) NOT NULL,
  venue VARCHAR(100) NOT NULL,
  number_of_stars INT NOT NULL,
  review_details VARCHAR(10000) NOT NULL,
  artist_id VARCHAR(100) NOT NULL,
  PRIMARY KEY ( review_id )
);

有人有任何想法吗?

1 个答案:

答案 0 :(得分:6)

首先,您应键入heroku config以获取clearDB凭据。

然后,您可以从终端运行此命令:mysql --host=us-cdbr-east.cleardb.com --user=xxxx --password=xxxx --reconnect heroku_xxxxxx < schema.sql