Search code examples
mysqlmacosmysql-workbench

MySQL Workbench: Connecting to database automatically on the startup


Currently, I have to:

  1. Launch MySQLWorkBench 8.0
  2. Click on the DB that I need to connect to enter image description here

I'd like to:

  1. Launch MySQLWorkBench 8.0
  2. MySQLWorkBench automatically connects to this DB without me clicking on it

I need to re-open MySQLWorkbench during my day a lot an hence would like to eliminate this step.


Solution

  • My first thought was: if you need it so often, don't close it.

    But it's easy to accomplish what you want if you are willing to run MySQL Workbench from a script. For instance open /Application in a terminal and run this command (on macOS):

    open -a MySQLWorkbench.app --args --query "Localhost 8.0"
    

    where "Localhost 8.0" must be changed to a connection name you want to open. This will open the SQL IDE for that server. You can put the call into a shell script and run that instead of the application itself.

    Instead of --query use --help to print a list of supported commands:

    MySQLWorkbench [<options>] [<name of a model file or sql script>]
    Options:
        --admin <instance>            Open a administration tab to the named instance
        --configdir <path>            Specify configuration directory location, default is platform specific.
        -h, --help                    Show help options   
        --log-level <level>           Valid levels are: error, warning, info, debug1, debug2, debug3
        --log-to-stderr               Also log to stderr  
        --migration                   Open a migration wizard tab
        --model <model file>          Open the given EER model file
        --open                        Open the given file at startup (deprecated, use script, model etc.)
        --query <connection>|<connection string>Open a query tab and ask for connection if nothing is specified.
    If named connection is specified it will be opened,
    else connection will be created based on the given connection string,
        --quit-when-done              Quit Workbench when the script is done
        --run <code>                  Execute the given Python code
        --run-python <code>           Execute Python code from a file
        --run-script <file>           Execute Python code from a file
        --script <sql file>           Open the given SQL file in an connection, best in conjunction with a query parameter
        --upgrade-mysql-dbs           Open a migration wizard tab
        -v, --verbose                 Enable diagnostics output
        --version                     Show Workbench version number and exit
    

    However, this doesn't show any output when using the open command (due to the way it works, see also How to get the output of an os x application on the console, or to a file?), but you can run the app binary directly (instead of the app bundle as shown above), like this:

    ./MySQLWorkbench.app/Contents/MacOS/MySQLWorkbench --help