Skip to content

Instantly share code, notes, and snippets.

@larsyencken
Created September 7, 2022 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsyencken/eb505fbc44d16ca09f67c6af1402874f to your computer and use it in GitHub Desktop.
Save larsyencken/eb505fbc44d16ca09f67c6af1402874f to your computer and use it in GitHub Desktop.
MySQL: use the ~/.my.cnf file to store multiple connection settings and use this tool to switch between them
#!/usr/bin/env python
#
# mysql-profile
#
# Switch between mysql profiles.
#
from pathlib import Path
import configparser
from dataclasses import dataclass
import click
MYSQL_CONFIG = Path.home() / '.my.cnf'
@click.group()
def cli():
pass
@cli.command()
def list():
config = _parse_config()
profiles = []
for section in config.sections():
if section.startswith('client-'):
name = section.split('-', 1)[1]
profiles.append(name)
for profile in sorted(profiles):
print(profile)
@cli.command()
@click.argument('profile')
def use(profile):
config = _parse_config()
name = f'client-{profile}'
config['client'] = config[name]
with open(MYSQL_CONFIG, 'w') as f:
config.write(f)
def _parse_config() -> configparser.ConfigParser:
config = configparser.ConfigParser()
config.read(MYSQL_CONFIG)
return config
if __name__ == '__main__':
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment