Refactor code that makes calls to `mysql` and friends

#562 added logic for supporting custom ports for `mysql` calls.
#684 added logic for supporting custom ports for `mysqldump` calls.

The logic is the same in both cases: convert `DB_*` constants to CLI parameters.

We can avoid that duplication. Also:

* [x] pass associative arrays, instead of strings
* [x] use $env parameter of `proc_open()`, instead of putenv()