Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

181
database/mysql/bin/my.ini Normal file
View File

@@ -0,0 +1,181 @@
# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# You can copy this file to
# /xampp/mysql/bin/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is /xampp/mysql/data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
# password = your_password
port = 3306
socket = "/xampp/mysql/mysql.sock"
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port= 3306
socket = "/xampp/mysql/mysql.sock"
basedir = "/xampp/mysql"
tmpdir = "/xampp/tmp"
datadir = "/xampp/mysql/data"
pid_file = "mysql.pid"
# enable-named-pipe
key_buffer = 16M
max_allowed_packet = 1M
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log_error = "mysql_error.log"
# Change here for bind listening
# bind-address="127.0.0.1"
# bind-address = ::1 # for ipv6
# Where do all the plugins live
plugin_dir = "/xampp/mysql/lib/plugin/"
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
# commented in by lampp security
#skip-networking
#skip-federated
# Replication Master Server (default)
# binary logging is required for replication
# log-bin deactivated by default since XAMPP 1.4.11
#log-bin=mysql-bin
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Point the following paths to different dedicated disks
#tmpdir = "/xampp/tmp"
#log-update = /path-to-dedicated-directory/hostname
# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000
# Comment the following if you are using InnoDB tables
#skip-innodb
innodb_data_home_dir = "/xampp/mysql/data"
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = "/xampp/mysql/data"
#innodb_log_arch_dir = "/xampp/mysql/data"
## You can set .._buffer_pool_size up to 50 - 80 %
## of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
## Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
## UTF 8 Settings
#init-connect=\'SET NAMES utf8\'
#collation_server=utf8_unicode_ci
#character_set_server=utf8
#skip-character-set-client-handshake
#character_sets-dir="/xampp/mysql/share/charsets"
sql_mode=NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION
log_bin_trust_function_creators = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,697 @@
#!/usr/bin/env python
from __future__ import division
from optparse import OptionParser
import collections
import signal
import os
import stat
import sys
import re
import subprocess
import logging
import logging.handlers
import time
import datetime
import shutil
import traceback
import tempfile
import MySQLdb
import MySQLdb.connections
from MySQLdb import OperationalError, ProgrammingError
logger = None
opts = None
rocksdb_files = ['MANIFEST', 'CURRENT', 'OPTIONS']
rocksdb_data_suffix = '.sst'
rocksdb_wal_suffix = '.log'
exclude_files = ['master.info', 'relay-log.info', 'worker-relay-log.info',
'auto.cnf', 'gaplock.log', 'ibdata', 'ib_logfile', '.trash']
wdt_bin = 'wdt'
def is_manifest(fname):
for m in rocksdb_files:
if fname.startswith(m):
return True
return False
class Writer(object):
a = None
def __init__(self):
a = None
class StreamWriter(Writer):
stream_cmd= ''
def __init__(self, stream_option, direct = 0):
super(StreamWriter, self).__init__()
if stream_option == 'tar':
self.stream_cmd= 'tar chf -'
elif stream_option == 'xbstream':
self.stream_cmd= 'xbstream -c'
if direct:
self.stream_cmd = self.stream_cmd + ' -d'
else:
raise Exception("Only tar or xbstream is supported as streaming option.")
def write(self, file_name):
rc= os.system(self.stream_cmd + " " + file_name)
if (rc != 0):
raise Exception("Got error on stream write: " + str(rc) + " " + file_name)
class MiscFilesProcessor():
datadir = None
wildcard = r'.*\.[frm|MYD|MYI|MAD|MAI|MRG|TRG|TRN|ARM|ARZ|CSM|CSV|opt|par]'
regex = None
start_backup_time = None
skip_check_frm_timestamp = None
def __init__(self, datadir, skip_check_frm_timestamp, start_backup_time):
self.datadir = datadir
self.regex = re.compile(self.wildcard)
self.skip_check_frm_timestamp = skip_check_frm_timestamp
self.start_backup_time = start_backup_time
def process_db(self, db):
# do nothing
pass
def process_file(self, path):
# do nothing
pass
def check_frm_timestamp(self, fname, path):
if not self.skip_check_frm_timestamp and fname.endswith('.frm'):
if os.path.getmtime(path) > self.start_backup_time:
logger.error('FRM file %s was updated after starting backups. '
'Schema could have changed and the resulting copy may '
'not be valid. Aborting. '
'(backup time: %s, file modifled time: %s)',
path, datetime.datetime.fromtimestamp(self.start_backup_time).strftime('%Y-%m-%d %H:%M:%S'),
datetime.datetime.fromtimestamp(os.path.getmtime(path)).strftime('%Y-%m-%d %H:%M:%S'))
raise Exception("Inconsistent frm file timestamp");
def process(self):
os.chdir(self.datadir)
for db in self.get_databases():
logger.info("Starting MySQL misc file traversal from database %s..", db)
self.process_db(db)
for f in self.get_files(db):
if self.match(f):
rel_path = os.path.join(db, f)
self.check_frm_timestamp(f, rel_path)
self.process_file(rel_path)
logger.info("Traversing misc files from data directory..")
for f in self.get_files(""):
should_skip = False
for e in exclude_files:
if f.startswith(e) or f.endswith(e):
logger.info("Skipping %s", f)
should_skip = True
break
if not should_skip:
self.process_file(f)
def match(self, filename):
if self.regex.match(filename):
return True
else:
return False
def get_databases(self):
dbs = []
dirs = [ d for d in os.listdir(self.datadir) \
if not os.path.isfile(os.path.join(self.datadir,d))]
for db in dirs:
if not db.startswith('.') and not self._is_socket(db) and not db == "#rocksdb":
dbs.append(db)
return dbs
def get_files(self, db):
dbdir = self.datadir + "/" + db
return [ f for f in os.listdir(dbdir) \
if os.path.isfile(os.path.join(dbdir,f))]
def _is_socket(self, item):
mode = os.stat(os.path.join(self.datadir, item)).st_mode
if stat.S_ISSOCK(mode):
return True
return False
class MySQLBackup(MiscFilesProcessor):
writer = None
def __init__(self, datadir, writer, skip_check_frm_timestamp, start_backup_time):
MiscFilesProcessor.__init__(self, datadir, skip_check_frm_timestamp, start_backup_time)
self.writer = writer
def process_file(self, fname): # overriding base class
self.writer.write(fname)
class MiscFilesLinkCreator(MiscFilesProcessor):
snapshot_dir = None
def __init__(self, datadir, snapshot_dir, skip_check_frm_timestamp, start_backup_time):
MiscFilesProcessor.__init__(self, datadir, skip_check_frm_timestamp, start_backup_time)
self.snapshot_dir = snapshot_dir
def process_db(self, db):
snapshot_sub_dir = os.path.join(self.snapshot_dir, db)
os.makedirs(snapshot_sub_dir)
def process_file(self, path):
dst_path = os.path.join(self.snapshot_dir, path)
os.link(path, dst_path)
# RocksDB backup
class RocksDBBackup():
source_dir = None
writer = None
# sst files sent in this backup round
sent_sst = {}
# target sst files in this backup round
target_sst = {}
# sst files sent in all backup rounds
total_sent_sst= {}
# sum of sst file size sent in this backup round
sent_sst_size = 0
# sum of target sst file size in this backup round
# if sent_sst_size becomes equal to target_sst_size,
# it means the backup round finished backing up all sst files
target_sst_size = 0
# sum of all sst file size sent all backup rounds
total_sent_sst_size= 0
# sum of all target sst file size from all backup rounds
total_target_sst_size = 0
show_progress_size_interval= 1073741824 # 1GB
wal_files= []
manifest_files= []
finished= False
def __init__(self, source_dir, writer, prev):
self.source_dir = source_dir
self.writer = writer
os.chdir(self.source_dir)
self.init_target_files(prev)
def init_target_files(self, prev):
sst = {}
self.sent_sst = {}
self.target_sst= {}
self.total_sent_sst = {}
self.sent_sst_size = 0
self.target_sst_size = 0
self.total_sent_sst_size= 0
self.total_target_sst_size= 0
self.wal_files= []
self.manifest_files= []
for f in os.listdir(self.source_dir):
if f.endswith(rocksdb_data_suffix):
# exactly the same file (same size) was sent in previous backup rounds
if prev is not None and f in prev.total_sent_sst and int(os.stat(f).st_size) == prev.total_sent_sst[f]:
continue
sst[f]= int(os.stat(f).st_size)
self.target_sst_size = self.target_sst_size + os.stat(f).st_size
elif is_manifest(f):
self.manifest_files.append(f)
elif f.endswith(rocksdb_wal_suffix):
self.wal_files.append(f)
self.target_sst= collections.OrderedDict(sorted(sst.items()))
if prev is not None:
self.total_sent_sst = prev.total_sent_sst
self.total_sent_sst_size = prev.total_sent_sst_size
self.total_target_sst_size = self.target_sst_size + prev.total_sent_sst_size
else:
self.total_target_sst_size = self.target_sst_size
def do_backup_single(self, fname):
self.writer.write(fname)
os.remove(fname)
def do_backup_sst(self, fname, size):
self.do_backup_single(fname)
self.sent_sst[fname]= size
self.total_sent_sst[fname]= size
self.sent_sst_size = self.sent_sst_size + size
self.total_sent_sst_size = self.total_sent_sst_size + size
def do_backup_manifest(self):
for f in self.manifest_files:
self.do_backup_single(f)
def do_backup_wal(self):
for f in self.wal_files:
self.do_backup_single(f)
# this is the last snapshot round. backing up all the rest files
def do_backup_final(self):
logger.info("Backup WAL..")
self.do_backup_wal()
logger.info("Backup Manifest..")
self.do_backup_manifest()
self.do_cleanup()
self.finished= True
def do_cleanup(self):
shutil.rmtree(self.source_dir)
logger.info("Cleaned up checkpoint from %s", self.source_dir)
def do_backup_until(self, time_limit):
logger.info("Starting backup from snapshot: target files %d", len(self.target_sst))
start_time= time.time()
last_progress_time= start_time
progress_size= 0
for fname, size in self.target_sst.iteritems():
self.do_backup_sst(fname, size)
progress_size= progress_size + size
elapsed_seconds = time.time() - start_time
progress_seconds = time.time() - last_progress_time
if self.should_show_progress(size):
self.show_progress(progress_size, progress_seconds)
progress_size=0
last_progress_time= time.time()
if elapsed_seconds > time_limit and self.has_sent_all_sst() is False:
logger.info("Snapshot round finished. Elapsed Time: %5.2f. Remaining sst files: %d",
elapsed_seconds, len(self.target_sst) - len(self.sent_sst))
self.do_cleanup()
break;
if self.has_sent_all_sst():
self.do_backup_final()
return self
def should_show_progress(self, size):
if int(self.total_sent_sst_size/self.show_progress_size_interval) > int((self.total_sent_sst_size-size)/self.show_progress_size_interval):
return True
else:
return False
def show_progress(self, size, seconds):
logger.info("Backup Progress: %5.2f%% Sent %6.2f GB of %6.2f GB data, Transfer Speed: %6.2f MB/s",
self.total_sent_sst_size*100/self.total_target_sst_size,
self.total_sent_sst_size/1024/1024/1024,
self.total_target_sst_size/1024/1024/1024,
size/seconds/1024/1024)
def print_backup_report(self):
logger.info("Sent %6.2f GB of sst files, %d files in total.",
self.total_sent_sst_size/1024/1024/1024,
len(self.total_sent_sst))
def has_sent_all_sst(self):
if self.sent_sst_size == self.target_sst_size:
return True
return False
class MySQLUtil:
@staticmethod
def connect(user, password, port, socket=None):
if socket:
dbh = MySQLdb.Connect(user=user,
passwd=password,
unix_socket=socket)
else:
dbh = MySQLdb.Connect(user=user,
passwd=password,
port=port,
host="127.0.0.1")
return dbh
@staticmethod
def create_checkpoint(dbh, checkpoint_dir):
sql = ("SET GLOBAL rocksdb_create_checkpoint='{0}'"
.format(checkpoint_dir))
cur= dbh.cursor()
cur.execute(sql)
cur.close()
@staticmethod
def get_datadir(dbh):
sql = "SELECT @@datadir"
cur = dbh.cursor()
cur.execute(sql)
row = cur.fetchone()
return row[0]
@staticmethod
def is_directio_enabled(dbh):
sql = "SELECT @@global.rocksdb_use_direct_reads"
cur = dbh.cursor()
cur.execute(sql)
row = cur.fetchone()
return row[0]
class BackupRunner:
datadir = None
start_backup_time = None
def __init__(self, datadir):
self.datadir = datadir
self.start_backup_time = time.time()
def start_backup_round(self, backup_round, prev_backup):
def signal_handler(*args):
logger.info("Got signal. Exit")
if b is not None:
logger.info("Cleaning up snapshot directory..")
b.do_cleanup()
sys.exit(1)
b = None
try:
signal.signal(signal.SIGINT, signal_handler)
w = None
if not opts.output_stream:
raise Exception("Currently only streaming backup is supported.")
snapshot_dir = opts.checkpoint_directory + '/' + str(backup_round)
dbh = MySQLUtil.connect(opts.mysql_user,
opts.mysql_password,
opts.mysql_port,
opts.mysql_socket)
direct = MySQLUtil.is_directio_enabled(dbh)
logger.info("Direct I/O: %d", direct)
w = StreamWriter(opts.output_stream, direct)
if not self.datadir:
self.datadir = MySQLUtil.get_datadir(dbh)
logger.info("Set datadir: %s", self.datadir)
logger.info("Creating checkpoint at %s", snapshot_dir)
MySQLUtil.create_checkpoint(dbh, snapshot_dir)
logger.info("Created checkpoint at %s", snapshot_dir)
b = RocksDBBackup(snapshot_dir, w, prev_backup)
return b.do_backup_until(opts.checkpoint_interval)
except Exception as e:
logger.error(e)
logger.error(traceback.format_exc())
if b is not None:
logger.info("Cleaning up snapshot directory.")
b.do_cleanup()
sys.exit(1)
def backup_mysql(self):
try:
w = None
if opts.output_stream:
w = StreamWriter(opts.output_stream)
else:
raise Exception("Currently only streaming backup is supported.")
b = MySQLBackup(self.datadir, w, opts.skip_check_frm_timestamp,
self.start_backup_time)
logger.info("Taking MySQL misc backups..")
b.process()
logger.info("MySQL misc backups done.")
except Exception as e:
logger.error(e)
logger.error(traceback.format_exc())
sys.exit(1)
class WDTBackup:
datadir = None
start_backup_time = None
def __init__(self, datadir):
self.datadir = datadir
self.start_backup_time = time.time()
def cleanup(self, snapshot_dir, server_log):
if server_log:
server_log.seek(0)
logger.info("WDT server log:")
logger.info(server_log.read())
server_log.close()
if snapshot_dir:
logger.info("Cleaning up snapshot dir %s", snapshot_dir)
shutil.rmtree(snapshot_dir)
def backup_with_timeout(self, backup_round):
def signal_handler(*args):
logger.info("Got signal. Exit")
self.cleanup(snapshot_dir, server_log)
sys.exit(1)
logger.info("Starting backup round %d", backup_round)
snapshot_dir = None
server_log = None
try:
signal.signal(signal.SIGINT, signal_handler)
# create rocksdb snapshot
snapshot_dir = os.path.join(opts.checkpoint_directory, str(backup_round))
dbh = MySQLUtil.connect(opts.mysql_user,
opts.mysql_password,
opts.mysql_port,
opts.mysql_socket)
logger.info("Creating checkpoint at %s", snapshot_dir)
MySQLUtil.create_checkpoint(dbh, snapshot_dir)
logger.info("Created checkpoint at %s", snapshot_dir)
# get datadir if not provided
if not self.datadir:
self.datadir = MySQLUtil.get_datadir(dbh)
logger.info("Set datadir: %s", self.datadir)
# create links for misc files
link_creator = MiscFilesLinkCreator(self.datadir, snapshot_dir,
opts.skip_check_frm_timestamp,
self.start_backup_time)
link_creator.process()
current_path = os.path.join(opts.backupdir, "CURRENT")
# construct receiver cmd, using the data directory as recovery-id.
# we delete the current file because it is not append-only, therefore not
# resumable.
remote_cmd = (
"ssh {0} rm -f {1}; "
"{2} -directory {3} -enable_download_resumption "
"-recovery_id {4} -start_port 0 -abort_after_seconds {5} {6}"
).format(opts.destination,
current_path,
wdt_bin,
opts.backupdir,
self.datadir,
opts.checkpoint_interval,
opts.extra_wdt_receiver_options)
logger.info("WDT remote cmd %s", remote_cmd)
server_log = tempfile.TemporaryFile()
remote_process = subprocess.Popen(remote_cmd.split(),
stdout=subprocess.PIPE,
stderr=server_log)
wdt_url = remote_process.stdout.readline().strip()
if not wdt_url:
raise Exception("Unable to get connection url from wdt receiver")
sender_cmd = (
"{0} -connection_url \'{1}\' -directory {2} -app_name=myrocks "
"-avg_mbytes_per_sec {3} "
"-enable_download_resumption -abort_after_seconds {4} {5}"
).format(wdt_bin,
wdt_url,
snapshot_dir,
opts.avg_mbytes_per_sec,
opts.checkpoint_interval,
opts.extra_wdt_sender_options)
sender_status = os.system(sender_cmd) >> 8
remote_status = remote_process.wait()
self.cleanup(snapshot_dir, server_log)
# TODO: handle retryable and non-retyable errors differently
return (sender_status == 0 and remote_status == 0)
except Exception as e:
logger.error(e)
logger.error(traceback.format_exc())
self.cleanup(snapshot_dir, server_log)
sys.exit(1)
def backup_using_wdt():
if not opts.destination:
logger.error("Must provide remote destination when using WDT")
sys.exit(1)
# TODO: detect whether WDT is installed
logger.info("Backing up myrocks to %s using WDT", opts.destination)
wdt_backup = WDTBackup(opts.datadir)
finished = False
backup_round = 1
while not finished:
start_time = time.time()
finished = wdt_backup.backup_with_timeout(backup_round)
end_time = time.time()
duration_seconds = end_time - start_time
if (not finished) and (duration_seconds < opts.checkpoint_interval):
# round finished before timeout
sleep_duration = (opts.checkpoint_interval - duration_seconds)
logger.info("Sleeping for %f seconds", sleep_duration)
time.sleep(sleep_duration)
backup_round = backup_round + 1
logger.info("Finished myrocks backup using WDT")
def init_logger():
global logger
logger = logging.getLogger('myrocks_hotbackup')
logger.setLevel(logging.INFO)
h1= logging.StreamHandler(sys.stderr)
f = logging.Formatter("%(asctime)s.%(msecs)03d %(levelname)s %(message)s",
"%Y-%m-%d %H:%M:%S")
h1.setFormatter(f)
logger.addHandler(h1)
backup_wdt_usage = ("Backup using WDT: myrocks_hotbackup "
"--user=root --password=pw --stream=wdt "
"--checkpoint_dir=<directory where temporary backup hard links "
"are created> --destination=<remote host name> --backup_dir="
"<remote directory name>. This has to be executed at the src "
"host.")
backup_usage= "Backup: set -o pipefail; myrocks_hotbackup --user=root --password=pw --port=3306 --checkpoint_dir=<directory where temporary backup hard links are created> | ssh -o NoneEnabled=yes remote_server 'tar -xi -C <directory on remote server where backups will be sent>' . You need to execute backup command on a server where you take backups."
move_back_usage= "Move-Back: myrocks_hotbackup --move_back --datadir=<dest mysql datadir> --rocksdb_datadir=<dest rocksdb datadir> --rocksdb_waldir=<dest rocksdb wal dir> --backup_dir=<where backup files are stored> . You need to execute move-back command on a server where backup files are sent."
def parse_options():
global opts
parser = OptionParser(usage = "\n\n" + backup_usage + "\n\n" + \
backup_wdt_usage + "\n\n" + move_back_usage)
parser.add_option('-i', '--interval', type='int', dest='checkpoint_interval',
default=300,
help='Number of seconds to renew checkpoint')
parser.add_option('-c', '--checkpoint_dir', type='string', dest='checkpoint_directory',
default='/data/mysql/backup/snapshot',
help='Local directory name where checkpoints will be created.')
parser.add_option('-d', '--datadir', type='string', dest='datadir',
default=None,
help='backup mode: src MySQL datadir. move_back mode: dest MySQL datadir')
parser.add_option('-s', '--stream', type='string', dest='output_stream',
default='tar',
help='Setting streaming backup options. Currently tar, WDT '
'and xbstream are supported. Default is tar')
parser.add_option('--destination', type='string', dest='destination',
default='',
help='Remote server name. Only used for WDT mode so far.')
parser.add_option('--avg_mbytes_per_sec', type='int',
dest='avg_mbytes_per_sec',
default=500,
help='Average backup rate in MBytes/sec. WDT only.')
parser.add_option('--extra_wdt_sender_options', type='string',
dest='extra_wdt_sender_options',
default='',
help='Extra options for WDT sender')
parser.add_option('--extra_wdt_receiver_options', type='string',
dest='extra_wdt_receiver_options',
default='',
help='Extra options for WDT receiver')
parser.add_option('-u', '--user', type='string', dest='mysql_user',
default='root',
help='MySQL user name')
parser.add_option('-p', '--password', type='string', dest='mysql_password',
default='',
help='MySQL password name')
parser.add_option('-P', '--port', type='int', dest='mysql_port',
default=3306,
help='MySQL port number')
parser.add_option('-S', '--socket', type='string', dest='mysql_socket',
default=None,
help='MySQL socket path. Takes precedence over --port.')
parser.add_option('-m', '--move_back', action='store_true', dest='move_back',
default=False,
help='Moving MyRocks backup files to proper locations.')
parser.add_option('-r', '--rocksdb_datadir', type='string', dest='rocksdb_datadir',
default=None,
help='RocksDB target data directory where backup data files will be moved. Must be empty.')
parser.add_option('-w', '--rocksdb_waldir', type='string', dest='rocksdb_waldir',
default=None,
help='RocksDB target data directory where backup wal files will be moved. Must be empty.')
parser.add_option('-b', '--backup_dir', type='string', dest='backupdir',
default=None,
help='backup mode for WDT: Remote directory to store '
'backup. move_back mode: Locations where backup '
'files are stored.')
parser.add_option('-f', '--skip_check_frm_timestamp',
dest='skip_check_frm_timestamp',
action='store_true', default=False,
help='skipping to check if frm files are updated after starting backup.')
parser.add_option('-D', '--debug_signal_file', type='string', dest='debug_signal_file',
default=None,
help='debugging purpose: waiting until the specified file is created')
opts, args = parser.parse_args()
def create_moveback_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
else:
for f in os.listdir(directory):
logger.error("Directory %s has file or directory %s!", directory, f)
raise
def print_move_back_usage():
logger.warning(move_back_usage)
def move_back():
if opts.rocksdb_datadir is None or opts.rocksdb_waldir is None or opts.backupdir is None or opts.datadir is None:
print_move_back_usage()
sys.exit()
create_moveback_dir(opts.datadir)
create_moveback_dir(opts.rocksdb_datadir)
create_moveback_dir(opts.rocksdb_waldir)
os.chdir(opts.backupdir)
for f in os.listdir(opts.backupdir):
if os.path.isfile(os.path.join(opts.backupdir,f)):
if f.endswith(rocksdb_wal_suffix):
shutil.move(f, opts.rocksdb_waldir)
elif f.endswith(rocksdb_data_suffix) or is_manifest(f):
shutil.move(f, opts.rocksdb_datadir)
else:
shutil.move(f, opts.datadir)
else: #directory
if f.endswith('.rocksdb'):
continue
shutil.move(f, opts.datadir)
def start_backup():
logger.info("Starting backup.")
runner = BackupRunner(opts.datadir)
b = None
backup_round= 1
while True:
b = runner.start_backup_round(backup_round, b)
backup_round = backup_round + 1
if b.finished is True:
b.print_backup_report()
logger.info("RocksDB Backup Done.")
break
if opts.debug_signal_file:
while not os.path.exists(opts.debug_signal_file):
logger.info("Waiting until %s is created..", opts.debug_signal_file)
time.sleep(1)
runner.backup_mysql()
logger.info("All Backups Done.")
def main():
parse_options()
init_logger()
if opts.move_back is True:
move_back()
elif opts.output_stream == 'wdt':
backup_using_wdt()
else:
start_backup()
if __name__ == "__main__":
main()

Binary file not shown.

View File

@@ -0,0 +1,284 @@
#!/usr/bin/perl
# -*- cperl -*-
#
# Copyright (c) 2007, 2017, 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-1335 USA
##############################################################################
#
# This script reports various configuration settings that may be needed
# when using the MySQL client library.
#
# This script try to match the shell script version as close as possible,
# but in addition being compatible with ActiveState Perl on Windows.
#
# All unrecognized arguments to this script are passed to mysqld.
#
# NOTE: This script will only be used on Windows until solved how to
# handle -lmariadb ws2_32 advapi32 kernel32 shlwapi crypt32 secur32 and other strings inserted that might contain
# several arguments, possibly with spaces in them.
#
# NOTE: This script was deliberately written to be as close to the shell
# script as possible, to make the maintenance of both in parallel
# easier.
#
##############################################################################
use File::Basename;
use Getopt::Long;
use Cwd;
use strict;
my @exclude_cflags =
qw/DDBUG_OFF DSAFE_MUTEX DUNIV_MUST_NOT_INLINE DFORCE_INIT_OF_VARS
DEXTRA_DEBUG DHAVE_valgrind O O[0-9] xO[0-9] W[-A-Za-z]*
Xa xstrconst xc99=none
unroll2 ip mp restrict/;
my @exclude_libs = qw/lmtmalloc static-libcxa i-static static-intel/;
my $cwd = cwd();
my $basedir;
my $socket = '/tmp/mysql.sock';
my $version = '10.4.32';
sub which
{
my $file = shift;
my $IFS = $^O eq "MSWin32" ? ";" : ":";
foreach my $dir ( split($IFS, $ENV{PATH}) )
{
if ( -f "$dir/$file" or -f "$dir/$file.exe" )
{
return "$dir/$file";
}
}
print STDERR "which: no $file in ($ENV{PATH})\n";
exit 1;
}
# ----------------------------------------------------------------------
# If we can find the given directory relatively to where mysql_config is
# we should use this instead of the incompiled one.
# This is to ensure that this script also works with the binary MySQL
# version
# ----------------------------------------------------------------------
sub fix_path
{
my $default = shift;
my @dirs = @_;
foreach my $dirname ( @dirs )
{
my $path = "$basedir/$dirname";
if ( -d $path )
{
return $path;
}
}
return $default;
}
sub get_full_path
{
my $file = shift;
# if the file is a symlink, try to resolve it
if ( $^O ne "MSWin32" and -l $file )
{
$file = readlink($file);
}
if ( $file =~ m,^/, )
{
# Do nothing, absolute path
}
elsif ( $file =~ m,/, )
{
# Make absolute, and remove "/./" in path
$file = "$cwd/$file";
$file =~ s,/\./,/,g;
}
else
{
# Find in PATH
$file = which($file);
}
return $file;
}
##############################################################################
#
# Form a command line that can handle spaces in paths and arguments
#
##############################################################################
sub quote_options {
my @cmd;
foreach my $opt ( @_ )
{
next unless $opt; # If undefined or empty, just skip
push(@cmd, "\"$opt\""); # Quote argument
}
return join(" ", @cmd);
}
##############################################################################
#
# Main program
#
##############################################################################
my $me = get_full_path($0);
$basedir = dirname(dirname($me)); # Remove "/bin/mysql_config" part
my $ldata = 'C:/Program Files/MariaDB 10.4/data';
my $execdir = 'C:/Program Files/MySQL/bin';
my $bindir = 'C:/Program Files/MySQL/bin';
# ----------------------------------------------------------------------
# If installed, search for the compiled in directory first (might be "lib64")
# ----------------------------------------------------------------------
my $pkglibdir = fix_path('C:/Program Files/MySQL/lib',"libmysql/relwithdebinfo",
"libmysql/release","libmysql/debug","lib/mysql","lib");
my $pkgincludedir = fix_path('C:/Program Files/MySQL/include/mysql', "include/mysql", "include");
# Assume no argument with space in it
my @ldflags = split(" ",'');
my $port;
if ( '0' == 0 ) {
$port = 0;
} else {
$port = '3306';
}
# ----------------------------------------------------------------------
# Create options
# We intentionally add a space to the beginning and end of lib strings, simplifies replace later
# ----------------------------------------------------------------------
my (@lib_opts,@lib_r_opts,@lib_e_opts);
if ( $^O eq "MSWin32" )
{
my $linkpath = "$pkglibdir";
# user32 is only needed for debug or embedded
my @winlibs = ("wsock32.lib","advapi32.lib","user32.lib");
@lib_opts = ("$linkpath/mysqlclient.lib",@winlibs);
@lib_r_opts = @lib_opts;
@lib_e_opts = ("$linkpath/mysqlserver.lib",@winlibs);
}
else
{
my $linkpath = "-L$pkglibdir ";
@lib_opts = ($linkpath,"-lmysqlclient");
@lib_r_opts = ($linkpath,"-lmysqlclient_r");
@lib_e_opts = ($linkpath,"-lmysqld");
}
my $flags;
$flags->{libs} =
[@ldflags,@lib_opts,'','','',''];
$flags->{libs_r} =
[@ldflags,@lib_r_opts,'','-lmariadb ws2_32 advapi32 kernel32 shlwapi crypt32 secur32 ',''];
$flags->{embedded_libs} =
[@ldflags,@lib_e_opts,'','','-lmariadb ws2_32 advapi32 kernel32 shlwapi crypt32 secur32 ','',''];
$flags->{include} = ["-I$pkgincludedir"];
$flags->{cflags} = [@{$flags->{include}},split(" ",'')];
# ----------------------------------------------------------------------
# Remove some options that a client doesn't have to care about
# FIXME until we have a --cxxflags, we need to remove -Xa
# and -xstrconst to make --cflags usable for Sun Forte C++
# ----------------------------------------------------------------------
my $filter = join("|", @exclude_cflags);
my @tmp = @{$flags->{cflags}}; # Copy the flag list
$flags->{cflags} = []; # Clear it
foreach my $cflag ( @tmp )
{
push(@{$flags->{cflags}}, $cflag) unless $cflag =~ m/^($filter)$/o;
}
# Same for --libs(_r)
$filter = join("|", @exclude_libs);
foreach my $lib_type ( "libs","libs_r","embedded_libs" )
{
my @tmp = @{$flags->{$lib_type}}; # Copy the flag list
$flags->{$lib_type} = []; # Clear it
foreach my $lib ( @tmp )
{
push(@{$flags->{$lib_type}}, $lib) unless $lib =~ m/^($filter)$/o;
}
}
my $include = quote_options(@{$flags->{include}});
my $cflags = quote_options(@{$flags->{cflags}});
my $libs = quote_options(@{$flags->{libs}});
my $libs_r = quote_options(@{$flags->{libs_r}});
my $embedded_libs = quote_options(@{$flags->{embedded_libs}});
##############################################################################
#
# Usage information, output if no option is given
#
##############################################################################
sub usage
{
print <<EOF;
Usage: $0 [OPTIONS]
Options:
--cflags [$cflags]
--include [$include]
--libs [$libs]
--libs_r [$libs_r]
--socket [$socket]
--port [$port]
--version [$version]
--libmysqld-libs [$embedded_libs]
EOF
exit 0;
}
@ARGV or usage();
##############################################################################
#
# Get options and output the values
#
##############################################################################
GetOptions(
"cflags" => sub { print "$cflags\n" },
"include" => sub { print "$include\n" },
"libs" => sub { print "$libs\n" },
"libs_r" => sub { print "$libs_r\n" },
"socket" => sub { print "$socket\n" },
"port" => sub { print "$port\n" },
"version" => sub { print "$version\n" },
"embedded-libs|embedded|libmysqld-libs" =>
sub { print "$embedded_libs\n" },
) or usage();
exit 0

View File

@@ -0,0 +1,167 @@
#!/usr/bin/perl
# Copyright (c) 2000, 2017, 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-1335 USA
# Convert given tables in a database to MYISAM
use DBI;
use Getopt::Long;
$opt_help=$opt_version=$opt_verbose=$opt_force=0;
$opt_user=$opt_database=$opt_password=undef;
$opt_host="localhost";
$opt_socket="";
$opt_engine="MYISAM";
$opt_port=0;
$exit_status=0;
GetOptions(
"e|engine|type=s" => \$opt_engine,
"f|force" => \$opt_force,
"help|?" => \$opt_help,
"h|host=s" => \$opt_host,
"p|password=s" => \$opt_password,
"u|user=s" => \$opt_user,
"v|verbose" => \$opt_verbose,
"V|version" => \$opt_version,
"S|socket=s" => \$opt_socket,
"P|port=i" => \$opt_port
) || usage(0);
usage($opt_version) if ($#ARGV < 0 || $opt_help || $opt_version);
$opt_database=shift(@ARGV);
if (grep { /^$opt_engine$/i } qw(HEAP MEMORY BLACKHOLE))
{
print "Converting to '$opt_engine' would delete your data; aborting\n";
exit(1);
}
$connect_opt="";
if ($opt_port)
{
$connect_opt.= ";port=$opt_port";
}
if (length($opt_socket))
{
$connect_opt.=";mysql_socket=$opt_socket";
}
$dbh = DBI->connect("DBI:mysql:$opt_database:${opt_host}$connect_opt",
$opt_user,
$opt_password,
{ PrintError => 0})
|| die "Can't connect to database $opt_database: $DBI::errstr\n";
my @tables;
push(@ARGV, "%") if(!@ARGV);
foreach $pattern (@ARGV)
{
my ($sth,$row);
$sth=$dbh->prepare("SHOW TABLES LIKE ?");
$rv= $sth->execute($pattern);
if(!int($rv))
{
warn "Can't get tables matching '$pattern' from $opt_database; $DBI::errstr\n";
exit(1) unless $opt_force;
}
while (($row = $sth->fetchrow_arrayref))
{
push(@tables, $row->[0]);
}
$sth->finish;
}
print "Converting tables:\n" if ($opt_verbose);
foreach $table (@tables)
{
my ($sth,$row);
# Check if table is already converted
$sth=$dbh->prepare("show table status like '$table'");
if ($sth->execute && ($row = $sth->fetchrow_arrayref))
{
if (uc($row->[1]) eq uc($opt_engine))
{
print "$table already uses the '$opt_engine' engine; Ignored\n";
next;
}
}
print "converting $table\n" if ($opt_verbose);
$table=~ s/`/``/g;
if (!$dbh->do("ALTER TABLE `$table` ENGINE=$opt_engine"))
{
print STDERR "Can't convert $table: Error $DBI::errstr\n";
exit(1) if (!$opt_force);
$exit_status=1;
}
}
$dbh->disconnect;
exit($exit_status);
sub usage
{
my($version)=shift;
print "$0 version 1.1\n";
exit(0) if ($version);
print <<EOF;
Conversion of a MariaDB tables to other storage engines
Usage: $0 database [table[ table ...]]
If no tables has been specifed, all tables in the database will be converted.
You can also use wildcards, ie "my%"
The following options are available:
-f, --force
Continue even if there is some error.
-?, --help
Shows this help
-e, --engine=ENGINE
Converts tables to the given storage engine (Default: $opt_engine)
-h, --host=HOST
Host name where the database server is located. (Default: $opt_host)
-p, --password=PASSWORD
Password for the current user.
-P, --port=PORT
TCP/IP port to connect to if host is not "localhost".
-S, --socket=SOCKET
Socket to connect with.
-u, --user=USER
User name to log into the SQL server.
-v, --verbose
This is a test specific option that is only used when debugging a test.
Print more information about what is going on.
-V, --version
Shows the version of this program.
EOF
exit(1);
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,960 @@
#!/usr/bin/perl
# Copyright (c) 2000, 2017, Oracle and/or its affiliates.
# Copyright (c) 2010, 2017, MariaDB Corporation
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1335 USA
use Getopt::Long;
use POSIX qw(strftime getcwd);
use File::Path qw(mkpath);
$|=1;
$VER="3.0";
my @defaults_options; # Leading --no-defaults, --defaults-file, etc.
$opt_example = 0;
$opt_help = 0;
$opt_log = undef();
$opt_mysqladmin = "C:/Program Files/MySQL/bin/mysqladmin";
$opt_mysqld = "./bin/mysqld";
$opt_no_log = 0;
$opt_password = undef();
$opt_tcp_ip = 0;
$opt_user = "root";
$opt_version = 0;
$opt_silent = 0;
$opt_verbose = 0;
$opt_wsrep_new_cluster = 0;
my $my_print_defaults_exists= 1;
my $logdir= undef();
my ($mysqld, $mysqladmin, $groupids, $homedir, $my_progname);
$homedir = $ENV{HOME};
$my_progname = $0;
$my_progname =~ s/.*[\/]//;
if (defined($ENV{UMASK})) {
my $UMASK = $ENV{UMASK};
my $m;
my $fmode = "0640";
if(($UMASK =~ m/[^0246]/) || ($UMASK =~ m/^[^0]/) || (length($UMASK) != 4)) {
printf("UMASK must be a 3-digit mode with an additional leading 0 to indicate octal.\n");
printf("The first digit will be corrected to 6, the others may be 0, 2, 4, or 6.\n"); }
else {
$fmode= substr $UMASK, 2, 2;
$fmode= "06${fmode}"; }
if($fmode != $UMASK) {
printf("UMASK corrected from $UMASK to $fmode ...\n"); }
$fmode= oct($fmode);
umask($fmode);
}
main();
####
#### main sub routine
####
sub main
{
my $flag_exit= 0;
if (!defined(my_which(my_print_defaults)))
{
# We can't throw out yet, since --version, --help, or --example may
# have been given
print "WARNING: my_print_defaults command not found.\n";
print "Please make sure you have this command available and\n";
print "in your path. The command is available from the latest\n";
print "MariaDB distribution.\n";
$my_print_defaults_exists= 0;
}
# Remove leading defaults options from @ARGV
while (@ARGV > 0)
{
last unless $ARGV[0] =~
/^--(?:no-defaults$|(?:defaults-file|defaults-extra-file)=)/;
push @defaults_options, (shift @ARGV);
}
foreach (@defaults_options)
{
$_ = quote_shell_word($_);
}
# Add [mysqld_multi] options to front of @ARGV, ready for GetOptions()
unshift @ARGV, defaults_for_group('mysqld_multi');
# We've already handled --no-defaults, --defaults-file, etc.
if (!GetOptions("help", "example", "version", "mysqld=s", "mysqladmin=s",
"user=s", "password=s", "log=s", "no-log",
"tcp-ip", "silent", "verbose", "wsrep-new-cluster"))
{
$flag_exit= 1;
}
usage() if ($opt_help);
if ($opt_verbose && $opt_silent)
{
print "Both --verbose and --silent have been given. Some of the warnings ";
print "will be disabled\nand some will be enabled.\n\n";
}
init_log() if (!defined($opt_log));
$groupids = $ARGV[1];
if ($opt_version)
{
print "$my_progname version $VER by Jani Tolonen\n";
exit(0);
}
example() if ($opt_example);
if ($flag_exit)
{
print "Error with an option, see $my_progname --help for more info.\n";
exit(1);
}
if (!defined(my_which(my_print_defaults)))
{
print "ABORT: Can't find command 'my_print_defaults'.\n";
print "This command is available from the latest MariaDB\n";
print "distribution. Please make sure you have the command\n";
print "in your PATH.\n";
exit(1);
}
usage() if (!defined($ARGV[0]) ||
(!($ARGV[0] =~ m/^start$/i) &&
!($ARGV[0] =~ m/^stop$/i) &&
!($ARGV[0] =~ m/^reload$/i) &&
!($ARGV[0] =~ m/^report$/i)));
if (!$opt_no_log)
{
w2log("$my_progname log file version $VER; run: ",
"$opt_log", 1, 0);
}
else
{
print "$my_progname log file version $VER; run: ";
print strftime "%a %b %e %H:%M:%S %Y", localtime;
print "\n";
}
if (($ARGV[0] =~ m/^start$/i) || ($ARGV[0] =~ m/^reload$/i))
{
if (!defined(($mysqld= my_which($opt_mysqld))) && $opt_verbose)
{
print "WARNING: Couldn't find the default mysqld binary.\n";
print "Tried: $opt_mysqld\n";
print "This is OK, if you are using option \"mysqld=...\" in ";
print "groups [mysqldN] separately for each.\n\n";
}
if ($ARGV[0] =~ m/^start$/i) {
start_mysqlds();
} elsif ($ARGV[0] =~ m/^reload$/i) {
reload_mysqlds();
}
}
else
{
if (!defined(($mysqladmin= my_which($opt_mysqladmin))) && $opt_verbose)
{
print "WARNING: Couldn't find the default mysqladmin binary.\n";
print "Tried: $opt_mysqladmin\n";
print "This is OK, if you are using option \"mysqladmin=...\" in ";
print "groups [mysqldN] separately for each.\n\n";
}
if ($ARGV[0] =~ m/^report$/i)
{
report_mysqlds();
}
else
{
stop_mysqlds();
}
}
}
#
# Quote word for shell
#
sub quote_shell_word
{
my ($option)= @_;
$option =~ s!([^\w=./-])!\\$1!g;
return $option;
}
####
#### get options for a group
####
sub defaults_for_group
{
my ($group) = @_;
return () unless $my_print_defaults_exists;
my $com= join ' ', 'my_print_defaults', @defaults_options, $group;
my @defaults = `$com`;
chomp @defaults;
return @defaults;
}
####
#### Init log file. Check for appropriate place for log file, in the following
#### order: my_print_defaults mysqld datadir, C:/Program Files/MySQL/share
####
sub init_log
{
foreach my $opt (defaults_for_group('--mysqld'))
{
if ($opt =~ m/^--datadir=(.*)/ && -d "$1" && -w "$1")
{
$logdir= $1;
}
}
if (!defined($logdir))
{
$logdir= "C:/Program Files/MySQL/share" if (-d "C:/Program Files/MySQL/share" && -w "C:/Program Files/MySQL/share");
}
if (!defined($logdir))
{
# Log file was not specified and we could not log to a standard place,
# so log file be disabled for now.
if (!$opt_silent)
{
print "WARNING: Log file disabled. Maybe directory or file isn't writable?\n";
}
$opt_no_log= 1;
}
else
{
$opt_log= "$logdir/mysqld_multi.log";
}
}
####
#### Report living and not running MariaDB servers
####
sub report_mysqlds
{
my (@groups, $com, $i, @options, $pec);
print "Reporting MariaDB servers\n";
if (!$opt_no_log)
{
w2log("\nReporting MariaDB servers","$opt_log",0,0);
}
@groups = &find_groups($groupids);
for ($i = 0; defined($groups[$i]); $i++)
{
$com= get_mysqladmin_options($i, @groups);
$com.= " ping >> /dev/null 2>&1";
system($com);
$pec = $? >> 8;
if ($pec)
{
print "MariaDB server from group: $groups[$i] is not running\n";
if (!$opt_no_log)
{
w2log("MariaDB server from group: $groups[$i] is not running",
"$opt_log", 0, 0);
}
}
else
{
print "MariaDB server from group: $groups[$i] is running\n";
if (!$opt_no_log)
{
w2log("MariaDB server from group: $groups[$i] is running",
"$opt_log", 0, 0);
}
}
}
if (!$i)
{
print "No groups to be reported (check your GNRs)\n";
if (!$opt_no_log)
{
w2log("No groups to be reported (check your GNRs)", "$opt_log", 0, 0);
}
}
}
####
#### start multiple servers
####
sub start_mysqlds()
{
my (@groups, $com, $tmp, $i, @options, $j, $mysqld_found, $suffix_found, $info_sent);
$suffix_found= 0;
if (!$opt_no_log)
{
w2log("\nStarting MariaDB servers\n","$opt_log",0,0);
}
else
{
print "\nStarting MariaDB servers\n";
}
@groups = &find_groups($groupids);
for ($i = 0; defined($groups[$i]); $i++)
{
@options = defaults_for_group($groups[$i]);
$basedir_found= 0; # The default
$mysqld_found= 1; # The default
$mysqld_found= 0 if (!length($mysqld));
$com= "$mysqld";
for ($j = 0, $tmp= ""; defined($options[$j]); $j++)
{
if ("--datadir=" eq substr($options[$j], 0, 10)) {
$datadir = $options[$j];
$datadir =~ s/\-\-datadir\=//;
eval { mkpath($datadir) };
if ($@) {
print "FATAL ERROR: Cannot create data directory $datadir: $!\n";
exit(1);
}
if (! -d $datadir."/mysql") {
if (-w $datadir) {
print "\n\nInstalling new database in $datadir\n\n";
$install_cmd="C:/Program Files/MySQL/bin/mysql_install_db ";
$install_cmd.="--user=mysql ";
$install_cmd.="--datadir=$datadir";
system($install_cmd);
} else {
print "\n";
print "FATAL ERROR: Tried to create mysqld under group [$groups[$i]],\n";
print "but the data directory is not writable.\n";
print "data directory used: $datadir\n";
exit(1);
}
}
if (! -d $datadir."/mysql") {
print "\n";
print "FATAL ERROR: Tried to start mysqld under group [$groups[$i]],\n";
print "but no data directory was found or could be created.\n";
print "data directory used: $datadir\n";
exit(1);
}
}
if ("--mysqladmin=" eq substr($options[$j], 0, 13))
{
# catch this and ignore
}
elsif ("--mysqld=" eq substr($options[$j], 0, 9))
{
$options[$j]=~ s/\-\-mysqld\=//;
$com= $options[$j];
$mysqld_found= 1;
}
elsif ("--basedir=" eq substr($options[$j], 0, 10))
{
$basedir= $options[$j];
$basedir =~ s/^--basedir=//;
$basedir_found= 1;
$options[$j]= quote_shell_word($options[$j]);
$tmp.= " $options[$j]";
}
elsif ("--defaults-group-suffix=" eq substr($options[$j], 0, 24))
{
$suffix_found= 1;
}
else
{
$options[$j]= quote_shell_word($options[$j]);
$tmp.= " $options[$j]";
}
}
if ($opt_verbose && $com =~ m/\/(safe_mysqld|mysqld_safe)$/ && !$info_sent)
{
print "WARNING: $1 is being used to start mysqld. In this case you ";
print "may need to pass\n\"ledir=...\" under groups [mysqldN] to ";
print "$1 in order to find the actual mysqld binary.\n";
print "ledir (library executable directory) should be the path to the ";
print "wanted mysqld binary.\n\n";
$info_sent= 1;
}
if (!$suffix_found)
{
$com.= " --defaults-group-suffix=";
$com.= substr($groups[$i],6);
}
$com.= $tmp;
if ($opt_wsrep_new_cluster) {
$com.= " --wsrep-new-cluster";
}
$com.= " >> $opt_log 2>&1" if (!$opt_no_log);
$com.= " &";
if (!$mysqld_found)
{
print "\n";
print "FATAL ERROR: Tried to start mysqld under group [$groups[$i]], ";
print "but no mysqld binary was found.\n";
print "Please add \"mysqld=...\" in group [mysqld_multi], or add it to ";
print "group [$groups[$i]] separately.\n";
exit(1);
}
if ($basedir_found)
{
$curdir=getcwd();
chdir($basedir) or die "Can't change to datadir $basedir";
}
system($com);
if ($basedir_found)
{
chdir($curdir) or die "Can't change back to original dir $curdir";
}
}
if (!$i && !$opt_no_log)
{
w2log("No MariaDB servers to be started (check your GNRs)",
"$opt_log", 0, 0);
}
}
####
#### reload multiple servers
####
sub reload_mysqlds()
{
my (@groups, $com, $tmp, $i, @options, $j);
if (!$opt_no_log)
{
w2log("\nReloading MySQL servers\n","$opt_log",0,0);
}
else
{
print "\nReloading MySQL servers\n";
}
@groups = &find_groups($groupids);
for ($i = 0; defined($groups[$i]); $i++)
{
$mysqld_server = $mysqld;
@options = defaults_for_group($groups[$i]);
for ($j = 0, $tmp= ""; defined($options[$j]); $j++)
{
if ("--mysqladmin=" eq substr($options[$j], 0, 13))
{
# catch this and ignore
}
elsif ("--mysqld=" eq substr($options[$j], 0, 9))
{
$options[$j] =~ s/\-\-mysqld\=//;
$mysqld_server = $options[$j];
}
elsif ("--pid-file=" eq substr($options[$j], 0, 11))
{
$options[$j] =~ s/\-\-pid-file\=//;
$pid_file = $options[$j];
}
}
$com = "killproc -p $pid_file -HUP $mysqld_server";
system($com);
$com = "touch $pid_file";
system($com);
}
if (!$i && !$opt_no_log)
{
w2log("No MySQL servers to be reloaded (check your GNRs)",
"$opt_log", 0, 0);
}
}
###
#### stop multiple servers
####
sub stop_mysqlds()
{
my (@groups, $com, $i, @options);
if (!$opt_no_log)
{
w2log("\nStopping MariaDB servers\n","$opt_log",0,0);
}
else
{
print "\nStopping MariaDB servers\n";
}
@groups = &find_groups($groupids);
for ($i = 0; defined($groups[$i]); $i++)
{
$com= get_mysqladmin_options($i, @groups);
$com.= " shutdown";
$com.= " >> $opt_log 2>&1" if (!$opt_no_log);
$com.= " &";
system($com);
}
if (!$i && !$opt_no_log)
{
w2log("No MariaDB servers to be stopped (check your GNRs)",
"$opt_log", 0, 0);
}
}
####
#### Sub function for mysqladmin option parsing
####
sub get_mysqladmin_options
{
my ($i, @groups)= @_;
my ($mysqladmin_found, $com, $tmp, $j);
@options = defaults_for_group($groups[$i]);
$mysqladmin_found= 1; # The default
$mysqladmin_found= 0 if (!length($mysqladmin));
$com = "$mysqladmin";
$tmp = " -u $opt_user";
if (defined($opt_password)) {
my $pw= $opt_password;
# Protect single quotes in password
$pw =~ s/'/'"'"'/g;
$tmp.= " -p'$pw'";
}
$tmp.= $opt_tcp_ip ? " -h 127.0.0.1" : "";
for ($j = 0; defined($options[$j]); $j++)
{
if ("--mysqladmin=" eq substr($options[$j], 0, 13))
{
$options[$j]=~ s/\-\-mysqladmin\=//;
$com= $options[$j];
$mysqladmin_found= 1;
}
elsif ((($options[$j] =~ m/^(\-\-socket\=)(.*)$/) && !$opt_tcp_ip) ||
($options[$j] =~ m/^(\-\-port\=)(.*)$/))
{
$tmp.= " $options[$j]";
}
}
if (!$mysqladmin_found)
{
print "\n";
print "FATAL ERROR: Tried to use mysqladmin in group [$groups[$i]], ";
print "but no mysqladmin binary was found.\n";
print "Please add \"mysqladmin=...\" in group [mysqld_multi], or ";
print "in group [$groups[$i]].\n";
exit(1);
}
$com.= $tmp;
return $com;
}
# Return a list of option files which can be opened. Similar, but not
# identical, to behavior of my_search_option_files()
# TODO implement and use my_print_defaults --list-groups instead
sub list_defaults_files
{
my %opt;
foreach (@defaults_options)
{
return () if /^--no-defaults$/;
$opt{$1} = $2 if /^--defaults-(extra-file|file)=(.*)$/;
}
return ($opt{file}) if exists $opt{file};
my @dirs;
# same rule as in mysys/my_default.c
if ('/etc') {
push @dirs, '/etc/my.cnf';
} else {
push @dirs, '/etc/my.cnf', '/etc/mysql/my.cnf';
}
push @dirs, "$ENV{MYSQL_HOME}/my.cnf" if $ENV{MYSQL_HOME};
push @dirs, $opt{'extra-file'} if $opt{'extra-file'};
push @dirs, "$ENV{HOME}/.my.cnf" if $ENV{HOME};
return @dirs;
}
# Takes a specification of GNRs (see --help), and returns a list of matching
# groups which actually are mentioned in a relevant config file
sub find_groups
{
my ($raw_gids) = @_;
my %gids;
my @groups;
if (defined($raw_gids))
{
# Make a hash of the wanted group ids
foreach my $raw_gid (split ',', $raw_gids)
{
# Match 123 or 123-456
my ($start, $end) = ($raw_gid =~ /^\s*(\d+)(?:\s*-\s*(\d+))?\s*$/);
$end = $start if not defined $end;
if (not defined $start or $end < $start or $start < 0)
{
print "ABORT: Bad GNR: $raw_gid; see $my_progname --help\n";
exit(1);
}
foreach my $i ($start .. $end)
{
# Use $i + 0 to normalize numbers (002 + 0 -> 2)
$gids{$i + 0}= 1;
}
}
}
my %seen;
my @defaults_files = list_defaults_files();
while (@defaults_files)
{
my $file = shift @defaults_files;
next unless defined $file and not $seen{$file}++ and open CONF, '<', $file;
while (<CONF>)
{
if (/^\s*\[\s*(mysqld)(\d+)\s*\]\s*$/)
{
#warn "Found a group: $1$2\n";
# Use $2 + 0 to normalize numbers (002 + 0 -> 2)
if (not defined($raw_gids) or $gids{$2 + 0})
{
push @groups, "$1$2";
}
}
elsif (/^\s*!include\s+(\S.*?)\s*$/)
{
push @defaults_files, $1;
}
elsif (/^\s*!includedir\s+(\S.*?)\s*$/)
{
push @defaults_files, <$1/*.cnf>;
}
}
close CONF;
}
return @groups;
}
####
#### w2log: Write to a logfile.
#### 1.arg: append to the log file (given string, or from a file. if a file,
#### file will be read from $opt_logdir)
#### 2.arg: logfile -name (w2log assumes that the logfile is in $opt_logdir).
#### 3.arg. 0 | 1, if true, print current date to the logfile. 3. arg will
#### be ignored, if 1. arg is a file.
#### 4.arg. 0 | 1, if true, first argument is a file, else a string
####
sub w2log
{
my ($msg, $file, $date_flag, $is_file)= @_;
my (@data);
open (LOGFILE, ">>$opt_log")
or die "FATAL: w2log: Couldn't open log file: $opt_log\n";
if ($is_file)
{
open (FROMFILE, "<$msg") && (@data=<FROMFILE>) &&
close(FROMFILE)
or die "FATAL: w2log: Couldn't open file: $msg\n";
foreach my $line (@data)
{
print LOGFILE "$line";
}
}
else
{
print LOGFILE "$msg";
print LOGFILE strftime "%a %b %e %H:%M:%S %Y", localtime if ($date_flag);
print LOGFILE "\n";
}
close (LOGFILE);
return;
}
####
#### my_which is used, because we can't assume that every system has the
#### which -command. my_which can take only one argument at a time.
#### Return values: requested system command with the first found path,
#### or undefined, if not found.
####
sub my_which
{
my ($command) = @_;
my (@paths, $path);
# If the argument is not 'my_print_defaults' then it would be of the format
# <absolute_path>/<program>
return $command if ($command ne 'my_print_defaults' && -f $command &&
-x $command);
@paths = split(':', $ENV{'PATH'});
foreach $path (@paths)
{
$path .= "/$command";
return $path if (-f $path && -x $path);
}
return undef();
}
####
#### example
####
sub example
{
print <<EOF;
# This is an example of a my.cnf file for $my_progname.
# Usually this file is located in home dir ~/.my.cnf or /etc/my.cnf
#
# SOME IMPORTANT NOTES FOLLOW:
#
# 1.COMMON USER
#
# Make sure that the MariaDB user, who is stopping the mysqld services, has
# the same password to all MariaDB servers being accessed by $my_progname.
# This user needs to have the 'Shutdown_priv' -privilege, but for security
# reasons should have no other privileges. It is advised that you create a
# common 'multi_admin' user for all MariaDB servers being controlled by
# $my_progname. Here is an example how to do it:
#
# GRANT SHUTDOWN ON *.* TO multi_admin\@localhost IDENTIFIED BY 'password'
#
# You will need to apply the above to all MariaDB servers that are being
# controlled by $my_progname. 'multi_admin' will shutdown the servers
# using 'mysqladmin' -binary, when '$my_progname stop' is being called.
#
# 2.PID-FILE
#
# If you are using mysqld_safe to start mysqld, make sure that every
# MariaDB server has a separate pid-file. In order to use mysqld_safe
# via $my_progname, you need to use two options:
#
# mysqld=/path/to/mysqld_safe
# ledir=/path/to/mysqld-binary/
#
# ledir (library executable directory), is an option that only mysqld_safe
# accepts, so you will get an error if you try to pass it to mysqld directly.
# For this reason you might want to use the above options within [mysqld#]
# group directly.
#
# 3.DATA DIRECTORY
#
# It is NOT advised to run many MariaDB servers within the same data directory.
# You can do so, but please make sure to understand and deal with the
# underlying caveats. In short they are:
# - Speed penalty
# - Risk of table/data corruption
# - Data synchronising problems between the running servers
# - Heavily media (disk) bound
# - Relies on the system (external) file locking
# - Is not applicable with all table types. (Such as InnoDB)
# Trying so will end up with undesirable results.
#
# 4.TCP/IP Port
#
# Every server requires one and it must be unique.
#
# 5.[mysqld#] Groups
#
# In the example below the first and the fifth mysqld group was
# intentionally left out. You may have 'gaps' in the config file. This
# gives you more flexibility.
#
# 6.MariaDB Server User
#
# You can pass the user=... option inside [mysqld#] groups. This
# can be very handy in some cases, but then you need to run $my_progname
# as UNIX root.
#
# 7.A Start-up Manage Script for $my_progname
#
# In the recent MariaDB distributions you can find a file called
# mysqld_multi.server.sh. It is a wrapper for $my_progname. This can
# be used to start and stop multiple servers during boot and shutdown.
#
# You can place the file in /etc/init.d/mysqld_multi.server.sh and
# make the needed symbolic links to it from various run levels
# (as per Linux/Unix standard). You may even replace the
# /etc/init.d/mysql.server script with it.
#
# Before using, you must create a my.cnf file either in C:/Program Files/MySQL/my.cnf
# or /root/.my.cnf and add the [mysqld_multi] and [mysqld#] groups.
#
# The script can be found from support-files/mysqld_multi.server.sh
# in MariaDB distribution. (Verify the script before using)
#
[mysqld_multi]
mysqld = C:/Program Files/MySQL/bin/mysqld_safe
mysqladmin = C:/Program Files/MySQL/bin/mysqladmin
user = multi_admin
password = my_password
[mysqld2]
socket = /tmp/mysql.sock2
port = 3307
pid-file = C:/Program Files/MariaDB 10.4/data2/hostname.pid2
datadir = C:/Program Files/MariaDB 10.4/data2
language = C:/Program Files/MySQL/share/mysql/english
user = unix_user1
[mysqld3]
mysqld = /path/to/mysqld_safe
ledir = /path/to/mysqld-binary/
mysqladmin = /path/to/mysqladmin
socket = /tmp/mysql.sock3
port = 3308
pid-file = C:/Program Files/MariaDB 10.4/data3/hostname.pid3
datadir = C:/Program Files/MariaDB 10.4/data3
language = C:/Program Files/MySQL/share/mysql/swedish
user = unix_user2
[mysqld4]
socket = /tmp/mysql.sock4
port = 3309
pid-file = C:/Program Files/MariaDB 10.4/data4/hostname.pid4
datadir = C:/Program Files/MariaDB 10.4/data4
language = C:/Program Files/MySQL/share/mysql/estonia
user = unix_user3
[mysqld6]
socket = /tmp/mysql.sock6
port = 3311
pid-file = C:/Program Files/MariaDB 10.4/data6/hostname.pid6
datadir = C:/Program Files/MariaDB 10.4/data6
language = C:/Program Files/MySQL/share/mysql/japanese
user = unix_user4
EOF
exit(0);
}
####
#### usage
####
sub usage
{
print <<EOF;
$my_progname version $VER by Jani Tolonen
Description:
$my_progname can be used to start, reload, or stop any number of separate
mysqld processes running in different TCP/IP ports and UNIX sockets.
$my_progname can read group [mysqld_multi] from my.cnf file. You may
want to put options mysqld=... and mysqladmin=... there. Since
version 2.10 these options can also be given under groups [mysqld#],
which gives more control over different versions. One can have the
default mysqld and mysqladmin under group [mysqld_multi], but this is
not mandatory. Please note that if mysqld or mysqladmin is missing
from both [mysqld_multi] and [mysqld#], a group that is tried to be
used, $my_progname will abort with an error.
$my_progname will search for groups named [mysqld#] from my.cnf (or
the given --defaults-extra-file=...), where '#' can be any positive
integer starting from 1. These groups should be the same as the regular
[mysqld] group, but with those port, socket and any other options
that are to be used with each separate mysqld process. The number
in the group name has another function; it can be used for starting,
reloading, stopping, or reporting any specific mysqld server.
Usage: $my_progname [OPTIONS] {start|reload|stop|report} [GNR,GNR,GNR...]
or $my_progname [OPTIONS] {start|reload|stop|report} [GNR-GNR,GNR,GNR-GNR,...]
The GNR means the group number. You can start, reload, stop or report any GNR,
or several of them at the same time. (See --example) The GNRs list can
be comma separated or a dash combined. The latter means that all the
GNRs between GNR1-GNR2 will be affected. Without GNR argument all the
groups found will either be started, reloaded, stopped, or reported. Note that
syntax for specifying GNRs must appear without spaces.
Options:
These options must be given before any others:
--no-defaults Do not read any defaults file
--defaults-file=... Read only this configuration file, do not read the
standard system-wide and user-specific files
--defaults-extra-file=... Read this configuration file in addition to the
standard system-wide and user-specific files
Using: @{[join ' ', @defaults_options]}
--example Give an example of a config file with extra information.
--help Print this help and exit.
--log=... Log file. Full path to and the name for the log file. NOTE:
If the file exists, everything will be appended.
Using: $opt_log
--mysqladmin=... mysqladmin binary to be used for a server shutdown.
Since version 2.10 this can be given within groups [mysqld#]
Using: $mysqladmin
--mysqld=... mysqld binary to be used. Note that you can give mysqld_safe
to this option also. The options are passed to mysqld. Just
make sure you have mysqld in your PATH or fix mysqld_safe.
Using: $mysqld
Please note: Since mysqld_multi version 2.3 you can also
give this option inside groups [mysqld#] in ~/.my.cnf,
where '#' stands for an integer (number) of the group in
question. This will be recognised as a special option and
will not be passed to the mysqld. This will allow one to
start different mysqld versions with mysqld_multi.
--no-log Print to stdout instead of the log file. By default the log
file is turned on.
--password=... Password for mysqladmin user.
--silent Disable warnings.
--tcp-ip Connect to the MariaDB server(s) via the TCP/IP port instead
of the UNIX socket. This affects stopping and reporting.
If a socket file is missing, the server may still be
running, but can be accessed only via the TCP/IP port.
By default connecting is done via the UNIX socket.
--user=... mysqladmin user. Using: $opt_user
--verbose Be more verbose.
--version Print the version number and exit.
--wsrep-new-cluster Bootstrap a cluster.
EOF
exit(0);
}

Binary file not shown.

View File

@@ -0,0 +1,236 @@
#!/usr/bin/perl
# Copyright (c) 2000, 2017, 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 Library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1335 USA
# mysqldumpslow - parse and summarize the MySQL slow query log
# Original version by Tim Bunce, sometime in 2000.
# Further changes by Tim Bunce, 8th March 2001.
# Handling of strings with \ and double '' by Monty 11 Aug 2001.
use strict;
use Getopt::Long;
# t=time, l=lock time, r=rows, a=rows affected
# at, al, ar and aa are the corresponding averages
my %opt = (
s => 'at',
h => '*',
);
GetOptions(\%opt,
'v|verbose+',# verbose
'help+', # write usage info
'd|debug+', # debug
's=s', # what to sort by (aa, ae, al, ar, at, a, c, e, l, r, t)
'r!', # reverse the sort order (largest last instead of first)
't=i', # just show the top n queries
'a!', # don't abstract all numbers to N and strings to 'S'
'n=i', # abstract numbers with at least n digits within names
'g=s', # grep: only consider stmts that include this string
'h=s', # hostname/basename of db server for *-slow.log filename (can be wildcard)
'i=s', # name of server instance (if using mysql.server startup script)
'l!', # don't subtract lock time from total time
) or usage("bad option");
$opt{'help'} and usage();
unless (@ARGV) {
my $defaults = `my_print_defaults --mysqld`;
my $datadir = ($defaults =~ m/--datadir=(.*)/g)[-1];
if (!$datadir or $opt{i}) {
# determine the datadir from the instances section of /etc/my.cnf, if any
my $instances = `my_print_defaults instances`;
die "Can't determine datadir from 'my_print_defaults instances' output: $defaults"
unless $instances;
my @instances = ($instances =~ m/^--(\w+)-/mg);
die "No -i 'instance_name' specified to select among known instances: @instances.\n"
unless $opt{i};
die "Instance '$opt{i}' is unknown (known instances: @instances)\n"
unless grep { $_ eq $opt{i} } @instances;
$datadir = ($instances =~ m/--$opt{i}-datadir=(.*)/g)[-1]
or die "Can't determine --$opt{i}-datadir from 'my_print_defaults instances' output: $instances";
warn "datadir=$datadir\n" if $opt{v};
}
my $slowlog = ($defaults =~ m/--log[-_]slow[-_]queries=(.*)/g)[-1];
if (!$slowlog)
{
$slowlog = ($defaults =~ m/--slow[-_]query[-_]log[-_]file=(.*)/g)[-1];
}
if ( $slowlog )
{
@ARGV = ($slowlog);
die "Can't find '$slowlog'\n" unless @ARGV;
}
else
{
if (!$opt{h})
{
$opt{h}= ($defaults =~ m/--log[-_]basename=(.*)/g)[-1];
}
@ARGV = <$datadir/$opt{h}-slow.log>;
die "Can't find '$datadir/$opt{h}-slow.log'\n" unless @ARGV;
}
}
warn "\nReading mysql slow query log from @ARGV\n";
my @pending;
my %stmt;
$/ = ";\n#"; # read entire statements using paragraph mode
while ( defined($_ = shift @pending) or defined($_ = <>) ) {
warn "[[$_]]\n" if $opt{d}; # show raw paragraph being read
my @chunks = split /^\/.*Version.*started with[\000-\377]*?Time.*Id.*Command.*Argument.*\n/m;
if (@chunks > 1) {
unshift @pending, map { length($_) ? $_ : () } @chunks;
warn "<<".join(">>\n<<",@chunks).">>" if $opt{d};
next;
}
s/^#? Time: \d{6}\s+\d+:\d+:\d+.*\n//;
my ($user,$host) = s/^#? User\@Host:\s+(\S+)\s+\@\s+(\S+).*\n// ? ($1,$2) : ('','');
s/^# Thread_id: [0-9]+\s+Schema: .*\s+QC_hit:.*[^\n]+\n//;
s/^# Query_time: ([0-9.]+)\s+Lock_time: ([0-9.]+)\s+Rows_sent: ([0-9.]+)\s+Rows_examined: ([0-9.]+).*\n//;
my ($t, $l, $r, $e) = ($1, $2, $3, $4);
s/^# Rows_affected: ([0-9.]+).*\n//;
my ($a) = ($1);
$t -= $l unless $opt{l};
# remove fluff that mysqld writes to log when it (re)starts:
s!^/.*Version.*started with:.*\n!!mg;
s!^Tcp port: \d+ Unix socket: \S+\n!!mg;
s!^Time.*Id.*Command.*Argument.*\n!!mg;
# Remove optimizer info
s!^# QC_Hit: \S+\s+Full_scan: \S+\s+Full_join: \S+\s+Tmp_table: \S+\s+Tmp_table_on_disk: \S+[^\n]+\n!!mg;
s!^# Filesort: \S+\s+Filesort_on_disk: \S+[^\n]+\n!!mg;
s!^# Full_scan: \S+\s+Full_join: \S+[^\n]+\n!!mg;
s/^use \w+;\n//; # not consistently added
s/^SET timestamp=\d+;\n//;
s/^[ ]*\n//mg; # delete blank lines
s/^[ ]*/ /mg; # normalize leading whitespace
s/\s*;\s*(#\s*)?$//; # remove trailing semicolon(+newline-hash)
next if $opt{g} and !m/$opt{g}/io;
unless ($opt{a}) {
s/\b\d+\b/N/g;
s/\b0x[0-9A-Fa-f]+\b/N/g;
s/''/'S'/g;
s/""/"S"/g;
s/(\\')//g;
s/(\\")//g;
s/'[^']+'/'S'/g;
s/"[^"]+"/"S"/g;
# -n=8: turn log_20001231 into log_NNNNNNNN
s/([a-z_]+)(\d{$opt{n},})/$1.('N' x length($2))/ieg if $opt{n};
# abbreviate massive "in (...)" statements and similar
s!(([NS],){100,})!sprintf("$2,{repeated %d times}",length($1)/2)!eg;
}
my $s = $stmt{$_} ||= { users=>{}, hosts=>{} };
$s->{c} += 1;
$s->{t} += $t;
$s->{l} += $l;
$s->{r} += $r;
$s->{e} += $e;
$s->{a} += $a;
$s->{users}->{$user}++ if $user;
$s->{hosts}->{$host}++ if $host;
warn "{{$_}}\n\n" if $opt{d}; # show processed statement string
}
foreach (keys %stmt) {
my $v = $stmt{$_} || die;
my ($c, $t, $l, $r, $e, $a) = @{ $v }{qw(c t l r e a)};
$v->{at} = $t / $c;
$v->{al} = $l / $c;
$v->{ar} = $r / $c;
$v->{ae} = $e / $c;
$v->{aa} = $a / $c;
}
my @sorted = sort { $stmt{$b}->{$opt{s}} <=> $stmt{$a}->{$opt{s}} } keys %stmt;
@sorted = @sorted[0 .. $opt{t}-1] if $opt{t};
@sorted = reverse @sorted if $opt{r};
foreach (@sorted) {
my $v = $stmt{$_} || die;
my ($c, $t, $at, $l, $al, $r, $ar, $e, $ae, $a, $aa) = @{ $v }{qw(c t at l al r ar e ae a aa)};
my @users = keys %{$v->{users}};
my $user = (@users==1) ? $users[0] : sprintf "%dusers",scalar @users;
my @hosts = keys %{$v->{hosts}};
my $host = (@hosts==1) ? $hosts[0] : sprintf "%dhosts",scalar @hosts;
printf "Count: %d Time=%.2fs (%ds) Lock=%.2fs (%ds) Rows_sent=%.1f (%d), Rows_examined=%.1f (%d), Rows_affected=%.1f (%d), $user\@$host\n%s\n\n",
$c, $at,$t, $al,$l, $ar,$r, $ae, $e, $aa, $a, $_;
}
sub usage {
my $str= shift;
my $text= <<HERE;
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
--verbose verbose
--debug debug
--help write this text to standard output
-v verbose
-d debug
-s ORDER what to sort by (aa, ae, al, ar, at, a, c, e, l, r, t), 'at' is default
aa: average rows affected
ae: aggregated rows examined
al: average lock time
ar: average rows sent
at: average query time
a: rows affected
c: count
e: rows examined
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries
-a don't abstract all numbers to N and strings to 'S'
-n NUM abstract numbers with at least n digits within names
-g PATTERN grep: only consider stmts that include this string
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
default is '*', i.e. match all
-i NAME name of server instance (if using mysql.server startup script)
-l don't subtract lock time from total time
HERE
if ($str) {
print STDERR "ERROR: $str\n\n";
print STDERR $text;
exit 1;
} else {
print $text;
exit 0;
}
}

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.