Remote mysql database on Heroku app - ruby-on-rails

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.

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.

I've written a gem that may help with this. You can find it at:
http://github.com/nbudin/heroku_external_db

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.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 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 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.
I hope that helps.

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.

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 with DATABASE_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!!!

Related

How to change DATABASE_URL for a heroku application

I wanted to use an external Database with my heroku application. But I'm unable to edit the configuration cariables. I tried using GUI, Which says, Cannot overwrite attachment values DATABASE_URL. While I tried using CLI as well. I used the command: heroku config:addDATABASE_URL="postgresql://username:password#IP:PORT". However, this throws an error ... is not a heroku command.
After trying out most these answers, I came across an update in 2016, here:
the database needs to be detached first, then update the variable of the DATABASE_URL.
heroku addons:attach heroku-postgresql -a <app_name> --as HEROKU_DATABASE
heroku addons:detach DATABASE -a <app_name>
heroku config:add DATABASE_URL=
An alternative method which does not require detaching (which may not be a desired outcome of the switch) is to simply attach the new database and then promote it, which the Heroku Documents explicitly states as a way to set the DATABASE_URL.
heroku addons:attach heroku-postgresql -a <app_name>
heroku pg:promote heroku-postgresql -a <app_name>
I got the very same situation today when I need to change postgres to postgis. Detach doesn't work for me so I done this to database.yml:
production:
url: <%= ENV['DATABASE_URL'].sub(/^postgres/, "postgis") %>
https://github.com/rgeo/activerecord-postgis-adapter/issues/214.
SQLAlchemy 1.4.x has removed support for the postgres:// URI scheme, which is used by Heroku Postgres (https://github.com/sqlalchemy/sqlalchemy/issues/6083). To maintain compatibility, perform the following before setting up connections with SQLAlchemy:
import os
import re
uri = os.getenv("DATABASE_URL") # or other relevant config var
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
# rest of connection code using the connection string `uri`
This will allow you to connect to Heroku Postgres services using SQLAlchemy >= 1.4.x
As explained in this article, the correct syntax to set/add a configuration variable is
$ heroku config:set DATABASE_URL="postgresql://username:password#IP:PORT"
However, it looks like (see the comments) the DATABASE_URL has been deprecated and trying to update it will trigger an error.
Based on the Heroku docs this is how you would share a database with multiple apps.
heroku addons:attach my-originating-app::DATABASE --app sushi
Solved it. Just for the reference of the users who have the same issue or want to have a similar implementation. Here's the workaround which worked for me.
Heroku no more overwrites databse.yml, so I just modified the DATBASE_URL in the database.yml and pushed it :)
It worked too!
Source
In my case, I needed to launch an java spring boot application with my personal data base (postgres). I have an instance on AWS, and when loading the app, an error was occurring because it would connect without ssl.
Considering this documentation (https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#using-ssl-with-postgresql), it says:
We used to suggest adding the URL parameter sslmode=disable to JDBC URLs. We now require use of SSL for all new Heroku Postgres databases. We will be enforcing use of SSL on all Heroku Postgres databases from March 2018. Please do not disable SSL for your database or your applications may break.
So, resuming, step 1, I deleted my addon Heroku Postgres on Resources tab.
Step 2, I changed my application.yml
from:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://<url>:<port>/<dataBaseName>?createDatabaseIfNotExist=true&useSSL=false
username: <user>
password: <pass>
to
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://<url>:<port>/<dataBaseName>?createDatabaseIfNotExist=true&useSSL=false&sslmode=disable
username: <user>
password: <pass>
I added "&sslmode=disable" at the end of url string.
And finally, rebuild/deploy (which in my case is automatic after pushing into my repo on github).
I hope this would help someone.
Peace...
One way to edit the DATABASE_URL will be to create another app and add the heroku_postgres add-on there and then grab the url of that database and use that in your main app by configuring the environment variables and set the value of DATABASE_URL to that url of the database.
Now you can easily change the DATABASE_URL as that is not attached with the app.

How to get files .gitignore'd on heroku?

We have inherited a Rails project hosted on Heroku. The problem is that I don't understand how to get a copy of the database.yml or other various config files that have been ignored in the .gitignore file.
Sure I can look through the history but it seems like a hip shot when comparing it to what should be on the production server. Right now we are having to make changes to the staging environment which makes things a bit arduous and less efficient than having a local dev environment so we can really get under the hood. Obviously, having an exact copy of the config is a good place to start.
With other hosts it's easy to get access to all the files on the server using good ol' ftp.
This might be more easily addressed with some sort of git procedure that I am less familiar with.
Heroku stores config variables to the ENV environment
heroku config will display these list of variables
heroku config:get DATABASE_URL --app YOUR_APP
Will return the database url that Heroku as assigned to your app, using this string one can deduce the parameters necessary to connect to your heroku applications database, it follows this format
username:password#database_ip:port/database_name
This should provide you with all the values you'll need for the heroku side database.yml, its a file that is created for you each time you deploy and there is nothing it that can't be gotten from the above URL.
gitignoring the database.yml file is good practice
It's not something you can do - entries added to .gitignore means they've been excluded from source control. They've never made it into Git.
As for database.yml you can just create the file in app\config\database.yml with local settings, it should look something like;
development:
adapter: postgresql
host: 127.0.0.1
username: somelocaluser
database: somelocaldb
It's safe to say though, that if the file is not in Git then it's not on Heroku since that's the only way you can get files to Heroku. Any config is likely to be in environment variables - heroku config will show you that.

How do I check the records of my heroku database?

I've just deployed my application to heroku and pointed my custom domain to the heroku servers. How can I check the records in my heroku database?
You can use heroku run rails console and look at your records with Model.all or any other method.
If you want to backup the database look at heroku PG backups, you then can import your database on your local machine and look at it there. Depending on your db adapter you could use sqlite browser for sqlite3 or phpmyadmin for MySQL.
I found a similar question like this and here is what #Chowlett says:
"You could run heroku pg:psql to fire up a Postgres console, then issue \d to see all tables, and \d tablename to see details for a particular table."
You can also type select * from tablename; to view the table contents.
How to view current database schema for Heroku app in Terminal?
heroku db:pull to pull your production DB locally to take a peek in it.
I'll give the method for connecting via a GUI tool
Run the following command to get the database credentials from Heroku:
heroku pg:credentials DATABASE_URL
Then you can use a GUI tool like PG Commander or PGAdmin to connect to the db
Heroku now has an add-on named PostgreSQL Studio (currently free & in beta) that would let you access your database from within the browser, without having to use CLI, much like PHP MyAdmin.
To attach this add-on to your application,
heroku addons:create pgstudio
Then go to the list of add-ons on Heroku, select PostgreSQL Studio, authorize it, select the database to connect with from the dropdown list of all databases and it will take you to the web-based interface to handle your selected database.
You may refer to this official article on Heroku:
https://devcenter.heroku.com/articles/pgstudio
The easy answer is:
heroku pg:info
You can also download a client side Postgres, like Postico, and using the information provided in that URL to enter password and database name etc, then you can create locally, just like phpMyAdmin.
I use the admin_data gem, works well in Heroku.
You can use heroku dataclips that allows to run queries online. Here you can find documentation https://devcenter.heroku.com/articles/dataclips.
Connect to the database using Sequel Pro. You can find your ClearDB url using heroku config command. The structure for connecting is as follows:
CLEARDB_DATABASE_URL => mysql://[username]:[password]#[host]/[database name]?reconnect=true

2 Rails Apps, 1 Database (using Heroku)

I've made 2 apps, App A and App B. App A's sole purpose is to allow users to sign up and App B's purpose is to take select users from App A email them. Since App A & B were created independently & are hosted in 2 separate Heroku instances, how can App B access the users database in App A? Is there a way to push certain relevant rows from App A to App B?
There is currently no way of sharing databases between Heroku apps.
You might be able to use the Amazon RDS add-on to run a dedicated MySQL instance.
The alternative is going to be creating an API and pushing data between the apps. You can run a background process to push the data in and out.
You can connect several Heroku instances on the same shared PostgreSQL database, given by the shared-database:5mb free add-on on Heroku.
On the instance on which you have the database, type:
$ heroku config
This will show a lot of settings, in which you'll see something like this:
DATABASE_URL => postgres://fbksgiuqlv:BuIvJDfS_eOBDJDZCc9SP#ec2-104-20-247-168.compute-1.amazonaws.com/fbksgiuqlv
This is the database connection string that your instances will be using.
Then on the other instances, overwrite the DATABASE_URL config variable by typing:
$ heroku config:add DATABASE_URL=your_new_connection_string
So tinkered a little around and did as below and it worked
prompt$ heroku console
Ruby console for your-app.heroku.com
>> dbconfig = YAML.load(File.read('config/database.yml'))
=> {"production"=>{"encoding"=>"unicode", "port"=>5432, "adapter"=>"postgresql", "username"=>"xxxxxxxxxxxxxx", "database"=>"xxxxxxxxxxxxx", "host"=>"kklasfdsfjsfdsfsd.fsdf.dsfdsf", "password"=>"xxxxxxxxxxxxxxxxxx"}}
puts dbconfig.to_yaml
---
production:
encoding: unicode
port: 5432
adapter: postgresql
username: xxxxxxxxxxx
database: xxxxxxxxxxxxxxx
host: ec2-50-2323kskdsakd231.amazonaws.com
password: xxxxxxxxxxxxxx
Then copy and paste the yaml to a connection for the other DB
and it works!!! For me!!!
I was looking into this as well and I noticed that it seems like it is now possible to actually do this.
See: http://newsletterer.heroku.com/2011/07 ("did you know" section at the bottom)
Basically you set up one app, retrieve the app database url, and add that url to the config of the other app, like so:
$ heroku config | grep DATABASE_URL --app sushi
DATABASE_URL => postgres://lswlmfdsfos:5FSLVUSLLT123#ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf
Then, set the DATABASE_URL for new apps to this value:
$ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:5FSLVUSLLT123#ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf --app sushi-analytics
Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf
Restarting app... done, v74.
That's it — now both apps will share one database.
I haven't tried this yet, but I am about to give it a shot, as I too was thinking about splitting an existing app into two apps. Let's hope this actually works!
Heroku actually overwrites the database.yml file that you have checked in, you can verify this by using the "heroku shell" command and typing cat config/databse.yml
They make up a bunch of random stuff that is used per application. I don't believe its possible to stop them from doing that.
You might be able to do this if you use schemas with PostgreSQL.

is there a way to edit herokus yml file

I setup a remote connection locally and need to push it to heroku. When I pushed it to heroku I got an error saying:
RemoteDBName is not configured.
I'm just assuming (also searched and saw) heroku uses their own config.yml file.
Figured this out, for anybody connecting to a remote database on heroku that might see this:
Heroku replaces your database.yml file with their own, overwriting anything in yours.
To get around this:
Create a new file in your config folder, name it whatever.yml
Setup the connection string in this file.
Create a new file in your initializers folder, I called mine load_remote.rb. In this file write this line of code:
REMOTE_DB = YAML.load_file("#{RAILS_ROOT}/config/YOURNEWFILEHERE.yml")
Establish your connection in any of the remote models with this line of code:
establish_connection Remote_DB['Whatever you named your connection string in the yml file here']
Let me show you how database configuration is done when you work with Heroku. I think this might be a bit vague in the documentation, some people get confused over it. Lets utilize the console:
zero:~/Projects/crantastic $ heroku console
Ruby console for crantastic.heroku.com
>> puts File.read(Rails.configuration.database_configuration_file)
---
production:
encoding: unicode
adapter: postgresql
username: something_random
port: 5432
host: somewhere.at.heroku
database: something_random
password: something_random
=> nil
>>
Heroku in practice replaces your apps database.yml when you push your site to their servers. Your data will be stored in one of their fancy PostgreSQL servers no matter what you use locally - this means that you don't have to think about database.yml at all (except for development purpses, naturally). Taps makes sure that everything's db agnostic. If you want to push your latest development db to Heroku, simply run heroku db:push

Resources