/usr/bin/mysqlrpladmin is in mysql-utilities 1.6.1-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | #!/usr/bin/python
#
# Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
This file contains the replication slave administration utility. It is used to
perform replication operations on one or more slaves.
"""
from mysql.utilities.common.tools import check_python_version
# Check Python version compatibility
check_python_version()
import logging
import os.path
import sys
from mysql.utilities import VERSION_FRM, VERSION_STRING
from mysql.utilities.exception import UtilError, UtilRplError, FormatError
from mysql.utilities.common.ip_parser import parse_connection
from mysql.utilities.common.server import Server, check_hostname_alias
from mysql.utilities.common.tools import check_connector_python
from mysql.utilities.common.topology import parse_topology_connections
from mysql.utilities.common.options import (add_format_option, add_verbosity,
add_failover_options, add_rpl_user,
add_ssl_options,
CaseInsensitiveChoicesOption,
check_server_lists,
get_ssl_dict,
license_callback, UtilitiesParser,
check_password_security,
check_script_option)
from mysql.utilities.common.messages import (PARSE_ERR_OPT_INVALID_CMD_TIP,
PARSE_ERR_OPTS_EXCLD,
PARSE_ERR_OPTS_REQ_BY_CMD,
PARSE_ERR_SLAVE_DISCO_REQ,
WARN_OPT_NOT_REQUIRED,
WARN_OPT_NOT_REQUIRED_ONLY_FOR,
ERROR_SAME_MASTER,
ERROR_MASTER_IN_SLAVES,
SCRIPT_THRESHOLD_WARNING, SLAVES,
CANDIDATES,
MSG_UTILITIES_VERSION)
from mysql.utilities.command.rpl_admin import (RplCommands, purge_log,
get_valid_rpl_commands,
get_valid_rpl_command_text)
class MyParser(UtilitiesParser):
"""Custom class to set the epilog.
"""
def format_epilog(self, formatter):
return self.epilog
# Constants
NAME = "MySQL Utilities - mysqlrpladmin "
DESCRIPTION = "mysqlrpladmin - administration utility for MySQL replication"
USAGE = "%prog --slaves=root@localhost:3306 <command>"
_DATE_FORMAT = '%Y-%m-%d %H:%M:%S %p'
# Check for connector/python
if not check_connector_python():
sys.exit(1)
if __name__ == '__main__':
# Setup the command parser
program = os.path.basename(sys.argv[0]).replace(".py", "")
parser = MyParser(
version=VERSION_FRM.format(program=program),
description=DESCRIPTION,
usage=USAGE,
add_help_option=False,
option_class=CaseInsensitiveChoicesOption,
epilog=get_valid_rpl_command_text(),
prog=program
)
# Default option to provide help information
parser.add_option("--help", action="help",
help="display this help message and exit")
# Add --License option
parser.add_option("--license", action='callback',
callback=license_callback,
help="display program's license and exit")
# Setup utility-specific options:
add_failover_options(parser)
# Connection information for the master
# Connect information for candidate server for switchover
parser.add_option("--new-master", action="store", dest="new_master",
default=None, type="string",
help="connection information for the slave to be used to"
" replace the master for switchover, in the form: "
"<user>[:<password>]@<host>[:<port>][:<socket>] or "
"<login-path>[:<port>][:<socket>] or "
"<config-path>[<[group]>]. Valid only with "
"switchover command.")
# Force the execution of the command, ignoring some errors
parser.add_option("--force", action="store_true", dest="force",
help="ignore prerequisite check results or some "
"inconsistencies found (e.g. errant transactions "
"on slaves) and execute action")
# Output format
add_format_option(parser, "display the output in either grid (default), "
"tab, csv, or vertical format", None)
# Add demote option
parser.add_option("--demote-master", action="store_true", dest="demote",
help="make master a slave after switchover.")
# Add no-health option
parser.add_option("--no-health", action="store_true", dest="no_health",
help="turn off health report after switchover or "
"failover.")
# Add verbosity mode
add_verbosity(parser, True)
# Replication user and password
add_rpl_user(parser)
# Add ssl options
add_ssl_options(parser)
# Now we process the rest of the arguments.
opt, args = parser.parse_args()
# Check security settings
check_password_security(opt, args)
# Check for invalid command
if len(args) > 1:
parser.error("You can only specify one command to execute at a time.")
elif len(args) == 0:
parser.error("You must specify a command to execute.")
command = args[0].lower()
# At least one of the options --discover-slaves-login or --slaves is
# required unless we are doing a health command.
if not opt.discover and not opt.slaves and not command == 'health':
parser.error(PARSE_ERR_SLAVE_DISCO_REQ)
# --discover-slaves-login and --slaves cannot be used simultaneously
# (only one)
if opt.discover and opt.slaves:
parser.error(PARSE_ERR_OPTS_EXCLD.format(
opt1='--discover-slaves-login', opt2='--slaves'
))
# Check slaves list
check_server_lists(parser, opt.master, opt.slaves)
# The value for --timeout needs to be an integer > 0.
try:
if int(opt.timeout) <= 0:
parser.error("The --timeout option requires a value greater "
"than 0.")
except ValueError:
parser.error("The --timeout option requires an integer value.")
# Check errors and warnings of options and combinations.
if command not in get_valid_rpl_commands():
parser.error("'{0}' is not a valid command.".format(command))
# --master and --new-master options are required by 'switchover'
if command == 'switchover' and (not opt.new_master or not opt.master):
req_opts = '--master and --new-master'
parser.error(PARSE_ERR_OPTS_REQ_BY_CMD.format(cmd=command,
opts=req_opts))
# Allow health report for --master or --slaves
if command == 'health' and not opt.master and not opt.slaves:
req_opts = '--master or --slaves'
parser.error(PARSE_ERR_OPTS_REQ_BY_CMD.format(cmd=command,
opts=req_opts))
# --master and either --slaves or --discover-slaves-login options are
# required by 'elect' and 'gtid'
if (command in ['elect', 'gtid'] and not opt.master and
(not opt.slaves or not opt.discover)):
req_opts = '--master and either --slaves or --discover-slaves-login'
parser.error(PARSE_ERR_OPTS_REQ_BY_CMD.format(cmd=command,
opts=req_opts))
# --slaves options are required by 'start', 'stop' and 'reset'
# --master is optional
if command in ['start', 'stop', 'reset'] and not opt.slaves:
req_opts = '--slaves'
parser.error(PARSE_ERR_OPTS_REQ_BY_CMD.format(cmd=command,
opts=req_opts))
# Validate the required options for the failover command
if command == 'failover':
# --discover-slaves-login is invalid (as it will require a master)
# instead --slaves needs to be used.
if opt.discover:
invalid_opt = '--discover-slaves-login'
parser.error(PARSE_ERR_OPT_INVALID_CMD_TIP.format(
opt=invalid_opt, cmd=command, opt_tip='--slaves'))
# --master will be ignored
if opt.master:
print(WARN_OPT_NOT_REQUIRED.format(opt='--master', cmd=command))
opt.master = None
# --ping only used by 'health' command
if opt.ping and not command == 'health':
print(WARN_OPT_NOT_REQUIRED_ONLY_FOR.format(opt='--ping', cmd=command,
only_cmd='health'))
opt.ping = None
# --exec-after only used by 'failover' or 'switchover' command
if opt.exec_after and command not in ['switchover', 'failover']:
only_used_cmds = 'failover or switchover'
print(WARN_OPT_NOT_REQUIRED_ONLY_FOR.format(opt='--exec-after',
cmd=command,
only_cmd=only_used_cmds))
opt.exec_after = None
# --exec-before only used by 'failover' or 'switchover' command
if opt.exec_before and command not in ['switchover', 'failover']:
only_used_cmds = 'failover or switchover'
print(WARN_OPT_NOT_REQUIRED_ONLY_FOR.format(opt='--exec-before',
cmd=command,
only_cmd=only_used_cmds))
opt.exec_before = None
# --new-master only required for 'switchover' command
if opt.new_master and command != 'switchover':
print(WARN_OPT_NOT_REQUIRED_ONLY_FOR.format(opt='--new-master',
cmd=command,
only_cmd='switchover'))
opt.new_master = None
# --candidates only used by 'failover' or 'elect' command
if opt.candidates and command not in ['elect', 'failover']:
only_used_cmds = 'failover or elect'
print(WARN_OPT_NOT_REQUIRED_ONLY_FOR.format(opt='--candidates',
cmd=command,
only_cmd=only_used_cmds))
opt.candidates = None
# --format only used by 'health' or 'gtid' command
if opt.format and command not in ['health', 'gtid']:
only_used_cmds = 'health or gtid'
print(WARN_OPT_NOT_REQUIRED_ONLY_FOR.format(opt='--format',
cmd=command,
only_cmd=only_used_cmds))
opt.format = None
# Parse the --new-master connection string
if opt.new_master:
try:
new_master_val = parse_connection(opt.new_master, None, opt)
except FormatError:
_, err, _ = sys.exc_info()
parser.error("New master connection values invalid: "
"{0}.".format(err))
except UtilError:
_, err, _ = sys.exc_info()
parser.error("New master connection values invalid: "
"{0}.".format(err.errmsg))
else:
new_master_val = None
# Parse the master, slaves, and candidates connection parameters
try:
master_val, slaves_val, candidates_val = parse_topology_connections(
opt)
except UtilRplError:
_, e, _ = sys.exc_info()
print("ERROR: {0}".format(e.errmsg))
sys.exit(1)
# Check hostname alias
if new_master_val:
if check_hostname_alias(master_val, new_master_val):
master = Server({'conn_info': master_val})
new_master = Server({'conn_info': new_master_val})
parser.error(ERROR_SAME_MASTER.format(
n_master_host=new_master.host,
n_master_port=new_master.port,
master_host=master.host,
master_port=master.port))
if master_val:
for slave_val in slaves_val:
if check_hostname_alias(master_val, slave_val):
master = Server({'conn_info': master_val})
slave = Server({'conn_info': slave_val})
msg = ERROR_MASTER_IN_SLAVES.format(master_host=master.host,
master_port=master.port,
slaves_candidates=SLAVES,
slave_host=slave.host,
slave_port=slave.port)
parser.error(msg)
for cand_val in candidates_val:
if check_hostname_alias(master_val, cand_val):
master = Server({'conn_info': master_val})
candidate = Server({'conn_info': cand_val})
msg = ERROR_MASTER_IN_SLAVES.format(
master_host=master.host,
master_port=master.port,
slaves_candidates=CANDIDATES,
slave_host=candidate.host,
slave_port=candidate.port)
parser.error(msg)
# Create dictionary of options
options = {
'new_master': new_master_val,
'candidates': candidates_val,
'ping': 3 if opt.ping is None else opt.ping,
'format': opt.format,
'verbosity': 0 if opt.verbosity is None else opt.verbosity,
'before': opt.exec_before,
'after': opt.exec_after,
'force': opt.force,
'max_position': opt.max_position,
'max_delay': opt.max_delay,
'discover': opt.discover,
'timeout': int(opt.timeout),
'demote': opt.demote,
'quiet': opt.quiet,
'logging': opt.log_file is not None,
'log_file': opt.log_file,
'no_health': opt.no_health,
'rpl_user': opt.rpl_user,
'script_threshold': opt.script_threshold,
}
# Check if script files exist and are executable and warn users if they
# are not.
script_opts = ['after', 'before']
for key in script_opts:
parameter_val = options[key]
check_script_option(parser, parameter_val)
# Add ssl options to options instead of connection.
options.update(get_ssl_dict(opt))
# If command = HEALTH, turn on --force
if command == 'health' or command == 'gtid':
options['force'] = True
# Purge log file of old data
if opt.log_file is not None and not purge_log(opt.log_file, opt.log_age):
parser.error("Error purging log file.")
# Warn user about script threshold checking.
if opt.script_threshold:
print(SCRIPT_THRESHOLD_WARNING)
# Setup log file
try:
logging.basicConfig(filename=opt.log_file, level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
datefmt=_DATE_FORMAT)
except IOError:
_, e, _ = sys.exc_info()
parser.error("Error opening log file: %s" % str(e.args[1]))
# Log MySQL Utilities version string
if opt.log_file:
logging.info(MSG_UTILITIES_VERSION.format(utility=program,
version=VERSION_STRING))
try:
rpl_cmds = RplCommands(master_val, slaves_val, options)
rpl_cmds.execute_command(command, options)
except UtilError:
_, e, _ = sys.exc_info()
print("ERROR: {0}".format(e.errmsg))
sys.exit(1)
sys.exit(0)
|