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 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 );
46 'The database wiki ID to use if not the current one',
false,
true );
49 $this->
addArg(
'-- mysql_option ...',
'Options to pass to mysql',
false );
53 $dbName = $this->
getOption(
'wikidb',
false );
54 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
57 $lb = $lbf->getExternalLB( $this->
getOption(
'cluster' ) );
58 }
catch ( InvalidArgumentException $e ) {
62 $lb = $lbf->getMainLB( $dbName );
65 $serverCount = $lb->getServerCount();
66 for ( $index = 0; $index < $serverCount; ++$index ) {
67 echo $lb->getServerName( $index ) .
"\n";
73 $serverCount = $lb->getServerCount();
74 for ( $index = 0; $index < $serverCount; ++$index ) {
75 if ( $lb->getServerName( $index ) === $host ) {
79 if ( $index >= $serverCount ) {
80 $this->
fatalError(
"Error: Host not configured: \"$host\"" );
82 } elseif ( $this->
hasOption(
'write' ) ) {
83 $index = $lb->getWriterIndex();
85 $group = $this->
getOption(
'group',
false );
86 $index = $lb->getReaderIndex( $group, $dbName );
87 if ( $index ===
false && $group ) {
89 $index = $lb->getReaderIndex(
false, $dbName );
91 if ( $index ===
false ) {
92 $this->
fatalError(
'Error: unable to get reader index' );
96 if ( $lb->getServerType( $index ) !==
'mysql' ) {
97 $this->
fatalError(
'Error: this script only works with MySQL/MariaDB' );
100 $this->runMysql( $lb->getServerInfo( $index ), $dbName );
109 private function runMysql( $info, $dbName ) {
112 $tmpFile = TempFSFile::factory(
'mw-mysql',
'ini' );
113 chmod( $tmpFile->getPath(), 0600 );
114 file_put_contents( $tmpFile->getPath(),
"[client]\npassword={$info['password']}\n" );
126 $realServer = $info[
'host'];
127 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
130 if ( $hostAndPort ) {
131 $realServer = $hostAndPort[0];
132 if ( $hostAndPort[1] ) {
133 $port = $hostAndPort[1];
135 } elseif ( substr_count( $realServer,
':' ) == 1 ) {
138 list( $realServer, $socket ) = explode(
':', $realServer, 2 );
141 if ( $dbName ===
false ) {
142 $dbName = $info[
'dbname'];
147 "--defaults-extra-file={$tmpFile->getPath()}",
148 "--user={$info['user']}",
149 "--database={$dbName}",
151 if ( $socket !==
false ) {
152 $args[] =
"--socket={$socket}";
154 $args[] =
"--host={$realServer}";
156 if ( $port !==
false ) {
157 $args[] =
"--port={$port}";
164 if ( function_exists(
'pcntl_signal' ) ) {
165 pcntl_signal( SIGINT, SIG_IGN );
169 $proc = proc_open( Shell::escape(
$args ), $desc, $pipes );
170 if ( $proc ===
false ) {
171 $this->
fatalError(
'Unable to execute mysql' );
174 $ret = proc_close( $proc );
176 $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)
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.