0

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.

feedthemachine
  • 592
  • 2
  • 11
  • 29

1 Answers1

4

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 
Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • Thanks a lot for solution! As i see command work only first time, if Workbench already running args will be ignored. Except when i run app as new instance. Is there any way to launch my query for current instance? – Denis Ostrovsky Jun 04 '20 at 09:37
  • MySQL workbench parses command line params only on startup, thus you cannot use a running instance with new parameters. – Mike Lischke Jun 05 '20 at 06:54
  • Hey @MikeLischke. I run the following command, and it opens the app and connects to the database: ```"C:\..\MySQLWorkbench.exe" --query testdb``` However when I add ```--script C:\..\23.10.20.sql``` or ```--open C:\..\23.10.20.sql``` they have no effect. I only get the app connected to the database. I combine them like: ```"C:\..\MySQLWorkbench.exe" --query testdb --script C:\..\23.10.20.sql``` Is that the right way to combine them? I asked this question here but no one answered: https://stackoverflow.com/questions/64595764/how-do-i-run-2-options-of-mysql-workbench-together – Cagri Nov 25 '20 at 15:01