MediaWiki REL1_34
mysql.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( "Execute the MySQL client binary. " .
38 "Non-option arguments will be passed through to mysql." );
39 $this->addOption( 'write', 'Connect to the master database', false, false );
40 $this->addOption( 'group', 'Specify query group', false, false );
41 $this->addOption( 'host', 'Connect to a specific MySQL server', false, true );
42 $this->addOption( 'list-hosts', 'List the available DB hosts', false, false );
43 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
44 $this->addOption( 'wikidb',
45 'The database wiki ID to use if not the current one', false, true );
46
47 // Fake argument for help message
48 $this->addArg( '-- mysql_option ...', 'Options to pass to mysql', false );
49 }
50
51 public function execute() {
52 $dbName = $this->getOption( 'wikidb', false );
53 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
54 if ( $this->hasOption( 'cluster' ) ) {
55 try {
56 $lb = $lbf->getExternalLB( $this->getOption( 'cluster' ) );
57 } catch ( InvalidArgumentException $e ) {
58 $this->error( "Error: invalid cluster" );
59 exit( 1 );
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 exit( 0 );
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->error( "Error: Host not configured: \"$host\"" );
81 exit( 1 );
82 }
83 } elseif ( $this->hasOption( 'write' ) ) {
84 $index = $lb->getWriterIndex();
85 } else {
86 $group = $this->getOption( 'group', false );
87 $index = $lb->getReaderIndex( $group, $dbName );
88 if ( $index === false ) {
89 $this->error( "Error: unable to get reader index" );
90 exit( 1 );
91 }
92 }
93
94 if ( $lb->getServerType( $index ) !== 'mysql' ) {
95 $this->error( "Error: this script only works with MySQL/MariaDB" );
96 exit( 1 );
97 }
98
99 $status = $this->runMysql( $lb->getServerInfo( $index ), $dbName );
100 exit( $status );
101 }
102
111 private function runMysql( $info, $dbName ) {
112 // Write the password to an option file to avoid disclosing it to other
113 // processes running on the system
114 $tmpFile = TempFSFile::factory( 'mw-mysql', 'ini' );
115 chmod( $tmpFile->getPath(), 0600 );
116 file_put_contents( $tmpFile->getPath(), "[client]\npassword={$info['password']}\n" );
117
118 // stdin/stdout need to be the actual file descriptors rather than
119 // PHP's pipe wrappers so that mysql can use them as an interactive
120 // terminal.
121 $desc = [
122 0 => STDIN,
123 1 => STDOUT,
124 2 => STDERR,
125 ];
126
127 // Split host and port as in DatabaseMysqli::mysqlConnect()
128 $realServer = $info['host'];
129 $hostAndPort = IP::splitHostAndPort( $realServer );
130 $socket = false;
131 $port = false;
132 if ( $hostAndPort ) {
133 $realServer = $hostAndPort[0];
134 if ( $hostAndPort[1] ) {
135 $port = $hostAndPort[1];
136 }
137 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
138 // If we have a colon and something that's not a port number
139 // inside the hostname, assume it's the socket location
140 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
141 }
142
143 if ( $dbName === false ) {
144 $dbName = $info['dbname'];
145 }
146
147 $args = [
148 'mysql',
149 "--defaults-extra-file={$tmpFile->getPath()}",
150 "--user={$info['user']}",
151 "--database={$dbName}",
152 ];
153 if ( $socket !== false ) {
154 $args[] = "--socket={$socket}";
155 } else {
156 $args[] = "--host={$realServer}";
157 }
158 if ( $port !== false ) {
159 $args[] = "--port={$port}";
160 }
161
162 $args = array_merge( $args, $this->mArgs );
163
164 // Ignore SIGINT if possible, otherwise the wrapper terminates when the user presses
165 // ctrl-C to kill a query.
166 if ( function_exists( 'pcntl_signal' ) ) {
167 pcntl_signal( SIGINT, SIG_IGN );
168 }
169
170 $pipes = [];
171 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
172 if ( $proc === false ) {
173 $this->error( "Unable to execute mysql" );
174 return 1;
175 }
176 $ret = proc_close( $proc );
177 if ( $ret === -1 ) {
178 $this->error( "proc_close() returned -1" );
179 return 1;
180 }
181 return $ret;
182 }
183}
184
185$maintClass = MysqlMaintenance::class;
186require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
if( $line===false) $args
Definition cdb.php:64
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 exists.
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:35
runMysql( $info, $dbName)
Run the mysql client for the given server info.
Definition mysql.php:111
execute()
Do the actual work.
Definition mysql.php:51
$maintClass
Definition mysql.php:185