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 master database', false, false );
41  $this->addOption( 'group', 'Specify query group', false, false );
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->error( "Error: invalid cluster" );
60  exit( 1 );
61  }
62  } else {
63  $lb = $lbf->getMainLB( $dbName );
64  }
65  if ( $this->hasOption( 'list-hosts' ) ) {
66  $serverCount = $lb->getServerCount();
67  for ( $index = 0; $index < $serverCount; ++$index ) {
68  echo $lb->getServerName( $index ) . "\n";
69  }
70  exit( 0 );
71  }
72  if ( $this->hasOption( 'host' ) ) {
73  $host = $this->getOption( 'host' );
74  $serverCount = $lb->getServerCount();
75  for ( $index = 0; $index < $serverCount; ++$index ) {
76  if ( $lb->getServerName( $index ) === $host ) {
77  break;
78  }
79  }
80  if ( $index >= $serverCount ) {
81  $this->error( "Error: Host not configured: \"$host\"" );
82  exit( 1 );
83  }
84  } elseif ( $this->hasOption( 'write' ) ) {
85  $index = $lb->getWriterIndex();
86  } else {
87  $group = $this->getOption( 'group', false );
88  $index = $lb->getReaderIndex( $group, $dbName );
89  if ( $index === false ) {
90  $this->error( "Error: unable to get reader index" );
91  exit( 1 );
92  }
93  }
94 
95  if ( $lb->getServerType( $index ) !== 'mysql' ) {
96  $this->error( "Error: this script only works with MySQL/MariaDB" );
97  exit( 1 );
98  }
99 
100  $status = $this->runMysql( $lb->getServerInfo( $index ), $dbName );
101  exit( $status );
102  }
103 
112  private function runMysql( $info, $dbName ) {
113  // Write the password to an option file to avoid disclosing it to other
114  // processes running on the system
115  $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
116  chmod( $tmpFile->getPath(), 0600 );
117  file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
118 
119  // stdin/stdout need to be the actual file descriptors rather than
120  // PHP's pipe wrappers so that mysql can use them as an interactive
121  // terminal.
122  $desc = [
123  0 => STDIN,
124  1 => STDOUT,
125  2 => STDERR,
126  ];
127 
128  // Split host and port as in DatabaseMysqli::mysqlConnect()
129  $realServer = $info['host'];
130  $hostAndPort = IPUtils::splitHostAndPort( $realServer );
131  $socket = false;
132  $port = false;
133  if ( $hostAndPort ) {
134  $realServer = $hostAndPort[0];
135  if ( $hostAndPort[1] ) {
136  $port = $hostAndPort[1];
137  }
138  } elseif ( substr_count( $realServer, ':' ) == 1 ) {
139  // If we have a colon and something that's not a port number
140  // inside the hostname, assume it's the socket location
141  list( $realServer, $socket ) = explode( ':', $realServer, 2 );
142  }
143 
144  if ( $dbName === false ) {
145  $dbName = $info['dbname'];
146  }
147 
148  $args = [
149  'mysql',
150  "--defaults-extra-file={$tmpFile->getPath()}",
151  "--user={$info['user']}",
152  "--database={$dbName}",
153  ];
154  if ( $socket !== false ) {
155  $args[] = "--socket={$socket}";
156  } else {
157  $args[] = "--host={$realServer}";
158  }
159  if ( $port !== false ) {
160  $args[] = "--port={$port}";
161  }
162 
163  $args = array_merge( $args, $this->mArgs );
164 
165  // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
166  // ctrl-C to kill a query.
167  if ( function_exists( 'pcntl_signal' ) ) {
168  pcntl_signal( SIGINT, SIG_IGN );
169  }
170 
171  $pipes = [];
172  $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
173  if ( $proc === false ) {
174  $this->error( "Unable to execute mysql" );
175  return 1;
176  }
177  $ret = proc_close( $proc );
178  if ( $ret === -1 ) {
179  $this->error( "proc_close() returned -1" );
180  return 1;
181  }
182  return $ret;
183  }
184 }
185 
186 $maintClass = MysqlMaintenance::class;
187 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:44
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:154
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
MysqlMaintenance
Definition: mysql.php:35
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
MysqlMaintenance\runMysql
runMysql( $info, $dbName)
Run the mysql client for the given server info.
Definition: mysql.php:112
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:245
$args
if( $line===false) $args
Definition: mcc.php:124
$maintClass
$maintClass
Definition: mysql.php:186
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:281
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:298
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266