29 require_once __DIR__ .
'/Maintenance.php';
36 parent::__construct();
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 );
45 'The database wiki ID to use if not the current one',
false,
true );
48 $this->
addArg(
'-- mysql_option ...',
'Options to pass to mysql',
false );
52 $dbName = $this->
getOption(
'wikidb',
false );
53 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
56 $lb = $lbf->getExternalLB( $this->
getOption(
'cluster' ) );
57 }
catch ( InvalidArgumentException $e ) {
58 $this->
error(
"Error: invalid cluster" );
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->
error(
"Error: Host not configured: \"$host\"" );
83 } elseif ( $this->
hasOption(
'write' ) ) {
84 $index = $lb->getWriterIndex();
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" );
94 if ( $lb->getServerType( $index ) !==
'mysql' ) {
95 $this->
error(
"Error: this script only works with MySQL/MariaDB" );
99 $status = $this->
runMysql( $lb->getServerInfo( $index ), $dbName );
115 chmod( $tmpFile->getPath(), 0600 );
116 file_put_contents( $tmpFile->getPath(),
"[client]\npassword={$info['password']}\n" );
128 $realServer = $info[
'host'];
132 if ( $hostAndPort ) {
133 $realServer = $hostAndPort[0];
134 if ( $hostAndPort[1] ) {
135 $port = $hostAndPort[1];
137 } elseif ( substr_count( $realServer,
':' ) == 1 ) {
140 list( $realServer, $socket ) = explode(
':', $realServer, 2 );
143 if ( $dbName ===
false ) {
144 $dbName = $info[
'dbname'];
149 "--defaults-extra-file={$tmpFile->getPath()}",
150 "--user={$info['user']}",
151 "--database={$dbName}",
153 if ( $socket !==
false ) {
154 $args[] =
"--socket={$socket}";
156 $args[] =
"--host={$realServer}";
158 if ( $port !==
false ) {
159 $args[] =
"--port={$port}";
166 if ( function_exists(
'pcntl_signal' ) ) {
167 pcntl_signal( SIGINT, SIG_IGN );
171 $proc = proc_open( Shell::escape(
$args ), $desc, $pipes );
172 if ( $proc ===
false ) {
173 $this->
error(
"Unable to execute mysql" );
176 $ret = proc_close( $proc );
178 $this->
error(
"proc_close() returned -1" );