MediaWiki 1.40.4
mysql.php
Go to the documentation of this file.
1<?php
28use Wikimedia\IPUtils;
29
30require_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 known MySQL server', false, true );
43 $this->addOption( 'raw-host',
44 'Connect directly to a specific MySQL server, even if not known to MediaWiki '
45 . 'via wgLBFactoryConf (e.g. parser cache or depooled host). '
46 . 'Credentails will be chosen based on --cluster and --wikidb.',
47 false,
48 true
49 );
50 $this->addOption( 'list-hosts', 'List the available DB hosts', false, false );
51 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
52 $this->addOption( 'wikidb',
53 'The database wiki ID to use if not the current one',
54 false,
55 true
56 );
57
58 // Fake argument for help message
59 $this->addArg( '-- mysql_option ...', 'Options to pass to mysql', false );
60 }
61
62 public function execute() {
63 $dbName = $this->getOption( 'wikidb', false );
64 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
65
66 // Pick LB
67 if ( $this->hasOption( 'cluster' ) ) {
68 try {
69 $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
70 } catch ( InvalidArgumentException $e ) {
71 $this->fatalError( 'Error: invalid cluster' );
72 }
73 } else {
74 $lb = $lbf->getMainLB( $dbName );
75 }
76
77 // List hosts, or pick host
78 if ( $this->hasOption( 'list-hosts' ) ) {
79 $serverCount = $lb->getServerCount();
80 for ( $index = 0; $index < $serverCount; ++$index ) {
81 echo $lb->getServerName( $index ) . "\n";
82 }
83 return;
84 }
85 if ( $this->hasOption( 'host' ) ) {
86 $host = $this->getOption( 'host' );
87 $serverCount = $lb->getServerCount();
88 for ( $index = 0; $index < $serverCount; ++$index ) {
89 if ( $lb->getServerName( $index ) === $host ) {
90 break;
91 }
92 }
93 if ( $index >= $serverCount ) {
94 $this->fatalError( "Error: Host not configured: \"$host\"" );
95 }
96 } elseif ( $this->hasOption( 'write' ) ) {
97 $index = $lb->getWriterIndex();
98 } else {
99 $group = $this->getOption( 'group', false );
100 $index = $lb->getReaderIndex( $group );
101 if ( $index === false && $group ) {
102 // retry without the group; it may not exist
103 $index = $lb->getReaderIndex( false );
104 }
105 if ( $index === false ) {
106 $this->fatalError( 'Error: unable to get reader index' );
107 }
108 }
109 if ( $lb->getServerType( $index ) !== 'mysql' ) {
110 $this->fatalError( 'Error: this script only works with MySQL/MariaDB' );
111 }
112
113 $serverInfo = $lb->getServerInfo( $index );
114 // Override host. This uses the server info of the host determined
115 // by the other options for the purposes of user/password.
116 if ( $this->hasOption( 'raw-host' ) ) {
117 $host = $this->getOption( 'raw-host' );
118 $serverInfo = [ 'host' => $host ] + $serverInfo;
119 }
120
121 $this->runMysql( $serverInfo, $dbName );
122 }
123
130 private function runMysql( $info, $dbName ) {
131 // Write the password to an option file to avoid disclosing it to other
132 // processes running on the system
133 $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
134 chmod( $tmpFile->getPath(), 0600 );
135 file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
136
137 // stdin/stdout need to be the actual file descriptors rather than
138 // PHP's pipe wrappers so that mysql can use them as an interactive
139 // terminal.
140 $desc = [
141 0 => STDIN,
142 1 => STDOUT,
143 2 => STDERR,
144 ];
145
146 // Split host and port as in DatabaseMysqli::mysqlConnect()
147 $realServer = $info['host'];
148 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
149 $socket = false;
150 $port = false;
151 if ( $hostAndPort ) {
152 $realServer = $hostAndPort[0];
153 if ( $hostAndPort[1] ) {
154 $port = $hostAndPort[1];
155 }
156 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
157 // If we have a colon and something that's not a port number
158 // inside the hostname, assume it's the socket location
159 [ $realServer, $socket ] = explode( ':', $realServer, 2 );
160 }
161
162 if ( $dbName === false ) {
163 $dbName = $info['dbname'];
164 }
165
166 $args = [
167 'mysql',
168 "--defaults-extra-file={$tmpFile->getPath()}",
169 "--user={$info['user']}",
170 "--database={$dbName}",
171 ];
172 if ( $socket !== false ) {
173 $args[] = "--socket={$socket}";
174 } else {
175 $args[] = "--host={$realServer}";
176 }
177 if ( $port !== false ) {
178 $args[] = "--port={$port}";
179 }
180
181 $args = array_merge( $args, $this->mArgs );
182
183 // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
184 // ctrl-C to kill a query.
185 if ( function_exists( 'pcntl_signal' ) ) {
186 pcntl_signal( SIGINT, SIG_IGN );
187 }
188
189 $pipes = [];
190 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
191 if ( $proc === false ) {
192 $this->fatalError( 'Unable to execute mysql' );
193 }
194
195 $ret = proc_close( $proc );
196 if ( $ret === -1 ) {
197 $this->fatalError( 'proc_close() returned -1' );
198 } elseif ( $ret ) {
199 $this->fatalError( 'Failed.', $ret );
200 }
201 }
202}
203
204$maintClass = MysqlMaintenance::class;
205require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Executes shell commands.
Definition Shell.php:46
__construct()
Default constructor.
Definition mysql.php:36
execute()
Do the actual work.
Definition mysql.php:62
$maintClass
Definition mysql.php:204