MediaWiki master
mysql.php
Go to the documentation of this file.
1<?php
15use Wikimedia\IPUtils;
17
18// @codeCoverageIgnoreStart
19require_once __DIR__ . '/Maintenance.php';
20// @codeCoverageIgnoreEnd
21
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription( "Execute the MySQL client binary. " .
29 "Non-option arguments will be passed through to mysql." );
30 $this->addOption( 'write', 'Connect to the primary database', false, false );
31 $this->addOption( 'group', 'Specify query group', false, true );
32 $this->addOption( 'host', 'Connect to a known MySQL server', false, true );
33 $this->addOption( 'raw-host',
34 'Connect directly to a specific MySQL server, even if not known to MediaWiki '
35 . 'via wgLBFactoryConf (e.g. parser cache or depooled host). '
36 . 'Credentials will be chosen based on --cluster and --wikidb.',
37 false,
38 true
39 );
40 $this->addOption( 'list-hosts', 'List the available DB hosts', false, false );
41 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
42 $this->addOption( 'wikidb',
43 'The database wiki ID to use if not the current one',
44 false,
45 true
46 );
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 = $this->getServiceContainer()->getDBLoadBalancerFactory();
55
56 // Pick LB
57 if ( $this->hasOption( 'cluster' ) ) {
58 try {
59 $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
60 } catch ( InvalidArgumentException ) {
61 $this->fatalError( 'Error: invalid cluster' );
62 }
63 } else {
64 $lb = $lbf->getMainLB( $dbName );
65 }
66
67 // List hosts, or pick host
68 if ( $this->hasOption( 'list-hosts' ) ) {
69 $serverCount = $lb->getServerCount();
70 for ( $index = 0; $index < $serverCount; ++$index ) {
71 echo $lb->getServerName( $index ) . "\n";
72 }
73 return;
74 }
75 if ( $this->hasOption( 'host' ) ) {
76 $host = $this->getOption( 'host' );
77 $serverCount = $lb->getServerCount();
78 for ( $index = 0; $index < $serverCount; ++$index ) {
79 if ( $lb->getServerName( $index ) === $host ) {
80 break;
81 }
82 }
83 if ( $index >= $serverCount ) {
84 $this->fatalError( "Error: Host not configured: \"$host\"" );
85 }
86 } elseif ( $this->hasOption( 'write' ) ) {
87 $index = ServerInfo::WRITER_INDEX;
88 } else {
89 $group = $this->getOption( 'group', false );
90 $index = $lb->getReaderIndex( $group );
91 if ( $index === false && $group ) {
92 // retry without the group; it may not exist
93 $index = $lb->getReaderIndex( false );
94 }
95 if ( $index === false ) {
96 $this->fatalError( 'Error: unable to get reader index' );
97 }
98 }
99 if ( $lb->getServerType( $index ) !== 'mysql' ) {
100 $this->fatalError( 'Error: this script only works with MySQL/MariaDB' );
101 }
102
103 $serverInfo = $lb->getServerInfo( $index );
104 // Override host. This uses the server info of the host determined
105 // by the other options for the purposes of user/password.
106 if ( $this->hasOption( 'raw-host' ) ) {
107 $host = $this->getOption( 'raw-host' );
108 $serverInfo = [ 'host' => $host ] + $serverInfo;
109 }
110
111 $this->runMysql( $serverInfo, $dbName );
112 }
113
120 private function runMysql( $info, $dbName ) {
121 // Write the password to an option file to avoid disclosing it to other
122 // processes running on the system
123 $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
124 chmod( $tmpFile->getPath(), 0600 );
125 file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
126
127 // stdin/stdout need to be the actual file descriptors rather than
128 // PHP's pipe wrappers so that mysql can use them as an interactive
129 // terminal.
130 $desc = [
131 0 => STDIN,
132 1 => STDOUT,
133 2 => STDERR,
134 ];
135
136 // Split host and port as in DatabaseMySQL::mysqlConnect()
137 $realServer = $info['host'];
138 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
139 $socket = false;
140 $port = false;
141 if ( $hostAndPort ) {
142 $realServer = $hostAndPort[0];
143 if ( $hostAndPort[1] ) {
144 $port = $hostAndPort[1];
145 }
146 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
147 // If we have a colon and something that's not a port number
148 // inside the hostname, assume it's the socket location
149 [ $realServer, $socket ] = explode( ':', $realServer, 2 );
150 }
151
152 if ( $dbName === false ) {
153 $dbName = $info['dbname'];
154 }
155
156 $args = [
157 'mysql',
158 "--defaults-extra-file={$tmpFile->getPath()}",
159 "--user={$info['user']}",
160 "--database={$dbName}",
161 ];
162 if ( $socket !== false ) {
163 $args[] = "--socket={$socket}";
164 } else {
165 $args[] = "--host={$realServer}";
166 }
167 if ( $port !== false ) {
168 $args[] = "--port={$port}";
169 }
170
171 $args = array_merge( $args, $this->getArgs() );
172
173 // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
174 // ctrl-C to kill a query.
175 if ( function_exists( 'pcntl_signal' ) ) {
176 pcntl_signal( SIGINT, SIG_IGN );
177 }
178
179 $pipes = [];
180 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
181 if ( $proc === false ) {
182 $this->fatalError( 'Unable to execute mysql' );
183 }
184
185 $ret = proc_close( $proc );
186 if ( $ret === -1 ) {
187 $this->fatalError( 'proc_close() returned -1' );
188 } elseif ( $ret ) {
189 $this->fatalError( 'Failed.', $ret );
190 }
191 }
192}
193
194// @codeCoverageIgnoreStart
195$maintClass = MysqlMaintenance::class;
196require_once RUN_MAINTENANCE_IF_MAIN;
197// @codeCoverageIgnoreEnd
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.
getArgs( $offset=0)
Get arguments.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Executes shell commands.
Definition Shell.php:32
__construct()
Default constructor.
Definition mysql.php:26
execute()
Do the actual work.
Definition mysql.php:52
This class is used to hold the location and do limited manipulation of files stored temporarily (this...
Container for accessing information about the database servers in a database cluster.
$maintClass
Definition mysql.php:195