How to run queries on live data
You can run queries on live databases in various ways.
Public replicas
For most use cases you can query the public replicas (known as Wiki Replicas) which host a complete and near real-time mirror of all MediaWiki databases in their original MySQL form. Replication lag is usually less than one second from production. You can write arbitrary select queries in SQL (browse tables and schemas at mw:Database layout).
One notable difference from production hosts is that public replicas are redacted to hide sensitive columns and private rows, such as user email addresses and deleted revisions.
The Quarry web interface is the easiest way to access public replicas. You can login with any wiki account, no privileges required. You can freely run long-running queries here as well.
If you join Toolforge (create a developer account, request Toolforge membership, and setup SSH) you can also do this via the command-line:
- ssh to
login.toolforge.org - run
sql <DBNAME>, for examplesql enwiki - write your query!
Learn more at Help:Toolforge/Database.
For other ways to access public replicas (including Jupyter notebooks and Superset), refer to Help:Wiki Replicas.
Production replicas
If public replicas or XML dumps are not suitable for your use case, you can query production replicas. These require production access which is granted to SREs, MediaWiki deployers, and trusted volunteers who have signed an NDA and acknowedged Server Access Responsibilities.
The below steps will connect you to a live and unredacted MediaWiki database replicas. The command selects a random replica from a MariaDB production cluster. This replica will be under load from real users on MediaWiki web servers.
For long-running queries on unredacted data, use the Analytics replicas instead. Slow or long-running queries should not be run in production and may be stopped a database administrator.
Follow these steps:
- ssh to a deployment server
- run
sql <DBNAME>, for examplesql enwiki. - write your query
Querying a specific production host
If you need to debug a specific database host in production, follow the following steps:
- ssh to a deployment server
- Connect to specific replica by specifying its hostname with the syntax
sql <DBNAME> --host <HOSTNAME>, for examplesql testwiki --host db1001 - run your query
To check the current status of a database, you can click on its server name in the dbtree chart or run the query show engine innodb status\G.
Production master database
Queries here are extremely risky in the potential damage or corruption they may cause, and may bog down performance of the live websites.
- Review your queries carefully. Write queries should exclusively match by a primary key and not with potentially slow or unindexed conditions.
- Verify "WHERE" conditions by running them as a select query on a replica first and confirm that they return the intended rows.
- Inform other people by sending a message to the
#wikimedia-operationschannel on IRC (Meta-Wiki guide, Office Wiki guide).- Write a one-line summary that starts with the
!logprefix and includes any Phabricator task references, then send it to the same#wikimedia-operationschannel. For example:
This prefix instructs a bot to record your action in the Server Admin Log. This enables people to discover and review what we do, and to find it in the far future when we may be wondering how or why something changed. Think of this as the equivalent to Special:Log for on-wiki administrative actions.!log Fix a corrupt testwiki revision per T00000, `UPDATE content_example SET example_address="es:DB://cluster1/example" WHERE example_id=1234 LIMIT 1`
- Write a one-line summary that starts with the
- One row. If planning to edit multiple rows, consider first updating one row only with
LIMIT 1and fully verify in your browser that it has the intended on-wiki effect. - If you absolutely must run a query on a master database, follow these instructions:
- ssh to a deployment server
- Connect to the master database server with the syntax
sql --write <DBNAME>, for examplesql --write testwiki - run your query
Analytics replicas
follow the instructions at SRE/Production access#Setting up your access to set up your SSH, then type:
- ssh to a stat machine, e.g.
ssh stat1011.eqiad.wmnet - pick a cluster and port number from the analytics replica list and connect, e.g.
mysql -h s3-analytics-replica.eqiad.wmnet -P 3313 -A - select the right database, e.g.
USE testwiki; - run your query