/usr/share/perl5/MMM/Tools/MySQL.pm is in mysql-mmm-tools 2.2.1-1.1.
This file is owned by root:root, with mode 0o644.
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 | package MMM::Tools::MySQL;
use strict;
use warnings FATAL => 'all';
use DBI;
use Log::Log4perl qw(:easy);
our $VERSION = '0.01';
=head1 NAME
MMM::Tools::MySQL - MySQL related functions for the mmm-tools.
=cut
=over 4
=item is_running([$pid])
Check if mysqld is running
=cut
sub is_running {
my $pid = shift;
unless ($pid) {
my $pidfile = $main::config->{host}->{$main::config->{this}}->{mysql_pidfile};
return 0 unless (-f $pidfile);
open(PID, $pidfile) || LOGDIE "ERROR: Can't read MySQL pid file '$pidfile'";
chomp($pid = <PID>);
close(PID);
}
my $cnt = kill(0, $pid);
return $pid if ($cnt);
return 0;
}
=item start
Start mysqld
=cut
sub start() {
my $rcscript = $main::config->{host}->{$main::config->{this}}->{mysql_rcscript};
my $pid = is_running();
if ($pid) {
ERROR "Error: Local MySQL server is running with pid $pid";
return 0;
}
INFO 'MySQL is not running. Going to start it...';
my $res = system($rcscript, 'start');
if ($res) {
ERROR "ERROR: Can't start local MySQL server!";
return 0;
}
INFO 'MySQL has been started!';
return 1;
}
=item stop
Stop mysqld
=cut
sub stop() {
my $rcscript = $main::config->{host}->{$main::config->{this}}->{mysql_rcscript};
my $pid = is_running();
unless ($pid) {
WARN 'MySQL is not running now, skipping shutdown ...';
return 1;
}
my $res = system($rcscript, 'stop');
if ($res) {
ERROR "ERROR: Can't stop local MySQL server!";
return 0;
}
my $wait = 15;
DEBUG ("Waiting MySQL process with $pid to shutdown: ");
while ($wait--) {
$pid = is_running($pid);
last if ($pid == 0);
DEBUG '.';
sleep(1);
}
if ($pid != 0) {
ERROR "ERROR: MySQL is running with PID $pid after shutdown request!";
return 0;
}
INFO 'MySQL has been stopped!';
return 1;
}
=item change_master_to(named_params)
Required params
master_host
master_port
master_user
master_pass
Optional params
master_log
master_pos
=cut
sub change_master_to {
my $args = shift;
$args->{host} ||= $main::config->{this};
LOGDIE 'Bad call of change_master_to()' unless (
defined($args->{master_host}) && defined($args->{master_port})
&& defined($args->{master_user}) && defined($args->{master_pass})
);
INFO "Changing master of host $args->{host} to $args->{master_host} ...";
# Get connection information
my ($host, $port, $user, $password) = _get_connection_info($args->{host});
unless (defined($host)) {
ERROR "No connection info for host '$args->{host}'";
return 0;
}
# Connect to server
my $dbh = _connect($host, $port, $user, $password);
unless ($dbh) {
ERROR "Can't connect to MySQL (host = $host:$port, user = $user)!";
return 0;
}
my $res;
# Stop slave
$res = $dbh->do('STOP SLAVE');
unless ($res) {
ERROR 'SQL Query Error: ', $dbh->errstr;
return 0;
}
# Force deletion of obsolete master.info, relay-log.info and relay logs.
$res = $dbh->do('RESET SLAVE');
unless ($res) {
ERROR 'SQL Query Error: ', $dbh->errstr;
return 0;
}
# Change master
my $sql = sprintf(
"CHANGE MASTER TO MASTER_HOST='%s', MASTER_PORT=%s, MASTER_USER='%s', MASTER_PASSWORD='%s'",
$args->{master_host}, $args->{master_port}, $args->{master_user}, $args->{master_pass}
);
if ($args->{master_log} && $args->{master_pos}) {
$sql .= sprintf(", MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s", $args->{master_log}, $args->{master_pos});
}
$res = $dbh->do($sql);
unless ($res) {
ERROR 'SQL Query Error: ', $dbh->errstr;
return 0;
}
# Start slave
$res = $dbh->do('START SLAVE');
unless ($res) {
ERROR 'SQL Query Error: ', $dbh->errstr;
return 0;
}
# Disconnect
$dbh->disconnect;
INFO "Successfully changed master.";
return 1;
}
=item _get_connection_info($host)
Get connection info for host $host
=cut
sub _get_connection_info($) {
my $host = shift;
# TODO maybe check $host
return (
$main::config->{host}->{$host}->{ip},
$main::config->{host}->{$host}->{mysql_port},
$main::config->{host}->{$host}->{tools_user},
$main::config->{host}->{$host}->{tools_password}
);
}
sub _connect($$$$) {
my ($host, $port, $user, $password) = @_;
my $dsn = "DBI:mysql:host=$host;port=$port;mysql_connect_timeout=3";
return DBI->connect($dsn, $user, $password, { PrintError => 0 });
}
sub get_master_host($) {
my $host_name = shift;
# Get connection information
my ($host, $port, $user, $password) = _get_connection_info($host_name);
unless (defined($host)) {
ERROR "No connection info for host '$host_name'";
return undef;
}
# Connect to server
my $dbh = _connect($host, $port, $user, $password);
unless ($dbh) {
ERROR "Can't connect to MySQL (host = $host:$port, user = $user)!";
return undef;
}
# Get slave status
my $res = $dbh->selectrow_hashref('SHOW SLAVE STATUS');
return "ERROR: Can't get slave status for host '$host_name'! Error: " . $dbh->errstr unless ($res);
# Disconnect
$dbh->disconnect();
return $res->{Master_Host};
}
1;
|