├── mysqljson ├── __init__.py └── __main__.py ├── LICENSE ├── .gitignore ├── README.md └── setup.py /mysqljson/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2018, Seth Black 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | env/ 14 | build/ 15 | develop-eggs/ 16 | dist/ 17 | downloads/ 18 | eggs/ 19 | .eggs/ 20 | lib/ 21 | lib64/ 22 | parts/ 23 | sdist/ 24 | var/ 25 | wheels/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | .hypothesis/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # dotenv 85 | .env 86 | 87 | # virtualenv 88 | .venv 89 | venv/ 90 | ENV/ 91 | 92 | # Spyder project settings 93 | .spyderproject 94 | .spyproject 95 | 96 | # Rope project settings 97 | .ropeproject 98 | 99 | # mkdocs documentation 100 | /site 101 | 102 | # mypy 103 | .mypy_cache/ 104 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mysql-to-json 2 | 3 | Connects to a MySQL database and exports selected data to JSON. 4 | 5 | ## Installation 6 | 7 | ``` 8 | $> pip3 install mysql-to-json 9 | ``` 10 | 11 | ## Usage 12 | ``` 13 | mysql-to-json [-h] [-d DATABASE] [-H HOSTNAME] [-P PORT] [-u USER] [-p] 14 | [-e QUERY] 15 | 16 | optional arguments: 17 | -h, --help show this help message and exit 18 | -d DATABASE, --database DATABASE 19 | MySQL database name. 20 | -H HOSTNAME, --hostname HOSTNAME 21 | MySQL host name. 22 | -P PORT, --port PORT MySQL port number. 23 | -u USER, --user USER MySQL username. 24 | -p, --password Shh! It's a secret. 25 | -e QUERY, --query QUERY 26 | Query to run. 27 | ``` 28 | 29 | ## Examples 30 | 31 | All examples simple select all table information from `information_schema` and save it to `tables.json` 32 | 33 | ### Simple 34 | 35 | This assumes we have full access to the mysql database from localhost. 36 | 37 | ``` 38 | $> mysql-to-json -e 'SELECT * FROM information_schema.tables' > tables.json 39 | ``` 40 | 41 | ### Medium Complexity 42 | 43 | This explicitly sets a user and asks for a password, while still connecting to localhost. 44 | 45 | ``` 46 | $> mysql-to-json -d mysql -u seth -p -e 'SELECT * FROM information_schema.tables' > tables.json 47 | ``` 48 | 49 | ### All The Things! 50 | 51 | This explicitly sets every command line option available. 52 | 53 | ``` 54 | $> mysql-to-json -h mydbserver.myhost.com -P 3306 -d mysql -u seth -p -e 'SELECT * FROM information_schema.tables' > tables.json 55 | ``` 56 | 57 | ### All The Things without prompt access 58 | 59 | This explicitly sets every command line option available and uses password stored in variable `$MYSQL_PASSWORD`. 60 | 61 | ``` 62 | $> echo $MYSQL_PASSWORD | mysql-to-json -h mydbserver.myhost.com -P 3306 -d mysql -u seth -p -e 'SELECT * FROM information_schema.tables' > tables.json 63 | ``` 64 | -------------------------------------------------------------------------------- /mysqljson/__main__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | mysql-to-json 5 | Connects to a MySQL database and exports selected data to JSON. 6 | copyright 2018 Seth Black 7 | """ 8 | 9 | import argparse 10 | import getpass 11 | import json 12 | import MySQLdb 13 | import sys 14 | 15 | def cursor_to_dict(cursor): 16 | data = cursor.fetchone() 17 | 18 | if data is None: 19 | return None 20 | 21 | desc = cursor.description 22 | 23 | result = {} 24 | 25 | for (name, value) in zip(desc, data): 26 | result[name[0]] = value 27 | 28 | return result 29 | 30 | def main(): 31 | arg_parser = argparse.ArgumentParser() 32 | 33 | arg_parser.add_argument('-d', '--database', help='MySQL database name.', default='mysql') 34 | arg_parser.add_argument('-H', '--hostname', help='MySQL host name.', default='127.0.0.1') 35 | arg_parser.add_argument('-P', '--port', help='MySQL port number.', default=3306, type=int) 36 | arg_parser.add_argument('-u', '--user', help='MySQL username.', default='root') 37 | arg_parser.add_argument('-p', '--password', help='Shh! It\'s a secret.', action='store_true') 38 | arg_parser.add_argument('-e', '--query', help='Query to run.', required=True) 39 | 40 | args = arg_parser.parse_args() 41 | 42 | password = '' 43 | 44 | if args.password == True: 45 | password = getpass.getpass() 46 | 47 | conn = MySQLdb.connect(host=args.hostname, user=args.user, passwd=password, db=args.database, port=args.port) 48 | 49 | try: 50 | cursor = conn.cursor() 51 | 52 | cursor.execute(args.query) 53 | except MySQLdb.Error as e: 54 | sys.stderr.write('MySQL Error [{}]: {}\n'.format((e.args[0], e.args[1]))) 55 | sys.exit() 56 | 57 | sys.stdout.write('[') 58 | 59 | row = cursor_to_dict(cursor) 60 | 61 | first_line = True 62 | 63 | while row is not None: 64 | if first_line == True: 65 | first_line = False 66 | else: 67 | sys.stdout.write(',') 68 | 69 | json_str = json.dumps(row, default=str) 70 | 71 | sys.stdout.write(json_str) 72 | 73 | row = cursor_to_dict(cursor) 74 | 75 | sys.stdout.write(']') 76 | 77 | if __name__ == "__main__": 78 | main() 79 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from setuptools import setup 4 | from distutils.command.install import install as _install 5 | 6 | setup( 7 | name = 'mysql-to-json', 8 | version = '1.0.0', 9 | description = 'Connects to a MySQL database and exports selected data to JSON.', 10 | author = 'Seth Black', 11 | author_email = 'sblack@sethserver.com', 12 | url = 'https://github.com/sethblack/mysql-to-json', 13 | packages = ['mysqljson'], 14 | keywords = ['mysql','json','database','db','export','export tool','export utility'], 15 | install_requires = [ 16 | 'mysqlclient' 17 | ], 18 | entry_points = { 19 | 'console_scripts' : [ 20 | 'mysql-to-json = mysqljson.__main__:main' 21 | ] 22 | }, 23 | classifiers = [ 24 | "Topic :: Database", 25 | "Programming Language :: Python", 26 | "Programming Language :: Python :: 3", 27 | "Programming Language :: Python :: 3 :: Only", 28 | "Development Status :: 5 - Production/Stable", 29 | "Environment :: Console", 30 | "Intended Audience :: Developers", 31 | "License :: OSI Approved :: BSD License", 32 | "Operating System :: OS Independent", 33 | ], 34 | long_description = """\ 35 | Connects to a MySQL database and exports selected data to JSON. 36 | 37 | ## Usage 38 | 39 | mysql-to-json [-h] [-d DATABASE] [-H HOSTNAME] [-P PORT] [-u USER] [-p] 40 | [-e QUERY] 41 | 42 | optional arguments: 43 | -h, --help show this help message and exit 44 | -d DATABASE, --database DATABASE 45 | MySQL database name. 46 | -H HOSTNAME, --hostname HOSTNAME 47 | MySQL host name. 48 | -P PORT, --port PORT MySQL port number. 49 | -u USER, --user USER MySQL username. 50 | -p, --password Shh! It's a secret. 51 | -e QUERY, --query QUERY 52 | Query to run. 53 | 54 | ## Examples 55 | 56 | All examples simple select all table information from `information_schema` and save it to `tables.json` 57 | 58 | ### Simple 59 | 60 | This assumes we have full access to the mysql database from localhost. 61 | 62 | $> mysql-to-json -e 'SELECT * FROM information_schema.tables' > tables.json 63 | 64 | ### Medium Complexity 65 | 66 | This explicitly sets a user and asks for a password, while still connecting to localhost. 67 | 68 | $> mysql-to-json -d mysql -u seth -p -e 'SELECT * FROM information_schema.tables' > tables.json 69 | 70 | ### All The Things! 71 | 72 | This explicitly sets every command line option available. 73 | 74 | $> mysql-to-json -h mydbserver.myhost.com -P 3306 -d mysql -u seth -p -e 'SELECT * FROM information_schema.tables' > tables.json 75 | """ 76 | ) 77 | --------------------------------------------------------------------------------