How to Use Your Own MySQL Database Server with Heroku

How to use your own mysql database server with heroku?

According to the Heroku documentation, changing DATABASE_URL is the correct way to go.

If you would like to have your rails application connect to a non-Heroku provided database, you can take advantage of this same mechanism. Simply set your DATABASE_URL config var to point to any cloud-accessible database, and Heroku will automatically create your database.yml file to point to your chosen server. The Amazon RDS Add-on does this for you automatically, though you can also use this same method to connect to non-RDS databases as well.

Here's an example that should work:

heroku config:add DATABASE_URL=mysql://user:password@host/db

You may need to redeploy by making a change and running git push heroku master

How to deploy local MySQL database to Heroku

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

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

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

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

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

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 is very good too!

Migrating MySQL database from local server to Heroku

You have at least three options:

  • There are other Heroku addons that provide MySQL databases. I'm not sure if any of them let you get started without a credit card.
  • You could use a MySQL provider that's not explicitly supported by Heroku and point your application to it manually.
  • You could update your application to use PostgreSQL instead of MySQL and use Heroku's own Postgres service.

How to make connection to heroku mysql database

I found the answer on server fault here

The correct format for the database connection is:

$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
$server = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$db = substr($url["path"], 1);
$connection = new mysqli($server, $username, $password, $db);

Remote mysql database on Heroku app

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 and 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:

heroku config:add DATABASE_URL=mysql://etok:somepassword@<your-server>:3306/etok
heroku config:add SHARED_DATABASE_URL=mysql://etok:somepassword@79.101.41.213:3306/etok

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:

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

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



Related Topics



Leave a reply



Submit