Ruby-on-rails Heroku 应用程序上的远程 mysql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5397028/
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-09-03 00:45:21  来源:igfitidea点击:

Remote mysql database on Heroku app

ruby-on-railsruby-on-rails-3heroku

提问by dormitkon

Can I use mysql database from my personal web server instead of heroku's database?

我可以从我的个人 Web 服务器使用 mysql 数据库而不是 heroku 的数据库吗?

I configured my production database like this:

我这样配置我的生产数据库:

production:
    adapter: mysql2
    database: somedatabase
    username: someusername
    password: somepassword
    host: 1.1.1.1:1234

But, this doesn't work, my app still uses heroku's shared database.

但是,这不起作用,我的应用程序仍然使用 heroku 的共享数据库。

回答by Vince

This is old but in case anyone drops around looking for an answer, it's much easier than using the gem. Just provide a DATABASE_URLand SHARED_DATABASE_URL(not sure if the second is needed). The database url format is adapter://username:password@hostname:port/database, so for example, you would do:

这是旧的,但如果有人四处寻找答案,它比使用 gem 容易得多。只需提供一个DATABASE_URLSHARED_DATABASE_URL(不确定是否需要第二个)。数据库 url 格式是adapter://username:password@hostname:port/database,例如,您将执行以下操作:

heroku config:add DATABASE_URL=mysql://etok:somepassword@<your-server>:3306/etok
heroku config:add SHARED_DATABASE_URL=mysql://etok:[email protected]:3306/etok

Then re-deploy your app. It will read your DATABASE_URLand generate the database.yml from that. The default port is already 3306 so it's not needed in the url in your case. When you deploy, you may notice that it generates your database.yml:

然后重新部署您的应用程序。它将读取您DATABASE_URL并从中生成 database.yml。默认端口已经是 3306,因此在您的情况下,url 中不需要它。当您部署时,您可能会注意到它生成了您的 database.yml:

-----> Writing config/database.yml to read from DATABASE_URL

Then you're set (as long as your server accepts connections from your heroku host.

然后你就设置好了(只要你的服务器接受来自你的 heroku 主机的连接。

回答by Nat Budin

I've written a gem that may help with this. You can find it at:

我写了一个 gem 可以帮助解决这个问题。您可以在以下位置找到它:

http://github.com/nbudin/heroku_external_db

http://github.com/nbudin/heroku_external_db

回答by Shan Valleru

heroku config:add DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:3306/databasename
heroku config:add SHARED_DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:3306/databasename

Then, do a

然后,做一个

Heroku restart 

that should do.

应该这样做。

Important Note: I suggest you to use database host IP address than using giving the hostname directly, coz, with some shared hosting services like godaddy, the db hostname looks like user.345432.abcd.godaddy.comand it seems like heroku is unable to resolve it properly (personal experience), I resolved the hostname to IP address and using the IP directly worked like a charm ! Also, If your database password has special characters, make sure you escape them correctly (like '\!' for '!' and so on..)

重要提示:我建议您使用数据库主机 IP 地址,而不是直接使用主机名,因为使用一些共享托管服务(例如 Godaddy),db 主机名看起来像user.345432.abcd.godaddy.com,而 heroku 似乎无法使用为了正确解决它(个人经验),我将主机名解析为 IP 地址并直接使用 IP 就像一个魅力!此外,如果您的数据库密码包含特殊字符,请确保正确转义它们(例如 '\!' 代表 '!' 等等......)

回答by d1jhoni1b

Yeah this is very straight forward and simple:

是的,这是非常直接和简单的:

1 - create mysql db 2 - create mysql db user (set defaults) 3.1 - Go to your Heroku panel/Config Vars

1 - 创建 mysql db 2 - 创建 mysql db 用户(设置默认值) 3.1 - 转到您的 Heroku 面板/配置变量

enter image description here

在此处输入图片说明

3.2 - Click on "Reveal Vars" and edit (clicking on pencil icon) on the one you want to change in this case DATABASE_URL(if not present just a new one with DATABASE_URLas the name)

3.2 - 在这种情况下,单击“显示变量”并编辑(单击铅笔图标)要更改的变量DATABASE_URL(如果没有,则只显示一个具有DATABASE_URL名称的新变量)

enter image description here

在此处输入图片说明

3 (#2) - Using command line

3 (#2) - 使用命令行

heroku config:add DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:databaseserverport/databasename

heroku config:add DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:databaseserverport/databasename

then just

那么就

heroku restart

heroku restart

And remember the syntax:

并记住语法:

DATABASE_URL

DATABASE_URL

mysql://user:password@hostnameOrIPAddress:PortNumber/databasename

mysql://user:password@hostnameOrIPAddress:PortNumber/databasename

MySQL DBMS's default port number is : 3306

MySQL DBMS 的默认端口号是: 3306

That's why you see examples mentioned previously using DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:3306/databasename

这就是为什么您会看到前面提到的示例使用 DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:3306/databasename

Hope this helps!!!

希望这可以帮助!!!

回答by John Beynon

have a look at Heroku Amazon RDS addon. I'm not saying use it, but it gives you an insight into what you need to do and how Heroku manages dataabases - basically you need to set a config variable to your mysql instance.

看看Heroku Amazon RDS 插件。我不是说使用它,但它让你深入了解你需要做什么以及 Heroku 如何管理数据库 - 基本上你需要为你的 mysql 实例设置一个配置变量。

回答by mikewilliamson

Heroku ignores your database.yml. You will need to explore the Amazon RDS solution John Beynon suggested or some other similar addon (if there is one). IMO, you will either have to re-evaluate your need to use your MySQL db or find some other hosting. Just in case you didn't already know it, the command:

Heroku 会忽略您的 database.yml。您将需要探索 John Beynon 建议的 Amazon RDS 解决方案或其他一些类似的插件(如果有的话)。IMO,您要么必须重新评估使用 MySQL 数据库的需求,要么找到其他托管服务。以防万一你还不知道,命令:

heroku db:push

will duplicate both the schema AND data of your MySQL development database in heroku's Postgres database. So sticking with MySQL for dev is no problem.

将在 heroku 的 Postgres 数据库中复制 MySQL 开发数据库的模式和数据。因此,坚持使用 MySQL for dev 是没有问题的。

I hope that helps.

我希望这有帮助。