Heroku 应用程序上的远程 mysql 数据库
我可以使用个人 Web 服务器上的 mysql 数据库而不是 heroku 的数据库吗?
我这样配置我的生产数据库:
production:
adapter: mysql2
database: somedatabase
username: someusername
password: somepassword
host: 1.1.1.1:1234
但是,这不起作用,我的应用程序仍然使用heroku的共享数据库。
Can I use mysql database from my personal web server instead of heroku's database?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这已经很旧了,但如果有人四处寻找答案,它比使用 gem 容易得多。只需提供
DATABASE_URL
和SHARED_DATABASE_URL
(不确定是否需要第二个)。数据库 URL 格式为adapter://username:password@hostname:port/database
,因此,例如,您可以执行以下操作:然后重新部署您的应用程序。它将读取您的
DATABASE_URL
并从中生成database.yml。默认端口已经是 3306,因此您的情况下的 url 中不需要它。部署时,您可能会注意到它会生成您的database.yml:然后您就设置好了(只要您的服务器接受来自heroku主机的连接。
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_URL
andSHARED_DATABASE_URL
(not sure if the second is needed). The database url format isadapter://username:password@hostname:port/database
, so for example, you would do:Then re-deploy your app. It will read your
DATABASE_URL
and 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:Then you're set (as long as your server accepts connections from your heroku host.
我写了一篇宝石,可能会对此有所帮助。您可以在以下位置找到它:
http://github.com/nbudin/heroku_external_db
I've written a gem that may help with this. You can find it at:
http://github.com/nbudin/heroku_external_db
然后,做一件
该做的事。
重要提示:我建议您使用数据库主机 IP 地址,而不是直接提供主机名,因为对于某些共享托管服务(例如 godaddy),数据库主机名看起来像 user.345432.abcd.godaddy.com< /em> 看来heroku无法正确解析它(个人经验),我将主机名解析为IP地址并直接使用IP就像一个魅力!另外,如果您的数据库密码包含特殊字符,请确保正确转义它们(例如“\!”代表“!”等等。)
Then, do a
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.com and 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..)
Heroku 会忽略您的database.yml。您将需要探索 John Beynon 建议的 Amazon RDS 解决方案或其他一些类似的插件(如果有)。 IMO,您要么必须重新评估使用 MySQL 数据库的需求,要么寻找其他托管服务。
以防万一您还不知道,该命令:
将在 Heroku 的 Postgres 数据库中复制 MySQL 开发数据库的架构和数据。因此,坚持使用 MySQL 进行开发是没有问题的。
我希望这有帮助。
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:
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.
I hope that helps.
查看 Heroku Amazon RDS 插件。我并不是说使用它,但它可以让你深入了解你需要做什么以及 Heroku 如何管理数据库 - 基本上你需要为你的 mysql 实例设置一个配置变量。
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.
是的,这非常直接和简单:
1 - 创建 mysql 数据库
2 - 创建 mysql 数据库用户(设置默认值)
3.1 - 转到 Heroku 面板/配置变量
3.2 - 单击“显示变量”并编辑(单击铅笔图标)本例中您要更改的变量
DATABASE_URL (如果不存在,则仅提供一个以
DATABASE_URL
作为名称的新代码)< img src="https://i.sstatic.net/uKjrZ.png" alt="在此处输入图像描述">
3 (#2) - 使用命令行
heroku config:add DATABASE_URL=mysql ://dbusername:dbpassword@databasehostIP:databaseserverport/databasename
然后只需
heroku restart
并记住语法:
DATABASE_URL
mysql://user:password @hostnameOrIPAddress:PortNumber/databasename
MySQL DBMS 的默认端口号是:
3306
这就是为什么您会看到前面提到的使用
DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:3306/ 的示例数据库名称
希望这有帮助!
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
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 withDATABASE_URL
as the name)3 (#2) - Using command line
heroku config:add DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:databaseserverport/databasename
then just
heroku restart
And remember the syntax:
DATABASE_URL
mysql://user:password@hostnameOrIPAddress:PortNumber/databasename
MySQL DBMS's default port number is :
3306
That's why you see examples mentioned previously using
DATABASE_URL=mysql://dbusername:dbpassword@databasehostIP:3306/databasename
Hope this helps!!!