0

I've been struggling with the following problem: I have a MySQL database running on a remote web host. I connect to the MySQL database in my Django app (I use it as the main database). The Django app is running on a Heroku server but I get different data results compared to running it locally.

Am I missing something, or are changes done on Heroku not committed to the database?

MySQL settings:

DATABASES = {
 #   'default': {
  #      'ENGINE': 'django.db.backends.sqlite3',
   #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
   # }
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'xxx',
        'USER': 'xxx',
        'PASSWORD': 'xxx',
        'HOST': 'xxx',
        'PORT': 'xxx',
    }
}
CPUFry
  • 566
  • 4
  • 18
  • Please show your settings. Are you sure the app on Heroku is connecting to the remote MySQL db? – Daniel Roseman Apr 21 '17 at 14:19
  • Updated the question with the MySQL settings. Yes I am sure, though the MySQL database is configured in the Django app, not in Heroku (if possible/necessary). – CPUFry Apr 21 '17 at 14:25
  • And there's nothing else in that settings file that could be overriding that DATABASES setting? – Daniel Roseman Apr 21 '17 at 14:26
  • No, it is the only DATABASES setting in the file. Is it required to host your database on Heroku or should this work (if set up correctly)? – CPUFry Apr 21 '17 at 14:29
  • 1
    Assuming your MySQL db is set up to accept connections from the internet, this should work. But if it didn't, your site wouldn't work at all; you wouldn't just get different data. Which is why the only explanation I can think of is that you are somehow overriding your settings somehow. Sorry to keep insisting, but for example are you sure there's no call to `dj_database_url`? – Daniel Roseman Apr 21 '17 at 14:31
  • I scanned the entire file. No possibility of overriding the database setting. Although, as this approach should work the error may be in my remaining code. Thank you for your help! – CPUFry Apr 21 '17 at 14:41

2 Answers2

1

I just see that if you delete the ad-on provided by heroku, it will use the DB you put on the settings of your project

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 10 '22 at 07:46
1

I believe Juan Carlos Hernández is correct. To expand on his answer, Heroku uses it's own database instance, unless you tell it otherwise by pointing the DATABASE_URL to the one you would like to use. Please note that Heroku will prevent you from overwriting the DATABASE_URL in use since that will destroy the existing database. Although tagged as ruby-on-rails, the answers I found here seem relevant.

To summarize, you just have to run

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

heroku restart

Or you can change these variables in the Heroku panel.

raphael
  • 2,469
  • 2
  • 7
  • 19