37 parent::__construct();
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 );
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.',
50 $this->
addOption(
'list-hosts',
'List the available DB hosts',
false,
false );
51 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
53 'The database wiki ID to use if not the current one',
59 $this->
addArg(
'-- mysql_option ...',
'Options to pass to mysql',
false );
63 $dbName = $this->
getOption(
'wikidb',
false );
64 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
69 $lb = $lbf->getExternalLB( $this->
getOption(
'cluster' ) );
70 }
catch ( InvalidArgumentException $e ) {
74 $lb = $lbf->getMainLB( $dbName );
79 $serverCount = $lb->getServerCount();
80 for ( $index = 0; $index < $serverCount; ++$index ) {
81 echo $lb->getServerName( $index ) .
"\n";
87 $serverCount = $lb->getServerCount();
88 for ( $index = 0; $index < $serverCount; ++$index ) {
89 if ( $lb->getServerName( $index ) === $host ) {
93 if ( $index >= $serverCount ) {
94 $this->
fatalError(
"Error: Host not configured: \"$host\"" );
96 } elseif ( $this->
hasOption(
'write' ) ) {
97 $index = $lb->getWriterIndex();
99 $group = $this->
getOption(
'group',
false );
100 $index = $lb->getReaderIndex( $group );
101 if ( $index ===
false && $group ) {
103 $index = $lb->getReaderIndex(
false );
105 if ( $index ===
false ) {
106 $this->
fatalError(
'Error: unable to get reader index' );
109 if ( $lb->getServerType( $index ) !==
'mysql' ) {
110 $this->
fatalError(
'Error: this script only works with MySQL/MariaDB' );
113 $serverInfo = $lb->getServerInfo( $index );
118 $serverInfo = [
'host' => $host ] + $serverInfo;
121 $this->runMysql( $serverInfo, $dbName );
130 private function runMysql( $info, $dbName ) {
133 $tmpFile = TempFSFile::factory(
'mw-mysql',
'ini' );
134 chmod( $tmpFile->getPath(), 0600 );
135 file_put_contents( $tmpFile->getPath(),
"[client]\npassword={$info['password']}\n" );
147 $realServer = $info[
'host'];
148 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
151 if ( $hostAndPort ) {
152 $realServer = $hostAndPort[0];
153 if ( $hostAndPort[1] ) {
154 $port = $hostAndPort[1];
156 } elseif ( substr_count( $realServer,
':' ) == 1 ) {
159 [ $realServer, $socket ] = explode(
':', $realServer, 2 );
162 if ( $dbName ===
false ) {
163 $dbName = $info[
'dbname'];
168 "--defaults-extra-file={$tmpFile->getPath()}",
169 "--user={$info['user']}",
170 "--database={$dbName}",
172 if ( $socket !==
false ) {
173 $args[] =
"--socket={$socket}";
175 $args[] =
"--host={$realServer}";
177 if ( $port !==
false ) {
178 $args[] =
"--port={$port}";
181 $args = array_merge( $args, $this->mArgs );
185 if ( function_exists(
'pcntl_signal' ) ) {
186 pcntl_signal( SIGINT, SIG_IGN );
190 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
191 if ( $proc ===
false ) {
192 $this->
fatalError(
'Unable to execute mysql' );
195 $ret = proc_close( $proc );
197 $this->
fatalError(
'proc_close() returned -1' );
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.