Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull mysql database from heroku to local machine

Hi I have a ruby on rails app hosted on heroku and it is using mysql as database.

Now I have to take backup of the database to my local machine. But I am getting issues while taking backup.

For this I installed taps gem and I am using following commands for it

heroku pg:pull mysql2://[email protected]/heroku_database  local_database --app my_app

but it is giving error as !Your app has no databases.

Can any one guide me how to pull mysql database from heroku to local machine.

EDIT

I have used following syntax for the command

heroku pg:pull <REMOTE_SOURCE_DATABASE> <LOCAL_TARGET_DATABASE>

and for getting REMOTE_SOURCE_DATABASE I have used following command

 heroku config:get DATABASE_URL --app my_app

I refer this link1 and link2 for more detailed heroku documentation.

like image 701
I-am-simple-user Avatar asked Feb 26 '15 07:02

I-am-simple-user


People also ask

How do I access heroku database MySQL?

Get the Hostname of the Database Once installed, type heroku login to login using the browser. Next enter the following command, using your own app name instead of the one in this example. The database URL string will be printed on the command line. The Hostname is the part after the @ sign and before the / .

How do I connect ClearDB heroku to MySQL workbench?

Copy the value of the CLEARDB_DATABASE_URL config variable. $ heroku config:set DATABASE_URL='mysql://adffdadf2341:[email protected]/heroku_db?reconnect=true' Adding config vars: DATABASE_URL => mysql2://adffd...b?reconnect=true Restarting app... done, v61. That's it!


2 Answers

The pg:pull command only works with Postgres databases in your Heroku app. But, you are using a third-party MySQL provider. Your database is hosted on the ClearDB servers and it's available to anyone with the right credentials, including both your app server on Heroku and your dev machine.

Even though there aren't special commands to pull the database, you don't need any - plain mysqldump should do.

mysqldump -h hostname.cleardb.com -u username heroku_database | mysql local_database

like image 67
Leonid Shevtsov Avatar answered Oct 26 '22 23:10

Leonid Shevtsov


Running $heroku config | grep ^DATABASE will give you something like this:

DATABASE_URL: mysql2://username:password@host/dbname?reconnect=true`

From there you can construct your db dump command:

mysqldump -h host -p -u username dbname | mysql local_database

This will prompt you for the password which you received from the previous command. If you wanted to create a script that would automatically include the password from the heroku command you could do something like this:

mysqldump -u username --password=`heroku config | grep ^DATABASE | sed 's/.*[a-z0-9][a-z0-9]*:\([a-z][a-z0-9]*\).*/\1/'` -h host dbname | mysql cedric

In this way you can have a script that will import the database without requiring any user input but also does not expose the password to your database.

like image 27
gabeodess Avatar answered Oct 27 '22 00:10

gabeodess