如何将本地 MySQL 数据库部署到 Heroku

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15191259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:45:10  来源:igfitidea点击:

How to deploy local MySQL database to Heroku

mysqlheroku

提问by Ken

I'm new to using Heroku and I have a Ruby/Sinatra app in which I plan on using a MySQL database as the main data store.

我是使用 Heroku 的新手,我有一个 Ruby/Sinatra 应用程序,我计划在其中使用 MySQL 数据库作为主要数据存储。

However, I do not want to write results directly to the database on Heroku. Instead, I want to write the results to a local database, and then be able to easily deploy/update/copy my local DB to the "production" database on Heroku.

但是,我不想将结果直接写入 Heroku 上的数据库。相反,我想将结果写入本地数据库,然后能够轻松地将我的本地数据库部署/更新/复制到 Heroku 上的“生产”数据库。

How do I do this?

我该怎么做呢?

Thanks.

谢谢。

回答by DanSingerman

Firstly Heroku natively uses postgres. Life will be easier for you if you use that locally.

首先,Heroku 本地使用 postgres。如果您在本地使用它,您的生活会更轻松。

You can import / export postgres dump files from heroku as described here: https://devcenter.heroku.com/articles/heroku-postgres-import-export

您可以按照此处所述从 heroku 导入/导出 postgres 转储文件:https: //devcenter.heroku.com/articles/heroku-postgres-import-export

If you really want to use mysql, you have two paths to follow.

如果你真的想使用mysql,你有两条路要走。

1) Run mysql locally, but convert to postgres when migrating to Heroku using the mysql2psql gem, as described here: https://devcenter.heroku.com/articles/heroku-mysql

1) 在本地运行 mysql,但在使用 mysql2psql gem 迁移到 Heroku 时转换为 postgres,如下所述:https://devcenter.heroku.com/articles/heroku-mysql

2) Use a mysql addon like https://addons.heroku.com/cleardb

2) 使用像https://addons.heroku.com/cleardb这样的 mysql 插件

However my recommendation would be to use postgres end to end, as it is baked into Heroku, and you'll be working with the default ways of using Heroku, not against them.

但是,我的建议是端到端地使用 postgres,因为它已融入 Heroku,并且您将使用 Heroku 的默认使用方式,而不是反对它们。

Postgres is very good too!

Postgres 也非常好!

回答by Billeh

I know nothing about Ruby & Sinatra, so feel free to comment and let me know how wrong I am if that's the case. However, I thought it might be pertinent to mention the JawsDB Plugin on Heroku, as the top answer here is from 2013 and may be a bit outdated.

我对 Ruby 和 Sinatra 一无所知,所以请随时发表评论,如果是这样的话,请告诉我我错了。但是,我认为在 Heroku 上提及 JawsDB 插件可能是恰当的,因为这里的最佳答案来自 2013 年,可能有点过时。

Here is a link with info on the JawsDB plugin: https://devcenter.heroku.com/articles/jawsdb

以下是 JawsDB 插件信息的链接:https://devcenter.heroku.com/articles/jawsdb

Provisioning the plugin is as simple as running the following command in the CLI:

配置插件就像在 CLI 中运行以下命令一样简单:

heroku addons:create jawsdb

Then configure the host, username, password, & database with parameters from MySQL Workbench (or whatever GUI you use).

然后使用 MySQL Workbench(或您使用的任何 GUI)中的参数配置主机、用户名、密码和数据库。

回答by Michael Tendo Ssemwanga

I wrote a short article

我写了一篇短文

https://medium.com/@michaeltendossemwanga/import-mysql-database-to-heroku-with-one-command-import-db-sql-a932d720c82b

https://medium.com/@michaeltendossemwanga/import-mysql-database-to-heroku-with-one-command-import-db-sql-a932d720c82b

Here is a batch script to fasten the process https://gist.github.com/MichaelTendoSsemwanga/f013963092e3abcce801834871d14b03

这是一个批处理脚本来加快进程 https://gist.github.com/MichaelTendoSsemwanga/f013963092e3abcce801834871d14b03

Save this batch script as import.bat

将此批处理脚本保存为 import.bat

@echo off
heroku config | findstr CLEARDB > config.txt
set /p url=<config.txt

set "string=%url:?=" & set "x=%"
set "x=%string:/=" & set "dbname=%"
echo DB name:   %dbname%
echo DB name:   %dbname% >> config.txt

set "x=%string:@=" & set "substring=%"
set "host=%substring:/=" & set "x=%"
echo Host:      %host%
echo Host:      %host% >> config.txt

set "x=%string::=" & set "substring=%"
set "password=%substring:@=" & set "x=%"
echo Password:  %password%
echo Password:  %password% >> config.txt

set "x=%string:://=" & set "substring=%"
set "user=%substring::=" & set "x=%"
echo User:      %user%
echo User:      %user% >> config.txt

mysql -u %user% -p%password% -h %host% -D %dbname%  < %1

In the heroku app directory, run

在 heroku 应用程序目录中,运行

import db.sql

Command line output

命令行输出