├── README.md ├── dmysql ├── dmysql-create-database ├── dmysql-import-database └── dmysql-server /README.md: -------------------------------------------------------------------------------- 1 | A series of useful commands written in python to interact with a MySQL Server running on Docker 2 | 3 | # Motivation 4 | Docker is awesome, but when using MySQL on a Dockerized environment simple tasks can become commands with lots of options and arguments. 5 | 6 | __Note:__ The following commands will use the official MySQL docker image found at https://registry.hub.docker.com/_/mysql/ 7 | 8 | # Commands provided 9 | 10 | dmysql-server (Start a new MySQL Server) 11 | dmysql (Open the mysql cli) 12 | dmysql-create-database (Creates a new database) 13 | dmysql-import-database (Imports a database) 14 | 15 | # Install 16 | Clone or download this project then run: 17 | 18 | $ cd directory_with_scripts 19 | $ sudo chmod +x dmysql* 20 | $ sudo cp dmysql* /usr/local/bin 21 | 22 | # License 23 | Released under the MIT License -------------------------------------------------------------------------------- /dmysql: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from subprocess import call 5 | import argparse 6 | 7 | def runContainer(containerName): 8 | ''' Runs the mysql client using the containerName provided 9 | ''' 10 | 11 | call("docker run -it --link="+containerName+":mysql --rm mysql sh -c 'exec mysql -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uroot -p'", shell=True) 12 | return 13 | 14 | def main(): 15 | ''' Creates the parser and executes the function runContainer 16 | ''' 17 | 18 | parser = argparse.ArgumentParser( 19 | description="Run mysql client on a docker container", 20 | prog="dmysql") 21 | 22 | parser.add_argument("container", help="The mysql container to connect to") 23 | 24 | args = parser.parse_args() 25 | 26 | runContainer(args.container) 27 | return 28 | 29 | # Execute the main function 30 | main() -------------------------------------------------------------------------------- /dmysql-create-database: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from subprocess import call 5 | import argparse 6 | 7 | def runContainer(containerName, databaseName): 8 | ''' Runs the command to create the database given using the container given 9 | ''' 10 | call("docker run -it --link="+containerName+":mysql --rm mysql sh -c 'exec mysqladmin -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uroot -p create "+ databaseName +" '", shell=True) 11 | return 12 | 13 | def main(): 14 | ''' Parse the options from the command line and run the function runContainer 15 | ''' 16 | parser = argparse.ArgumentParser( 17 | description="Import a .sql file into a database", 18 | prog="dmysql-import-database") 19 | 20 | # Create the options 21 | parser.add_argument("container", help="The mysql container to use") 22 | parser.add_argument("database", help="Database to create") 23 | 24 | args = parser.parse_args() 25 | 26 | runContainer(containerName=args.container, databaseName=args.database) 27 | 28 | return 29 | 30 | # Execute the main function 31 | main() -------------------------------------------------------------------------------- /dmysql-import-database: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | from subprocess import call 5 | import os 6 | import argparse 7 | 8 | def runContainer(containerName, path, database = ""): 9 | ''' Runs the container calling docker run and passing 10 | the necessary arguments. The database is optional since 11 | we can import an .sql file without creating first a database or 12 | specifying one. 13 | ''' 14 | 15 | # Split the path given and create a list out of it 16 | pathList = path.split("/") 17 | 18 | # Take the last element of the list because this is the file 19 | fileName = pathList[-1] 20 | 21 | # Read the path and get the directory 22 | dirname = os.path.dirname(path) 23 | 24 | if database: 25 | print 'Importing {file} into database {database} using container {container}'.format(file=fileName, database=database, container=containerName) 26 | call("docker run -it --link="+containerName+":mysql -v "+ dirname +":/tmp/import --rm mysql sh -c 'exec mysql -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uroot -p "+ database +" < /tmp/import/" + fileName + "'", shell=True) 27 | else: 28 | print 'Importing {file} into container {container}'.format(file=fileName, container=containerName) 29 | call("docker run -it --link="+containerName+":mysql -v "+ dirname +":/tmp/import --rm mysql sh -c 'exec mysql -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uroot -p < /tmp/import/" + fileName + "'", shell=True) 30 | return 31 | 32 | def main(): 33 | ''' Interpret the command line arguments 34 | and pass the options to runContainer 35 | ''' 36 | 37 | parser = argparse.ArgumentParser( 38 | description="Import a .sql file into a database", 39 | prog="dmysql-import-database") 40 | 41 | # Create the options 42 | parser.add_argument("container", help="The mysql container to use") 43 | parser.add_argument("filepath", help="File to the sql file you want to import") 44 | parser.add_argument("--database", help="Database to import the file into") 45 | 46 | args = parser.parse_args() 47 | 48 | if args.database: 49 | runContainer(args.container, args.filepath, args.database) 50 | else: 51 | runContainer(args.container, args.filepath) 52 | return 53 | 54 | # Execute main 55 | main() -------------------------------------------------------------------------------- /dmysql-server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | from subprocess import call 5 | import argparse 6 | 7 | def createVolume(containerVolumeName): 8 | ''' Create a data only container 9 | ''' 10 | print 'Creating the container using the volumes-from strategy. Using container {container} as the container name'.format(container=containerVolumeName) 11 | call("docker run -v /var/lib/mysql --name "+ containerVolumeName+" busybox true", shell=True) 12 | return 13 | 14 | def runContainer(containerName, rootPassword, hasVolume=False): 15 | ''' Creates the container 16 | ''' 17 | 18 | if hasVolume: 19 | # Create the container name variable 20 | containerVolumeName = containerName.upper() + '_DATA' 21 | 22 | # Create data container 23 | createVolume(containerVolumeName) 24 | 25 | call('docker run --volumes-from ' + containerVolumeName + ' --name ' + containerName + ' -e MYSQL_ROOT_PASSWORD="' + rootPassword + '" -d mysql', shell=True) 26 | else: 27 | call('docker run --name ' + containerName + ' -e MYSQL_ROOT_PASSWORD="' + rootPassword + '" -d mysql', shell=True) 28 | return 29 | 30 | def main(): 31 | 32 | parser = argparse.ArgumentParser( 33 | description="Creates a new MySQL container using the mysql image", 34 | prog="dmysql-server") 35 | 36 | parser.add_argument("containerName", help="The name of the mysql container to create") 37 | parser.add_argument("rootPassword", help="The root password to define when creating the container") 38 | parser.add_argument("--with-volume", help="Creates a data-only container", action='store_true') 39 | 40 | args = parser.parse_args() 41 | 42 | # If there's a volume 43 | if args.with_volume: 44 | runContainer(containerName=args.containerName, rootPassword=args.rootPassword, hasVolume=True) 45 | else: 46 | runContainer(containerName=args.containerName, rootPassword=args.rootPassword) 47 | 48 | return 49 | 50 | # Execute main 51 | main() --------------------------------------------------------------------------------