MediaWiki  master
mysql.php
Go to the documentation of this file.
1 <?php
28 use Wikimedia\IPUtils;
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( "Execute the MySQL client binary. " .
39  "Non-option arguments will be passed through to mysql." );
40  $this->addOption( 'write', 'Connect to the primary database', false, false );
41  $this->addOption( 'group', 'Specify query group', false, true );
42  $this->addOption( 'host', 'Connect to a specific MySQL server', false, true );
43  $this->addOption( 'list-hosts', 'List the available DB hosts', false, false );
44  $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
45  $this->addOption( 'wikidb',
46  'The database wiki ID to use if not the current one', false, true );
47 
48  // Fake argument for help message
49  $this->addArg( '-- mysql_option ...', 'Options to pass to mysql', false );
50  }
51 
52  public function execute() {
53  $dbName = $this->getOption( 'wikidb', false );
54  $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
55  if ( $this->hasOption( 'cluster' ) ) {
56  try {
57  $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
58  } catch ( InvalidArgumentException $e ) {
59  $this->fatalError( 'Error: invalid cluster' );
60  }
61  } else {
62  $lb = $lbf->getMainLB( $dbName );
63  }
64  if ( $this->hasOption( 'list-hosts' ) ) {
65  $serverCount = $lb->getServerCount();
66  for ( $index = 0; $index < $serverCount; ++$index ) {
67  echo $lb->getServerName( $index ) . "\n";
68  }
69  return;
70  }
71  if ( $this->hasOption( 'host' ) ) {
72  $host = $this->getOption( 'host' );
73  $serverCount = $lb->getServerCount();
74  for ( $index = 0; $index < $serverCount; ++$index ) {
75  if ( $lb->getServerName( $index ) === $host ) {
76  break;
77  }
78  }
79  if ( $index >= $serverCount ) {
80  $this->fatalError( "Error: Host not configured: \"$host\"" );
81  }
82  } elseif ( $this->hasOption( 'write' ) ) {
83  $index = $lb->getWriterIndex();
84  } else {
85  $group = $this->getOption( 'group', false );
86  $index = $lb->getReaderIndex( $group, $dbName );
87  if ( $index === false && $group ) {
88  // retry without the group; it may not exist
89  $index = $lb->getReaderIndex( false, $dbName );
90  }
91  if ( $index === false ) {
92  $this->fatalError( 'Error: unable to get reader index' );
93  }
94  }
95 
96  if ( $lb->getServerType( $index ) !== 'mysql' ) {
97  $this->fatalError( 'Error: this script only works with MySQL/MariaDB' );
98  }
99 
100  $this->runMysql( $lb->getServerInfo( $index ), $dbName );
101  }
102 
109  private function runMysql( $info, $dbName ) {
110  // Write the password to an option file to avoid disclosing it to other
111  // processes running on the system
112  $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
113  chmod( $tmpFile->getPath(), 0600 );
114  file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
115 
116  // stdin/stdout need to be the actual file descriptors rather than
117  // PHP's pipe wrappers so that mysql can use them as an interactive
118  // terminal.
119  $desc = [
120  0 => STDIN,
121  1 => STDOUT,
122  2 => STDERR,
123  ];
124 
125  // Split host and port as in DatabaseMysqli::mysqlConnect()
126  $realServer = $info['host'];
127  $hostAndPort = IPUtils::splitHostAndPort( $realServer );
128  $socket = false;
129  $port = false;
130  if ( $hostAndPort ) {
131  $realServer = $hostAndPort[0];
132  if ( $hostAndPort[1] ) {
133  $port = $hostAndPort[1];
134  }
135  } elseif ( substr_count( $realServer, ':' ) == 1 ) {
136  // If we have a colon and something that's not a port number
137  // inside the hostname, assume it's the socket location
138  list( $realServer, $socket ) = explode( ':', $realServer, 2 );
139  }
140 
141  if ( $dbName === false ) {
142  $dbName = $info['dbname'];
143  }
144 
145  $args = [
146  'mysql',
147  "--defaults-extra-file={$tmpFile->getPath()}",
148  "--user={$info['user']}",
149  "--database={$dbName}",
150  ];
151  if ( $socket !== false ) {
152  $args[] = "--socket={$socket}";
153  } else {
154  $args[] = "--host={$realServer}";
155  }
156  if ( $port !== false ) {
157  $args[] = "--port={$port}";
158  }
159 
160  $args = array_merge( $args, $this->mArgs );
161 
162  // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
163  // ctrl-C to kill a query.
164  if ( function_exists( 'pcntl_signal' ) ) {
165  pcntl_signal( SIGINT, SIG_IGN );
166  }
167 
168  $pipes = [];
169  $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
170  if ( $proc === false ) {
171  $this->fatalError( 'Unable to execute mysql' );
172  }
173 
174  $ret = proc_close( $proc );
175  if ( $ret === -1 ) {
176  $this->fatalError( 'proc_close() returned -1' );
177  } elseif ( $ret ) {
178  $this->fatalError( 'Failed.', $ret );
179  }
180  }
181 }
182 
183 $maintClass = MysqlMaintenance::class;
184 require_once RUN_MAINTENANCE_IF_MAIN;
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:45
MysqlMaintenance\__construct
__construct()
Default constructor.
Definition: mysql.php:36
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:202
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
MysqlMaintenance
Definition: mysql.php:35
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
MysqlMaintenance\runMysql
runMysql( $info, $dbName)
Run the mysql client for the given server info.
Definition: mysql.php:109
MysqlMaintenance\execute
execute()
Do the actual work.
Definition: mysql.php:52
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$args
if( $line===false) $args
Definition: mcc.php:124
$maintClass
$maintClass
Definition: mysql.php:183
TempFSFile\factory
static factory( $prefix, $extension='', $tmpDirectory=null)
Make a new temporary file on the file system.
Definition: TempFSFile.php:69
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:300
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271