Skip to content

Instantly share code, notes, and snippets.

@rehmatworks
Created January 6, 2021 13:39
Show Gist options
  • Save rehmatworks/eeaf8fa002eef8228776a0d4e84d6c1a to your computer and use it in GitHub Desktop.
Save rehmatworks/eeaf8fa002eef8228776a0d4e84d6c1a to your computer and use it in GitHub Desktop.
"""
A very tiny Python script to dump all databases for a user as .sql files.
"""
DB_USER=''
DB_PASS=''
import os
import mysql.connector
conn = mysql.connector.connect(user=DB_USER, password=DB_PASS)
cursor = conn.cursor()
dbs = []
cursor.execute('show databases')
for databases in cursor:
for database in databases:
try:
dbname = str(database)
print('Dumping database {} as {}.sql'.format(dbname, dbname))
os.system('mysqldump {} > {}.sql'.format(dbname, dbname))
except:
print('Database dumping failed!!')
@rehmatworks
Copy link
Author

Usage:

  1. Edit mysql-export.py and set the DB username and password.
  2. Install MySQL connector module pip install mysql-connector
  3. Run the script in a directory where you want to export the SQL files (python mysql-export.py)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment