MediaWiki fundraising/REL1_35
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 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;
187require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true)
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Executes shell commands.
Definition Shell.php:44
__construct()
Default constructor.
Definition mysql.php:36
runMysql( $info, $dbName)
Run the mysql client for the given server info.
Definition mysql.php:112
execute()
Do the actual work.
Definition mysql.php:52
if( $line===false) $args
Definition mcc.php:124
$maintClass
Definition mysql.php:186