39 parent::__construct();
41 "Non-option arguments will be passed through to mysql." );
42 $this->
addOption(
'write',
'Connect to the primary database',
false,
false );
43 $this->
addOption(
'group',
'Specify query group',
false,
true );
44 $this->
addOption(
'host',
'Connect to a known MySQL server',
false,
true );
46 'Connect directly to a specific MySQL server, even if not known to MediaWiki '
47 .
'via wgLBFactoryConf (e.g. parser cache or depooled host). '
48 .
'Credentails will be chosen based on --cluster and --wikidb.',
52 $this->
addOption(
'list-hosts',
'List the available DB hosts',
false,
false );
53 $this->
addOption(
'cluster',
'Use an external cluster by name',
false,
true );
55 'The database wiki ID to use if not the current one',
61 $this->
addArg(
'-- mysql_option ...',
'Options to pass to mysql',
false );
65 $dbName = $this->
getOption(
'wikidb',
false );
71 $lb = $lbf->getExternalLB( $this->
getOption(
'cluster' ) );
72 }
catch ( InvalidArgumentException $e ) {
76 $lb = $lbf->getMainLB( $dbName );
81 $serverCount = $lb->getServerCount();
82 for ( $index = 0; $index < $serverCount; ++$index ) {
83 echo $lb->getServerName( $index ) .
"\n";
89 $serverCount = $lb->getServerCount();
90 for ( $index = 0; $index < $serverCount; ++$index ) {
91 if ( $lb->getServerName( $index ) === $host ) {
95 if ( $index >= $serverCount ) {
96 $this->
fatalError(
"Error: Host not configured: \"$host\"" );
98 } elseif ( $this->
hasOption(
'write' ) ) {
99 $index = ServerInfo::WRITER_INDEX;
101 $group = $this->
getOption(
'group',
false );
102 $index = $lb->getReaderIndex( $group );
103 if ( $index ===
false && $group ) {
105 $index = $lb->getReaderIndex(
false );
107 if ( $index ===
false ) {
108 $this->
fatalError(
'Error: unable to get reader index' );
111 if ( $lb->getServerType( $index ) !==
'mysql' ) {
112 $this->
fatalError(
'Error: this script only works with MySQL/MariaDB' );
115 $serverInfo = $lb->getServerInfo( $index );
120 $serverInfo = [
'host' => $host ] + $serverInfo;
123 $this->runMysql( $serverInfo, $dbName );
132 private function runMysql( $info, $dbName ) {
135 $tmpFile = TempFSFile::factory(
'mw-mysql',
'ini' );
136 chmod( $tmpFile->getPath(), 0600 );
137 file_put_contents( $tmpFile->getPath(),
"[client]\npassword={$info['password']}\n" );
149 $realServer = $info[
'host'];
150 $hostAndPort = IPUtils::splitHostAndPort( $realServer );
153 if ( $hostAndPort ) {
154 $realServer = $hostAndPort[0];
155 if ( $hostAndPort[1] ) {
156 $port = $hostAndPort[1];
158 } elseif ( substr_count( $realServer,
':' ) == 1 ) {
161 [ $realServer, $socket ] = explode(
':', $realServer, 2 );
164 if ( $dbName ===
false ) {
165 $dbName = $info[
'dbname'];
170 "--defaults-extra-file={$tmpFile->getPath()}",
171 "--user={$info['user']}",
172 "--database={$dbName}",
174 if ( $socket !==
false ) {
175 $args[] =
"--socket={$socket}";
177 $args[] =
"--host={$realServer}";
179 if ( $port !==
false ) {
180 $args[] =
"--port={$port}";
183 $args = array_merge( $args, $this->
getArgs() );
187 if ( function_exists(
'pcntl_signal' ) ) {
188 pcntl_signal( SIGINT, SIG_IGN );
192 $proc = proc_open( Shell::escape( $args ), $desc, $pipes );
193 if ( $proc ===
false ) {
194 $this->
fatalError(
'Unable to execute mysql' );
197 $ret = proc_close( $proc );
199 $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.
getServiceContainer()
Returns the main service container.
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.