Mysql.py

From Wikitech

mysql.py is a wrapper around the mysql command line client from mariadb, which is tuned for WMF production admin convenience. It is intended primarily for interactive usage, so it defaults to WMF configuration and saves typing. It requires sudo or root user privileges, to read /root's config. It is backwards compatible with the mysql command line client, but has some additional configuration changes and shortcuts:

  • It will try to force the use of TLS for any connections outside of localhost (for security), using the right CA (independently of the /etc/ configuration.
  • It will use current user's .my.cnf configuration, so typically you want to run it with sudo meaning it will try to connect as root and with the appropriate root password, depending on the role (mediawiki db vs wikireplicas, etc.)
  • It will try to complete the fully qualified name of the host, with just the hostname: So mysql.py -h db1215 will be interpreted as if one had written mysql -h db1215.eqiad.wmnet
  • It will make the port easier to write, so if one writes mysql.py -h db1215:3315 it will be as if one writes mysql -h db1215.eqiad.wmnet -P 3315
  • It will translate sections names into ports, based on the /etc/wmfmariadb/section_ports.csv file, generated from puppet's hiera: So if one writes mysql.py -h db1215:s5 it will be as if one writes mysql -h db1215.eqiad.wmnet -P 3315

mysql.py is available on cumin hosts (mysql management profile) as it requires private account information access to execute database server admin account queries: cumin1002.eqiad.wmnet, cumin2002.codfw.wmnet.

Example usage

Connect quickly to a server:

mysql.py -h db1215

Connect quickly to a multinstance server:

mysql.py -h db1215:3315

Alternative syntax:

mysql.py -h db1215:s5

All other mysql parameters can still be used, they are just passed on:

mysql.py -A -h labsdb1009 enwiki_p

Faster way to query 1 time things:

mysql.py -h db1215 -u dump -e "SELECT @@read_only" -p

Combined with lists of host:section or host:port, it can do batch requests:

./section s5 | while read host; do mysql.py -h $host -e "SELECT @@hostname, @@port"; done

Development

Its code can be found on the WMFMariaDBpy repo.