2021-10-09

Top
NSD RDBM1 DAY04
案例1: 用户授权
案例2:root密码
案例3:数据备份与恢复
案例4:binlog日志
案例5:使用binlog日志恢复数据
1 案例1: 用户授权
1.1 问题
1.2 允许192.168.4.0/24网段主机使用root连接数据库服务器,对所有库和所有表有完全权限、密码为123qqq…A
1.3 添加用户dba007,对所有库和所有表有完全权限、且有授权权限,密码为123qqq…A 客户端为网络中的所有主机。
1.4 撤销root从本机访问权限,然后恢复。
1.5 允许任意主机使用webuser用户连接数据库服务器,仅对webdb库有完全权限,密码为123qqq…A
1.6 撤销webuser的权限,使其仅有查询记录权限
1.7 步骤
实现此案例需要按照如下步骤进行。

步骤一:用户授权

1)允许root从192.168.4.0/24访问,对所有库表有完全权限,密码为123qqq…A

授权之前,从192.168.4.0/24网段的客户机访问时,将会被拒绝:

[root@host120 ~]# mysql -u root -p -h 192.168.4.10
Enter password: //输入正确的密码
ERROR 2003 (HY000): Host ‘192.168.4.120’ is not allowed to connect to this MySQL server
授权操作,此处可设置与从localhost访问时不同的密码:

mysql> GRANT all ON . TO root@‘192.168.4.%’ IDENTIFIED BY ‘tarena’;
Query OK, 0 rows affected (0.00 sec)
再次从192.168.4.0/24网段的客户机访问时,输入正确的密码后可登入:

[root@host120 ~]# mysql -u root -p -h 192.168.4.10
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
从网络登入后,测试新建一个库、查看所有库:

mysql> CREATE DATABASE rootdb; //创建新库rootdb
Query OK, 1 row affected (0.06 sec)
mysql> SHOW DATABASES;
±-------------------+
| Database |
±-------------------+
| information_schema |
| home |
| mysql |
| performance_schema |
| rootdb | //新建的rootdb库
| sys |
| userdb |
±-------------------+
7 rows in set (0.01 sec)
2)在Mysql服务器上建立一个管理账号dba007,对所有库完全控制,并赋予其授权的权限新建账号并授权:

mysql> GRANT all ON . TO dba007@localhost
-> IDENTIFIED BY '123qqq…A ’
-> WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
查看dba007的权限:

mysql> SHOW GRANTS FOR dba007@localhost;
±----------------------------------------------------------------------+
| Grants for dba007@localhost |
±----------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON . TO ‘dba007’@‘localhost’ WITH GRANT OPTION |
±----------------------------------------------------------------------+
1 row in set (0.00 sec)
3)撤销root从本机访问的权限,然后恢复

注意:如果没有事先建立其他管理账号,请不要轻易撤销root用户的本地访问权限,否则恢复起来会比较困难,甚至不得不重装数据库。

撤销root对数据库的操作权限:

mysql> REVOKE all ON . FROM root@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GRANTS FOR root@localhost;
±-------------------------------------------------------------+
| Grants for root@localhost |
±-------------------------------------------------------------+
| GRANT USAGE ON . TO ‘root’@‘localhost’ WITH GRANT OPTION |
| GRANT PROXY ON ‘’@’’ TO ‘root’@‘localhost’ WITH GRANT OPTION |
±-------------------------------------------------------------+
2 rows in set (0.00 sec)
验证撤销后的权限效果:

mysql> exit //退出当前MySQL连接
Bye
[root@dbsvr1 ~]# mysql -u root -p //重新以root从本地登入
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.15 MySQL Community Server (GPL)
Copyright © 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> CREATE DATABASE newdb2014; //尝试新建库失败
ERROR 1044 (42000): Access denied for user ‘root’@‘localhost’ to database ‘newdb2014’
mysql> DROP DATABASE rootdb; //尝试删除库失败
ERROR 1044 (42000): Access denied for user ‘root’@‘localhost’ to database ‘rootdb’
尝试以当前的root用户恢复权限,也会失败(无权更新授权表):

mysql> GRANT all ON . TO root@localhost IDENTIFIED BY ‘1234567’;
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
怎么办呢?

退出当前MySQL连接,以上一步添加的管理账号dba007登入:

mysql> exit //退出当前MySQL连接
Bye
[root@dbsvr1 ~]# mysql -u dba007 -p //以另一个管理账号登入
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
由管理账号dba007重新为root添加本地访问权限:

mysql> GRANT all ON . TO root@localhost IDENTIFIED BY ‘1234567’;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GRANTS FOR root@localhost; //查看恢复结果
±--------------------------------------------------------------------+
| Grants for root@localhost |
±--------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON . TO ‘root’@‘localhost’ WITH GRANT OPTION |
| GRANT PROXY ON ‘’@’’ TO ‘root’@‘localhost’ WITH GRANT OPTION |
±--------------------------------------------------------------------+
2 rows in set (0.00 sec)
退出,再重新以root登入,测试一下看看,权限又恢复了吧:

mysql> exit //退出当前MySQL连接
Bye
[root@dbsvr1 ~]# mysql -u root -p //重新以root登入
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> CREATE DATABASE newdb2014; //成功创建新库
Query OK, 1 row affected (0.00 sec)
4)允许webuser从任意客户机登录,只对webdb库有完全权限,密码为 123qqq…A

添加授权:

mysql> GRANT all ON webdb.* TO webuser@’%’ IDENTIFIED BY ‘888888’;
Query OK, 0 rows affected (0.00 sec)
查看授权结果:

mysql> SHOW GRANTS FOR webuser@’%’;
±---------------------------------------------------+
| Grants for webuser@% |
±---------------------------------------------------+
| GRANT USAGE ON . TO ‘webuser’@’%’ |
| GRANT ALL PRIVILEGES ON webdb.* TO ‘webuser’@’%’ |
±---------------------------------------------------+
2 rows in set (0.00 sec)
5)撤销webuser的完全权限,改为查询权限

撤销所有权限:

mysql> REVOKE all ON webdb.* FROM webuser@’%’;
Query OK, 0 rows affected (0.00 sec)
只赋予查询权限:

mysql> GRANT select ON webdb.* TO webuser@’%’;
Query OK, 0 rows affected (0.00 sec)
确认授权更改结果:

mysql> SHOW GRANTS FOR webuser@’%’;
±-------------------------------------------+
| Grants for webuser@% |
±-------------------------------------------+
| GRANT USAGE ON . TO ‘webuser’@’%’ |
| GRANT SELECT ON webdb.* TO ‘webuser’@’%’ |
±-------------------------------------------+
2 rows in set (0.00 sec)
2 案例2:root密码
2.1 问题
具体要求如下:

恢复管理员root密码 123qqq…A
重置管理员root密码 A…qqq321
2.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:恢复管理员root密码

1)首先停止已运行的MySQL服务程序

[root@dbsvr1 ~]# systemctl stop mysqld.service //停止服务
[root@dbsvr1 ~]# systemctl status mysqld.service //确认状态
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
Active: inactive (dead) since 五 2017-04-07 23:01:38 CST; 21s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 20260 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 20238 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 20262 (code=exited, status=0/SUCCESS)
2)然后跳过授权表启动MySQL服务程序

这一步主要利用mysqld的 --skip-grant-tables选项

修改my.cnf配置,添加 skip_grant_tables=1启动设置:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
skip_grant_tables
… …
[root@dbsvr1 ~]# systemctl start mysqld.service
[root@dbsvr1 ~]# service mysql status
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
Active: active (running) since 五 2017-04-07 23:40:20 CST; 40s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 11698 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 11676 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 11701 (mysqld)
CGroup: /system.slice/mysqld.service
└─11701 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.p…
3)使用mysql命令连接到MySQL服务,重设root的密码

由于前一步启动的MySQL服务跳过了授权表,所以可以root从本机直接登录

[root@dbsvr1 ~]# mysql //直接回车即可

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
进入 mysql> 环境后,通过修改mysql库中user表的相关记录,重设root用户从本机登录的密码:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD(‘123qqq…A’)
-> WHERE user=‘root’ AND host=‘localhost’; //重设root的密码
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> FLUSH PRIVILEGES; //刷新授权表
Query OK, 0 rows affected (0.01 sec)
mysql> exit //退出mysql> 环境
Bye
通过执行“FLUSH PRIVILEGES;”可使授权表立即生效,对于正常运行的MySQL服务,也可以用上述方法来修改密码,不用重启服务。本例中因为是恢复密码,最好重启MySQL服务程序,所以上述“FLUSH PRIVILEGES;”操作可跳过。

4)重新以正常方式启动MySQL服务程序,验证新密码

如果前面是修改/etc/my.cnf配置的方法来跳过授权表,则重置root密码后,应去除相应的设置以恢复正常:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
#skip_grant_tables=1 //注释掉或删除此行
… …
按正常方式,通过mysql脚本重启服务即可:

[root@dbsvr1 ~]# systemctl restart mysqld.service
验证无密码登录时,将会被拒绝:

[root@dbsvr1 ~]# mysql -u root
Enter password: //没有跳过授权表回车会报错
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)
只有提供重置后的新密码,才能成功登入:

[root@dbsvr1 ~]# mysql -uroot –p123qqq…A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
步骤二:重置管理员root密码

正常的前提是:已知当前MySQL管理用户(root)的密码。

1)方法1,在Shell命令行下设置

使用mysqladmin管理工具,需要验证旧的密码。比如,以下操作将会把root的密码设置为 1234567:

[root@dbsvr1 ~]# mysqladmin -uroot -p password ‘A…qqq321’
Enter password: //验证原来的密码
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. //提示明文修改不安全,并不是报错
[root@dbsvr1 ~]# mysql -uroot –pA…qqq321 //使用修改后的密码登录
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
步骤三:修改管理员root密码的其他方法

1)方法1,以root登入mysql> 后,使用SET PASSWORD指令设置

这个与新安装MySQL-server后首次修改密码时要求的方式相同,平时也可以用:

mysql> SET PASSWORD FOR root@localhost=PASSWORD(‘1234567’);
Query OK, 0 rows affected, 1 warning (0.00 sec)
2)方法2,以root登入mysql> 后,使用GRANT授权工具设置

这个是最常见的用户授权方式(下一节会做更多授权的练习):

mysql> GRANT all ON . TO root@localhost IDENTIFIED BY ‘1234567’;
Query OK, 0 rows affected, 1 warning (0.00 sec)
3)方法3,以root登入mysql> 后,使用UPDATE更新相应的表记录

这种方法与恢复密码时的操作相同:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD(‘1234567’)
-> WHERE user=‘root’ AND host=‘localhost’; //重设root的密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> FLUSH PRIVILEGES; //刷新授权表
Query OK, 0 rows affected (0.00 sec)
在上述方法中,需要特别注意:当MySQL服务程序以 skip-grant-tables 选项启动时,如果未执行“FLUSH PRIVILEGES;”操作,是无法通过SET PASSWORD或者GRANT方式来设置密码的。比如,验证这两种方式时,都会看到ERROR 1290的出错提示:

mysql> SET PASSWORD FOR root@localhost=PASSWORD(‘1234567’);
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> GRANT all ON . TO root@localhost IDENTIFIED BY ‘1234567’;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
3 案例3:数据备份与恢复
3.1 问题
具体要求如下:

练习mysqldump命令的使用
使用 mysql 命令恢复删除的数据
3.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:练习mysqldump命令的使用

1)备份MySQL服务器上的所有库

将所有的库备份为mysql-all.sql文件:

[root@dbsvr1 ~]# mysqldump -u root -p --all-databases > /root/alldb.sql
Enter password: //验证口令
[root@dbsvr1 mysql]# file /root/alldb.sql //确认备份文件类型
/root/alldb.sql: UTF-8 Unicode English text, with very long lines
查看备份文件alldb.sql的部分内容:

[root@dbsvr1 ~]# grep -vE ‘/|-|^$’ /root/alldb.sql | head -15
CREATE DATABASE /!32312 IF NOT EXISTS/ home /*!40100 DEFAULT CHARACTER SET latin1 */;
USE home;
DROP TABLE IF EXISTS biao01;
CREATE TABLE biao01 (
id int(2) NOT NULL,
name varchar(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES biao01 WRITE;
UNLOCK TABLES;
DROP TABLE IF EXISTS biao02;
CREATE TABLE biao02 (
id int(4) NOT NULL,
name varchar(8) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
… …
注意:若数据库都使用MyISAM存储引擎,可以采用冷备份的方式,直接复制对应的数据库目录即可;恢复时重新复制回来就行。

2)只备份指定的某一个库

将userdb库备份为userdb.sql文件:

[root@dbsvr1 ~]# mysqldump -u root -p userdb > userdb.sql
Enter password: //验证口令
查看备份文件userdb.sql的部分内容:

[root@dbsvr1 ~]# grep -vE ‘/|-|^$’ /root/userdb.sql
DROP TABLE IF EXISTS stu_info;
CREATE TABLE stu_info (
name varchar(12) NOT NULL,
gender enum(‘boy’,‘girl’) DEFAULT ‘boy’,
age int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES stu_info WRITE;
… …
3)同时备份指定的多个库

同时备份mysql、userdb库,保存为mysql+userdb.sql文件:

[root@dbsvr1 ~]# mysqldump -u root -p -B mysql userdb > mysql+test+userdb.sql
Enter password: //验证口令
查看备份文件userdb.sql的部分内容:

[root@dbsvr1 ~]# grep ‘^CREATE DATA’ /root/mysql+userdb.sql
CREATE DATABASE /!32312 IF NOT EXISTS/ mysql /!40100 DEFAULT CHARACTER SET latin1 /;
CREATE DATABASE /
!32312 IF NOT EXISTS
/ userdb /*!40100 DEFAULT CHARACTER SET latin1 */;
步骤二:使用 mysql 命令恢复删除的数据

以恢复userdb库为例,可参考下列操作。通常不建议直接覆盖旧库,而是采用建立新库并导入逻辑备份的方式执行恢复,待新库正常后即可废弃或删除旧库。

1)创建名为userdb2的新库

mysql> CREATE DATABASE userdb2;
Query OK, 1 row affected (0.00 sec)
2)导入备份文件,在新库中重建表及数据

[root@dbsvr1 ~]# mysql -u root -p userdb2 < /root/userdb.sql
Enter password: //验证口令
3)确认新库正常,启用新库

mysql> USE userdb2; //切换到新库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SELECT sn,username,uid,gid,homedir //查询数据,确认可用
-> FROM userlist LIMIT 10;
±—±---------±----±----±----------------+
| sn | username | uid | gid | homedir |
±—±---------±----±----±----------------+
| 1 | root | 0 | 0 | /root |
| 2 | bin | 1 | 1 | /bin |
| 3 | daemon | 2 | 2 | /sbin |
| 4 | adm | 3 | 4 | /var/adm |
| 5 | lp | 4 | 7 | /var/spool/lpd |
| 6 | sync | 5 | 0 | /sbin |
| 7 | shutdown | 6 | 0 | /sbin |
| 8 | halt | 7 | 0 | /sbin |
| 9 | mail | 8 | 12 | /var/spool/mail |
| 10 | operator | 11 | 0 | /root |
±—±---------±----±----±----------------+
10 rows in set (0.00 sec)
4)废弃或删除旧库

mysql> DROP DATABASE userdb;
Query OK, 2 rows affected (0.09 sec)
4 案例4:binlog日志
4.1 问题
启用binlog日志,具体要求如下:

启用binlog日志,把日志文件存放到系统的/mylog目录下,日志文件为db50
手动创建3个新的日志文件
删除编号3之前的日志文件
4.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:启用binlog日志

1)修改配置文件,并重启服务。

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
server_id=1 //指定server_id
log-bin=/mylog/db50 //指定日志目录及名称
:wq
[root@dbsvr1 ~]# mkdir /mylog //创建目录
[root@dbsvr1 ~]# chmod mysql /mylog //修改所有者
[root@dbsvr1 ~]# systemctl restart mysqld.service //重启服务
2)查看日志信息

[root@dbsvr1 ~]#
[root@localhost ~]# mysql -uroot -p123qqq…A //管理员登录
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright © 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> show master status; //查看日志信息
±------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±------------±---------±-------------±-----------------±------------------+
| db50.000001 | 154 | | | |
±------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
mysql>
3)手动创建3个新的日志文件

mysql>
mysql> flush logs; //刷新日志
Query OK, 0 rows affected (0.14 sec)
mysql> flush logs; //刷新日志
Query OK, 0 rows affected (0.11 sec)
mysql> flush logs; //刷新日志
Query OK, 0 rows affected (0.12 sec)
mysql> system ls /mylog/ //查看日志文件
db50.000001 db50.000002 db50.000003 db50.000004 db50.index
mysql>
mysql> show master status; //查看日志信息
±------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±------------±---------±-------------±-----------------±------------------+
| db50.000004 | 154 | | | |
±------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)
mysql>
4)删除编号3之前的日志文件

mysql>
mysql> purge master logs to “db50.000003”; //删除日志
Query OK, 0 rows affected (0.05 sec)
mysql> system ls /mylog/ //查看日志文件
db50.000003 db50.000004 db50.index
mysql>
mysql> system cat /mylog/db50.index //查看索引文件
/mylog/db50.000003
/mylog/db50.000004
mysql>
5 案例5:使用binlog日志恢复数据
5.1 问题
利用binlog恢复库表,要求如下:

启用binlog日志
创建db1库tb1表,插入3条记录
删除tb1表中刚插入的3条记录
使用mysqlbinlog恢复删除的3条记录
5.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:启用binlog日志

1)调整/etc/my.cnf配置,并重启服务

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
server_id=1 //定义server_id
log-bin=mysql-bin //定义日志名
binlog_format=”mixed” //定义日志格式
[root@dbsvr1 ~]# systemctl restart mysqld.service //重启服务
2)确认binlog日志文件

新启用binlog后,每次启动MySQl服务都会新生成一份日志文件:

[root@dbsvr1 ~]# ls /var/lib/mysql/mysql-bin.*
/var/lib/mysql/mysql-bin.000001 /var/lib/mysql/mysql-bin.index
其中mysql-bin.index文件记录了当前保持的二进制文件列表:

[root@dbsvr1 ~]# cat /var/lib/mysql/mysql-bin.index
./mysql-bin.000001
重启MySQL服务程序,或者执行SQL操作“FLUSH LOGS;”,会生成一份新的日志:

[root@dbsvr1 ~]# ls /var/lib/mysql/mysql-bin.*
/var/lib/mysql/mysql-bin.000001 /var/lib/mysql/mysql-bin.index
/var/lib/mysql/mysql-bin.000002
[root@dbsvr1 ~]# cat /var/lib/mysql/mysql-bin.index
./mysql-bin.000001
./mysql-bin.000002
步骤二:利用binlog日志重做数据库操作

1)执行数据库表添加操作

创建db1·库tb1表,表结构自定义:

mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.05 sec)
mysql> USE db1;
Database changed
mysql> CREATE TABLE tb1(
-> id int(4) NOT NULL,name varchar(24)
-> );
Query OK, 0 rows affected (0.28 sec)
插入3条表记录:

mysql> INSERT INTO tb1 VALUES
-> (1,‘Jack’),
-> (2,‘Kenthy’),
-> (3,‘Bob’);
Query OK, 3 rows affected (0.12 sec)
Records: 3 Duplicates: 0 Warnings: 0
确认插入的表记录数据:

mysql> SELECT * FROM tb1;
±—±-------+
| id | name |
±—±-------+
| 1 | Jack |
| 2 | Kenthy |
| 3 | Bob |
±—±-------+
3 rows in set (0.00 sec)
2)删除前一步添加的3条表记录

执行删除所有表记录操作:

mysql> DELETE FROM tb1;
Query OK, 3 rows affected (0.09 sec)
确认删除结果:

mysql> SELECT * FROM tb1;
Empty set (0.00 sec)
步骤三:通过binlog日志恢复表记录

binlog会记录所有的数据库、表更改操作,所以可在必要的时候重新执行以前做过的一部分数据操作,但对于启用binlog之前已经存在的库、表数据将不适用。

根据上述“恢复被删除的3条表记录”的需求,应通过mysqlbinlog工具查看相关日志文件,找到删除这些表记录的时间点,只要恢复此前的SQL操作(主要是插入那3条记录的操作)即可。

1)查看mysql-bin.000002日志内容

[root@dbsvr1 ~]# mysqlbinlog /var/lib/mysql/mysql-bin.000002
/!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1/;
/!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0/;
DELIMITER /!/;

at 4

#170412 12:05:32 server id 1 end_log_pos 123 CRC32 0x6d8c069c Start: binlog v 4, server v 5.7.17-log created 170412 12:05:32 at startup

Warning: this binlog is either in use or was not closed properly.

ROLLBACK/!/;
BINLOG ’
jKftWA8BAAAAdwAAAHsAAAABAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACMp+1YEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AZwGjG0=
'/!/;

at 123

#170412 12:05:32 server id 1 end_log_pos 154 CRC32 0x17f50164 Previous-GTIDs

[empty]

at 154

#170412 12:05:59 server id 1 end_log_pos 219 CRC32 0x4ba5a976 Anonymous_GTID last_committed=0 sequence_number=1
SET @@SESSION.GTID_NEXT= ‘ANONYMOUS’/!/;

at 219

#170412 12:05:59 server id 1 end_log_pos 310 CRC32 0x5b66ae13 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1491969959/!/;
SET @@session.pseudo_thread_id=3/!/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/!/;
SET @@session.sql_mode=1436549152/!/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/!/;
/!\C utf8 //!/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/!/;
SET @@session.lc_time_names=0/!/;
SET @@session.collation_database=DEFAULT/!/;
CREATE DATABASE db1
/!/;

at 310

#170412 12:06:23 server id 1 end_log_pos 375 CRC32 0x2967cc28 Anonymous_GTID last_committed=1 sequence_number=2
SET @@SESSION.GTID_NEXT= ‘ANONYMOUS’/!/;

at 375

#170412 12:06:23 server id 1 end_log_pos 502 CRC32 0x5de09aae Query thread_id=3 exec_time=0 error_code=0
use db1/!/;
SET TIMESTAMP=1491969983/!/;
CREATE TABLE tb1(
id int(4) NOT NULL,name varchar(24)
)
/!/;

at 502

#170412 12:06:55 server id 1 end_log_pos 567 CRC32 0x0b8cd418 Anonymous_GTID last_committed=2 sequence_number=3
SET @@SESSION.GTID_NEXT= ‘ANONYMOUS’/!/;

at 567

#170412 12:06:55 server id 1 end_log_pos 644 CRC32 0x7e8f2fa0 Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1491970015/!/;
BEGIN
/!/;

at 644

#170412 12:06:55 server id 1 end_log_pos 772 CRC32 0x4e3f728e Query thread_id=3 exec_time=0 error_code=0 //插入表记录的起始时间点
SET TIMESTAMP=1491970015/!/;
INSERT INTO tb1 VALUES(1,‘Jack’),(2,‘Kenthy’), (3,‘Bob’)
/!/;

at 772

#170412 12:06:55 server id 1 end_log_pos 803 CRC32 0x6138b21f Xid = 10
//确认事务的时间点
COMMIT/!/;

at 803

#170412 12:07:24 server id 1 end_log_pos 868 CRC32 0xbef3f472 Anonymous_GTID last_committed=3 sequence_number=4
SET @@SESSION.GTID_NEXT= ‘ANONYMOUS’/!/;

at 868

#170412 12:07:24 server id 1 end_log_pos 945 CRC32 0x5684e92c Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1491970044/!/;
BEGIN
/!/;

at 945

#170412 12:07:24 server id 1 end_log_pos 1032 CRC32 0x4c1c75fc Query thread_id=3 exec_time=0 error_code=0 //删除表记录的时间点
SET TIMESTAMP=1491970044/!/;
DELETE FROM tb1
/!/;

at 1032

#170412 12:07:24 server id 1 end_log_pos 1063 CRC32 0xccf549b2 Xid = 12
COMMIT/!/;
SET @@SESSION.GTID_NEXT= ‘AUTOMATIC’ /* added by mysqlbinlog / /!*/;
DELIMITER ;

End of log file

/!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE/;
/!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0/;
2) 执行指定Pos节点范围内的sql命令恢复数据

根据上述日志分析,只要恢复从2014.01.12 20:12:14到2014.01.12 20:13:50之间的操作即可。可通过mysqlbinlog指定时间范围输出,结合管道交给msyql命令执行导入重做:

[root@dbsvr1 ~]# mysqlbinlog
–start-datetime=“2017-04-12 12:06:55” \
–stop-datetime=“2017-04-12 12:07:23”
/var/lib/mysql/mysql-bin.000002 | mysql -u root -p
Enter password: //验证口令
3)确认恢复结果

mysql> SELECT * FROM db1.tb1;
±—±-------+
| id | name |
±—±-------+
| 1 | Jack |
| 2 | Kenthy |
| 3 | Bob |
±—±-------+
3 rows in set (0.00 sec)

Top
NSD RDBM2 DAY04
案例1:准备MHA集群环境
案例2:部署MHA集群
案例3:测试配置
1 案例1:准备MHA集群环境
1.1 问题
配置SSH免密登录
安装依赖包
配置MySQL一主多从结构
1.2 方案
准备5台虚拟机,角色规划如图-1所示。


图-1

IP规划,如图-2所示:


图-2

1.3 步骤
实现此案例需要按照如下步骤进行。

步骤一: 配置ssh免密登录

1)配置数据库服务器192.168.4.51

[root@host51 ~]# ssh-keygen //创建秘钥对

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): //回车

Enter passphrase (empty for no passphrase): //回车

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:qb7EZByHad3Jadr+zkiEbo7ZKGmCNlctgp+Wfp3Yad0 root@pxcnode71

The key’s randomart image is:

±–[RSA 2048]----+

| |

| + o o |

| = o * |

| o o * |

| . = S o |

| . . * + o |

| … =.O * + |

|.o.*+= & o E |

|. =+…B.o …+ |

±—[SHA256]-----+

[root@host51 ~]#

[root@host51 ~]# ssh-copy-id root@192.168.4.52 //传递公钥给host52主机

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys

root@192.168.4.71’s password: //输入host52主机系统管理员root用户密码

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘root@192.168.4.52’”

and check to make sure that only the key(s) you wanted were added.

[root@host51 ~]#

[root@host51 ~]# ssh-copy-id root@192.168.4.53 //传递公钥给host53主机

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys

root@192.168.4.71’s password: //输入host53主机系统管理员root用户密码

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘root@192.168.4.53’”

and check to make sure that only the key(s) you wanted were added.

[root@host51 ~]#

[root@host51 ~]# ssh root@192.168.4.52 //可以无密码连接52主机

Last login: Fri Jun 21 13:21:39 2019 from 192.168.4.254

.-"""-.

/ .===. \

/ 6 6 /

( ___/ )

ooo_/__________

/ \

| I am Virtual Host ! ! ! |

_______________ooo/

| | |

|_ | _|

| | |

|||

/-‘Y’-\

(__/ __)

[root@host52 ~]#

[root@host52 ~]# exit //断开连接

登出

Connection to 192.168.4.52 closed.

[root@host51 ~]#

[root@host51 ~]# ssh root@192.168.4.53 //可以无密码连接52主机

Last login: Fri Jun 21 09:01:15 2019 from 192.168.4.254

.-"""-.

/ .===. \

/ 6 6 /

( ___/ )

ooo_/__________

/ \

| I am Virtual Host ! ! ! |

_______________ooo/

| | |

|_ | _|

| | |

|||

/-‘Y’-\

(__/ __)

[root@host53 ~]# exit//断开连接

登出

Connection to 192.168.4.53 closed.

[root@host51 ~]#

2)配置数据库服务器192.168.4.52

[root@host52 ~]# ssh-keygen //创建秘钥对

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): //回车

Enter passphrase (empty for no passphrase): //回车

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:qb7EZByHad3Jadr+zkiEbo7ZKGmCNlctgp+Wfp3Yad0 root@pxcnode71

The key’s randomart image is:

±–[RSA 2048]----+

| |

| + o o |

| = o * |

| o o * |

| . = S o |

| . . * + o |

| … =.O * + |

|.o.*+= & o E |

|. =+…B.o …+ |

±—[SHA256]-----+

[root@host52 ~]#

[root@host52 ~]# ssh-copy-id root@192.168.4.51 //传递公钥给host51主机

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys

root@192.168.4.51’s password: //输入host51主机系统管理员root用户密码

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘root@192.168.4.51’”

and check to make sure that only the key(s) you wanted were added.

[root@host52 ~]#

[root@host52 ~]# ssh-copy-id root@192.168.4.53 //传递公钥给host53主机

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys

root@192.168.4.53’s password: //输入host53主机系统管理员root用户密码

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘root@192.168.4.53’”

and check to make sure that only the key(s) you wanted were added.

[root@host52 ~]#

[root@host52 ~]# ssh root@192.168.4.51 //可以无密码连接51主机

Last login: Fri Jun 21 13:21:39 2019 from 192.168.4.254

.-"""-.

/ .===. \

/ 6 6 /

( ___/ )

ooo_/__________

/ \

| I am Virtual Host ! ! ! |

_______________ooo/

| | |

|_ | _|

| | |

|||

/-‘Y’-\

(__/ __)

[root@host51 ~]#

[root@host51 ~]# exit //断开连接

登出

Connection to 192.168.4.52 closed.

[root@host52 ~]#

[root@host52 ~]# ssh root@192.168.4.53 //可以无密码连接53主机

Last login: Fri Jun 21 09:01:15 2019 from 192.168.4.254

.-"""-.

/ .===. \

/ 6 6 /

( ___/ )

ooo_/__________

/ \

| I am Virtual Host ! ! ! |

_______________ooo/

| | |

|_ | _|

| | |

|||

/-‘Y’-\

(__/ __)

[root@host53 ~]# exit//断开连接

登出

Connection to 192.168.4.53 closed.

[root@host52 ~]#

3)配置数据库服务器192.168.4.53

[root@host53 ~]# ssh-keygen //创建秘钥对

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): //回车

Enter passphrase (empty for no passphrase): //回车

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:qb7EZByHad3Jadr+zkiEbo7ZKGmCNlctgp+Wfp3Yad0 root@pxcnode71

The key’s randomart image is:

±–[RSA 2048]----+

| |

| + o o |

| = o * |

| o o * |

| . = S o |

| . . * + o |

| … =.O * + |

|.o.*+= & o E |

|. =+…B.o …+ |

±—[SHA256]-----+

[root@host53 ~]#

[root@host53 ~]# ssh-copy-id root@192.168.4.51 //传递公钥给host51主机

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys

root@192.168.4.51’s password: //输入host51主机系统管理员root用户密码

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘root@192.168.4.51’”

and check to make sure that only the key(s) you wanted were added.

[root@host53 ~]#

[root@host53 ~]# ssh-copy-id root@192.168.4.52 //传递公钥给host52主机

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys

root@192.168.4.52’s password: //输入host52主机系统管理员root用户密码

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘root@192.168.4.52’”

and check to make sure that only the key(s) you wanted were added.

[root@host53 ~]#

[root@host53 ~]# ssh root@192.168.4.51 //可以无密码连接51主机

Last login: Fri Jun 21 13:21:39 2019 from 192.168.4.254

.-"""-.

/ .===. \

/ 6 6 /

( ___/ )

ooo_/__________

/ \

| I am Virtual Host ! ! ! |

_______________ooo/

| | |

|_ | _|

| | |

|||

/-‘Y’-\

(__/ __)

[root@host51 ~]#

[root@host51 ~]# exit //断开连接

登出

Connection to 192.168.4.51 closed.

[root@host53 ~]#

[root@host53 ~]# ssh root@192.168.4.52 //可以无密码连接52主机

Last login: Fri Jun 21 09:01:15 2019 from 192.168.4.254

.-"""-.

/ .===. \

/ 6 6 /

( ___/ )

ooo_/__________

/ \

| I am Virtual Host ! ! ! |

_______________ooo/

| | |

|_ | _|

| | |

|||

/-‘Y’-\

(__/ __)

[root@host52 ~]# exit//断开连接

登出

Connection to 192.168.4.52 closed.

[root@host53 ~]#

步骤二: 安装依赖包

1)配置数据库服务器192.168.4.51

[root@host51 ~]# yum -y install perl-* //安装系统自带的perl软件包

[root@host51 ~]# cd mha-soft-student

[root@host51 ~]# yum –y install perl-* //安装共享的perl软件包

2)配置数据库服务器192.168.4.52

[root@host52 ~]# yum -y install perl-* //安装系统自带的perl软件包
[root@host52 ~]# cd mha-soft-student
[root@host52 ~]# yum –y install perl-* //安装共享的perl软件包[root@localhost
3)配置数据库服务器192.168.4.53

[root@host53 ~]# yum -y install perl-* //安装系统自带的perl软件包

[root@host53 ~]# cd mha-soft-student

[root@host53 ~]# yum –y install perl-* //安装共享的perl软件包

4)配置管理服务器192.168.4.57

[root@mgm57 ~]# yum -y install perl-* //安装系统自带的perl软件包

[root@mgm57 ~]# cd mha-soft-student

[root@mgm57 ~]# yum –y install perl-* //安装共享的perl软件包

步骤三: 配置MySQL一主多从结构

1)配置主服务器192.168.4.51

[root@host51 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master51 //日志名
server_id=51 //指定server_id
:wq
[root@host51 ~]# systemctl restart mysqld
[root@host51 ~]# mysql -uroot -p123qqq…A
mysql> grant replication slave on . to repluser@"%" identified by “123qqq…A"; //添加从服务器同步数据连接用户
mysql> show master status; //查看日志信息
mysql: [Warning] Using a password on the command line interface can be insecure.
±----------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±----------------±---------±-------------±-----------------±------------------+
| master51.000001 | 441 | | | |
±----------------±---------±-------------±-----------------±------------------+
[root@host51 ~]#
2)配置从服务器192.168.4.52

[root@host52 ~]# vim /etc/my.cnf

[mysqld]

server_id=52 //指定server_id

:wq

[root@host52 ~]# systemctl restart mysqld //重启数据库服务

[root@host52 ~]# mysql -uroot –p123qqq…A //数据库管理员登录

mysql> change master to //指定主服务器信息

master_host=“192.168.4.51”, //IP地址

master_user=“repluser”, //授权用户

master_password=“123qqq…A”, //授权用户密码

master_log_file=“master51.000001”, //binlog日志

master_log_pos=441; //偏移量

mysql> start slave; //启动slave进程

mysql> exit ; //断开连接

[root@host52 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep 192.168.4.51

Master_Host: 192.168.4.51 //主服务器Ip地址

[root@host52 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep –i yes

Slave_IO_Running: Yes //I0线程正常

Slave_SQL_Running: Yes //SQL线程正常

3)配置从服务器192.168.4.53

[root@host53 ~]# vim /etc/my.cnf

[mysqld]

server_id=53 //指定server_id

:wq

[root@host53 ~]# systemctl restart mysqld //重启数据库服务

[root@host53 ~]# mysql -uroot –p123qqq…A //数据库管理员登录

mysql> change master to //指定主服务器信息

master_host=“192.168.4.51”, //IP地址

master_user=“repluser”, //授权用户

master_password=“123qqq…A”, //授权用户密码

master_log_file=“master51.000001”, //binlog日志

master_log_pos=441; //偏移量

mysql> start slave; //启动slave进程

mysql> exit ; //断开连接

[root@host53 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep 192.168.4.51

Master_Host: 192.168.4.51 //主服务器Ip地址

[root@host53 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep –i yes

Slave_IO_Running: Yes //I0线程正常

Slave_SQL_Running: Yes //SQL线程正常

2 案例2:部署MHA集群
2.1 问题
配置管理节点
配置数据节点
2.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:配置管理节点

1)安装软件

[root@mgm57 ~]# cd mha-soft-student/
[root@mgm57 mha-soft-student]#
[root@mgm57 mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm//安装mha-node软件包
准备中… ################################# [100%]
正在升级/安装…
1:mha4mysql-node-0.56-0.el6 ################################# [100%]
[root@mgm57 mha-soft-student]#
[root@mgm57 mha-soft-student]# rpm -qa | grep mha //查看是否安装成功
mha4mysql-node-0.56-0.el6.noarch
[root@mgm57 mha-soft-student]#
[root@mgm57 mha-soft-student]# tar -zxvf mha4mysql-manager-0.56.tar.gz //解压mha-manager软件包
mha4mysql-manager-0.56/
mha4mysql-manager-0.56/debian/
mha4mysql-manager-0.56/debian/control
mha4mysql-manager-0.56/debian/copyright
……
……
[root@mgm57 mha-soft-student]# ls
app1.cnf mha4mysql-manager-0.56
mha4mysql-node-0.56-0.el6.noarch.rpm
master_ip_failover mha4mysql-manager-0.56.tar.gz
[root@mgm57 mha-soft-student]# cd mha4mysql-manager-0.56 //进入源码目录
[root@mgm57 mha4mysql-manager-0.56]# ls //查看文件列表
AUTHORS COPYING inc Makefile.PL META.yml rpm t
bin debian lib MANIFEST README samples tests
[root@mgm57 mha4mysql-manager-0.56]#
[root@mgm57 mha4mysql-manager-0.56]# perl Makefile.PL //配置
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies…
[Core Features]

  • DBI …loaded. (1.627)
  • DBD::mysql …loaded. (4.023)
  • Time::HiRes …loaded. (1.9725)
  • Config::Tiny …loaded. (2.14)
  • Log::Dispatch …loaded. (2.41)
  • Parallel::ForkManager …loaded. (1.18)
  • MHA::NodeConst …loaded. (0.56)
    *** Module::AutoInstall configuration finished.
    Checking if your kit is complete…
    Looks good
    Writing Makefile for mha4mysql::manager
    Writing MYMETA.yml and MYMETA.json
    [root@mgm57 mha4mysql-manager-0.56]# make //编译
    [root@mgm57 mha4mysql-manager-0.56]# make install //安装
    [root@mgm57 mha4mysql-manager-0.56]# ls /root/perl5/bin //查看安装的命令
    masterha_check_repl masterha_conf_host masterha_master_switch
    masterha_check_ssh masterha_manager masterha_secondary_check
    masterha_check_status masterha_master_monitor masterha_stop
    2)编辑主配置文件

[root@mgm57 ~ ]# mkdir /etc/mha //创建工作目录
[root@mgm57 ~ ]# cp mha4mysql-manager-0.56/sample/conf/app1.cnf /etc/mha/ //拷贝模板文件
[root@mgm57 ~ ]# vim /etc/mha/app1.cnf //编辑主配置文件
[server default] //管理服务默认配置
manager_workdir=/etc/mha //工作目录
manager_log=/etc/mha/manager.log //日志文件
master_ip_failover_script=/etc/mha/master_ip_failover //故障切换脚本
ssh_user=root //访问ssh服务用户
ssh_port=22 //ssh服务端口
repl_user=repluser //主服务器数据同步授权用户
repl_password=123qqq…A //密码
user=root //监控用户
password=123qqq…A //密码
[server1] //指定第1台数据库服务器
hostname=192.168.4.51 //服务器ip地址
port=3306 //服务端口
candidate_master=1 //竞选主服务器
[server2] //指定第2台数据库服务器
hostname=192.168.4.52
port=3306
candidate_master=1

[server3] //指定第3台数据库服务器
hostname=192.168.4.53
port=3306
candidate_master=1
:wq
3)创建故障切换脚本

[root@mgm57 ~]# cp mha-soft-student/master_ip_failover /etc/mha/
[root@mgm57 ~]# vim +35 /etc/mha/master_ip_failover
my $vip = ‘192.168.4.100/24’; # Virtual IP //定义VIP地址
my k e y = " 1 " ; / / 定 义 变 量 key = "1"; //定义变量 key="1";//key
my s s h s t a r t v i p = " / s b i n / i f c o n f i g e t h 0 : ssh_start_vip = "/sbin/ifconfig eth0: sshstartvip="/sbin/ifconfigeth0:key $vip"; //部署vip地址命令
my s s h s t o p v i p = " / s b i n / i f c o n f i g e t h 0 : ssh_stop_vip = "/sbin/ifconfig eth0: sshstopvip="/sbin/ifconfigeth0:key down"; //释放vip地址命令
:wq
[root@mgm57 ~]# chmod +x /etc/mha/master_ip_failover //给脚本加执行权限
4)在当前主服务器部署vip地址

[root@host51 ~]# ifconfig eth0:1 //部署之前查看
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 52:54:00:d8:10:d7 txqueuelen 1000 (Ethernet)
[root@host51 ~]# ifconfig eth0:1 192.168.4.100 //部署vip地址
[root@host51 ~]# ifconfig eth0:1 //部署后查看
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255
ether 52:54:00:d8:10:d7 txqueuelen 1000 (Ethernet)
步骤二:配置数据节点

1)在所有数据库服务器上,安装mha-node软件包

]# cd /root/mha-soft-student/
]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
准备中… ################################# [100%]
正在升级/安装…
1:mha4mysql-node-0.56-0.el6 ################################# [100%]
2)在所有数据服务器上添加监控用户

可以只在host51主机执行授权命令,host52和host53 会自动同步授权

]# mysql –uroot –p密码
mysql> grant all on . to root@"%" identified by “123qqq…A”;
mysql> exit;
3)在2台从服务器上添加,数据同步连接用户

在从服务器host52添加用户

[root@host52]# mysql –uroot –p密码
mysql> grant replication slave on . to repluser@"%" identified by “123qqq…A”;
mysql> exit;
在从服务器host53添加用户

[root@host53]# mysql –uroot –p密码
mysql> grant replication slave on . to repluser@"%" identified by “123qqq…A”;
mysql> exit;
4)修改数据库服务运行参数

修改主服务器host51

[root@host51 ~]# vim /etc/my.cnf
[mysqld]
plugin-load=“rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so” //加载模块
rpl_semi_sync_master_enabled=1 //启用master模块
rpl_semi_sync_slave_enabled=1 //启用slave模块
relay_log_purge=0 //禁止自动删除中继日志文件
:wq
[root@host51 ~]# systemctl restart mysqld //重启服务
修改从服务器host52

[root@host52 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master52
plugin-load=“rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so” //加载模块
rpl_semi_sync_master_enabled=1 //启用master模块
rpl_semi_sync_slave_enabled=1 //启用slave模块
relay_log_purge=0 //禁止自动删除中继日志文件
:wq
[root@host52 ~]# systemctl restart mysqld //重启服务
修改从服务器host53

[root@host53 ~]# vim /etc/my.cnf

[mysqld]

log-bin=master53

plugin-load=“rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so” //加载模块

rpl_semi_sync_master_enabled=1 //启用master模块

rpl_semi_sync_slave_enabled=1 //启用slave模块

relay_log_purge=0 //禁止自动删除中继日志文件

:wq

[root@host53 ~]# systemctl restart mysqld //重启服务

3 案例3:测试配置
3.1 问题
测试集群环境
访问集群
测试高可用
修复故障服务器
3.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:测试集群环境

1)在管理主机,测试ssh配置

[root@mgm57 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf //执行测试命令
Thu Jun 20 15:33:48 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jun 20 15:33:48 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf…
Thu Jun 20 15:33:48 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
Thu Jun 20 15:33:48 2019 - [info] Starting SSH connection tests…
Thu Jun 20 15:33:49 2019 - [debug]
Thu Jun 20 15:33:48 2019 - [debug] Connecting via SSH from root@192.168.4.51(192.168.4.51:22) to root@192.168.4.52(192.168.4.52:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:49 2019 - [debug] Connecting via SSH from root@192.168.4.51(192.168.4.51:22) to root@192.168.4.53(192.168.4.53:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:50 2019 - [debug]
Thu Jun 20 15:33:49 2019 - [debug] Connecting via SSH from root@192.168.4.52(192.168.4.52:22) to root@192.168.4.51(192.168.4.51:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:49 2019 - [debug] Connecting via SSH from root@192.168.4.52(192.168.4.52:22) to root@192.168.4.53(192.168.4.53:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:50 2019 - [debug] ok.
Thu Jun 20 15:33:50 2019 - [debug] Connecting via SSH from root@192.168.4.53(192.168.4.53:22) to root@192.168.4.52(192.168.4.52:22)…
Thu Jun 20 15:33:50 2019 - [debug] ok.
Thu Jun 20 15:33:51 2019 - [info] All SSH connection tests passed successfully.//测试成功提示
2)在管理主机,测试主从同步

[root@host57 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf //执行测试命令
Thu Jun 20 15:37:46 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
Thu Jun 20 15:37:46 2019 - [info] MHA::MasterMonitor version 0.56.
Thu Jun 20 15:37:47 2019 - [info] GTID failover mode = 0
Thu Jun 20 15:37:47 2019 - [info] Dead Servers: //没有停止的mysql服务器
Thu Jun 20 15:37:47 2019 - [info] Alive Servers://运行mysql服务主机列表
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.51(192.168.4.51:3306)
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.52(192.168.4.52:3306)
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.53(192.168.4.53:3306)
Thu Jun 20 15:37:47 2019 - [info] Alive Slaves:
Thu Jun 20 15:37:47 2019 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.53(192.168.4.53:3306) Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Jun 20 15:37:47 2019 - [info] Replicating from 192.168.4.51(192.168.4.51:3306)
Thu Jun 20 15:37:47 2019 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Jun 20 15:37:47 2019 - [info] Current Alive Master: 192.168.4.51(192.168.4.51:3306)
Thu Jun 20 15:37:47 2019 - [info] Checking slave configurations…
Thu Jun 20 15:37:47 2019 - [info] read_only=1 is not set on slave 192.168.4.52(192.168.4.52:3306).
Thu Jun 20 15:37:47 2019 - [info] read_only=1 is not set on slave 192.168.4.53(192.168.4.53:3306).
Thu Jun 20 15:37:47 2019 - [info] Checking replication filtering settings…
Thu Jun 20 15:37:47 2019 - [info] binlog_do_db= , binlog_ignore_db=
Thu Jun 20 15:37:47 2019 - [info] Replication filtering check ok.
Thu Jun 20 15:37:47 2019 - [info] GTID (with auto-pos) is not supported
Thu Jun 20 15:37:47 2019 - [info] Starting SSH connection tests…
Thu Jun 20 15:37:49 2019 - [info] All SSH connection tests passed successfully.
Thu Jun 20 15:37:49 2019 - [info] Checking MHA Node version…
Thu Jun 20 15:37:50 2019 - [info] Version check ok.
Thu Jun 20 15:37:50 2019 - [info] Checking SSH publickey authentication settings on the current master…
Thu Jun 20 15:37:50 2019 - [info] HealthCheck: SSH to 192.168.4.51 is reachable.
Thu Jun 20 15:37:50 2019 - [info] Master MHA Node version is 0.56.
Thu Jun 20 15:37:50 2019 - [info] Checking recovery script configurations on 192.168.4.51(192.168.4.51:3306)…
Thu Jun 20 15:37:50 2019 - [info] Connecting to root@192.168.4.51(192.168.4.51:22)…
Creating /var/tmp if not exists… ok.
Checking output directory is accessible or not…
ok.
Binlog found at /var/lib/mysql, up to master51.000002
Thu Jun 20 15:37:50 2019 - [info] Binlog setting check done.
Thu Jun 20 15:37:50 2019 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers…
Thu Jun 20 15:37:50 2019 - [info] Connecting to root@192.168.4.52(192.168.4.52:22)…
Checking slave recovery environment settings…
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to host52-relay-bin.000006
Temporary relay log file is /var/lib/mysql/host52-relay-bin.000006
Testing mysql connection and privileges…mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output… done.
Cleaning up test file(s)… done.
Thu Jun 20 15:37:51 2019 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user=‘root’ --slave_host=192.168.4.53 --slave_ip=192.168.4.53 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.17-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx
Thu Jun 20 15:37:51 2019 - [info] Connecting to root@192.168.4.53(192.168.4.53:22)…
Checking slave recovery environment settings…
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to host53-relay-bin.000006
Temporary relay log file is /var/lib/mysql/host53-relay-bin.000006
Testing mysql connection and privileges…mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output… done.
Cleaning up test file(s)… done.
Thu Jun 20 15:37:52 2019 - [info] Slaves settings check done.
Thu Jun 20 15:37:52 2019 - [info]
192.168.4.51(192.168.4.51:3306) (current master)
±-192.168.4.52(192.168.4.52:3306)
±-192.168.4.53(192.168.4.53:3306)
Thu Jun 20 15:37:52 2019 - [info] Checking replication health on 192.168.4.52…
Thu Jun 20 15:37:52 2019 - [info] ok.
Thu Jun 20 15:37:52 2019 - [info] Checking replication health on 192.168.4.53…
Thu Jun 20 15:37:52 2019 - [info] ok.
Thu Jun 20 15:37:52 2019 - [info] Checking master_ip_failover_script status:
Thu Jun 20 15:37:52 2019 - [info] /etc/mha/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.4.51 --orig_master_ip=192.168.4.51 --orig_master_port=3306
Thu Jun 20 15:37:52 2019 - [info] OK.
Thu Jun 20 15:37:52 2019 - [warning] shutdown_script is not defined.
Thu Jun 20 15:37:52 2019 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.//测试成功提示信息
3)启动管理服务

[root@mgm57 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf
–ignore_last_failover //执行启动命令
Thu Jun 20 17:05:58 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jun 20 17:05:58 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf…
Thu Jun 20 17:05:58 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
4)查看服务状态

[root@mgm57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf//执行命令
app1 (pid:15806) is running(0:PING_OK), master:192.168.4.51 //服务运行,监视主服务器192.168.4.51
[root@mgm57 ~]# ls /etc/mha/ //查看工作目录文件列表
app1.cnf app1.master_status.health manager.log master_ip_failover
步骤二:访问集群

1)在主服务器51 添加访问数据的连接用户

]# mysql -uroot -p123qqq…A
mysql> create database db9;
Query OK, 1 row affected (0.05 sec)
mysql> create table db9.a (id int);
Query OK, 0 rows affected (0.63 sec)
mysql> grant select,insert on db9.* to yaya55@"%" identified by “123qqq…A”;
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql>exit
2)客户端50 连接vip地址访问集群

host50~]# mysql -h192.168.4.100 -uyaya55 -p123qqq…A
mysql> select * from db9.a;
mysql> insert into db9.a values(100);
mysql> select * from db9.a;
±-----+
| id |
±-----+
| 100 |
±-----+
1 row in set (0.00 sec)
mysql>exit
3)在从服务器host52 查看数据

[root@host52 ~]# mysql -uroot -p123qqq…A -e “select * from db9.a”
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----+
| id |
±-----+
| 100 |
±-----+
4)在从服务器host53 查看数据

[root@host53 ~]# mysql -uroot -p123qqq…A -e “select * from db9.a”
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----+
| id |
±-----+
| 100 |
±-----+
步骤三:测试高可用

1)停止主服务器51的mysql服务

host51~]# systemctl stop mysqld
2)查看管理服务 ,输出的监控信息

[root@mgm57~]#masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf \

–ignore_last_failover
Thu Jun 20 17:05:58 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jun 20 17:05:58 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf…
Thu Jun 20 17:05:58 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
Creating /var/tmp if not exists… ok.
Checking output directory is accessible or not…
ok.
Binlog found at /var/lib/mysql, up to master51.000002
Thu Jun 20 17:35:59 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jun 20 17:35:59 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf…
Thu Jun 20 17:35:59 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
[root@host57 ~]#
[root@mgm57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 is stopped(2:NOT_RUNNING). //监控到主服务器宕机 管理服务自动停止
[root@mgm57 ~]#
3)客户端依然连接vip地址,可以访问到数据

client50]# ping -c 2 192.168.4.100 //能够ping通vip地址
PING 192.168.4.100 (192.168.4.100) 56(84) bytes of data.
64 bytes from 192.168.4.100: icmp_seq=1 ttl=255 time=0.222 ms
64 bytes from 192.168.4.100: icmp_seq=2 ttl=255 time=0.121 ms
— 192.168.4.71 ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.121/0.171/0.222/0.052 ms
client50]# mysql -h192.168.4.100 -uyaya55 -p123qqq…A //连接vip地址
mysql> insert into db9.a values(200); //插入记录
mysql> select * from db9.a;//查询记录
±-----+
| id |
±-----+
| 100 |
| 200 |
±-----+
4)查看vip地址

在host52主机查看到vip地址,说明host52 主机被选举为主服务器

[root@host52 ~]# ifconfig eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255
ether 52:54:00:f5:c4:6a txqueuelen 1000 (Ethernet)
在host53主机未查看到vip地址,说明host53主机是当前host52的从服务器

[root@host53 ~]# ifconfig eth0:1 //未查到vip地址
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 52:54:00:28:22:2e txqueuelen 1000 (Ethernet)
[root@host53 ~]# mysql -uroot -p123qqq…A -e “show slave status\G” | grep -i 192
mysql: [Warning] Using a password on the command line interface can be insecure.
Master_Host: 192.168.4.52 //主服务器Ip地址
[root@host53 ~]#
[root@host53 ~]# mysql -uroot -p123qqq…A -e “show slave status\G” | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes //IO线程正常
Slave_SQL_Running: Yes //SQL线程正常
[root@host53 ~]# mysql -uroot -p123qqq…A -e “select * from db9.a” //自动同步数据
mysql: [Warning] Using a password on the command line interface can be insecure.
±-----+
| id |
±-----+
| 100 |
| 200 |
±-----+
步骤四:修复故障服务器

host51~]# systemctl start mysqld
[root@host52 ~]# mysqldump -uroot -p123qqq…A --master-data db9 > db9.sql //在主服务器host52 做完全备份
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@host52 ~]#
[root@host52 ~]# scp db9.sql root@192.168.4.51:/root/ //拷贝备份文件给host51主机
db9.sql 100% 1918 3.1MB/s 00:00
[root@host52 ~]#
host51 ~]# mysql -uroot -p123qqq…A db9 < /root/db9.sql//host51 主机使用备份文件恢复数据
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@host51 ~]# grep master52 /root/db9.sql //查看日志名及偏移量
CHANGE MASTER TO MASTER_LOG_FILE=‘master52.000001’, MASTER_LOG_POS=895;
[root@host51 ~]# mysql -uroot -p123qqq…A
mysql>change master to master_host=“192.168.4.52”,master_user=“repluser”,master_password=“123qqq…A”,master_log_file=“master52.000001”,master_log_pos=895;
Query OK, 0 rows affected, 2 warnings (0.14 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
Mysql> exit ;
[root@host51 ~]# mysql -uroot -p123qqq…A -e “show slave status\G” |grep 192.168.4.52
mysql: [Warning] Using a password on the command line interface can be insecure.
Master_Host: 192.168.4.52 //主服务器ip地址
[root@host51 ~]#
[root@host51 ~]# mysql -uroot -p123qqq…A -e “show slave status\G” |grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes //IO线程状态正常
Slave_SQL_Running: Yes //SQL线程状态正常
[root@host51 ~]#
]# vim /etc/mha/app1.cnf
[server1 ]
hostname=192.168.4.51
port=3306
candidate_master=1
:wq
[root@mgm57 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf //测试SSH
Thu Jun 20 15:33:48 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jun 20 15:33:48 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf…
Thu Jun 20 15:33:48 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
Thu Jun 20 15:33:48 2019 - [info] Starting SSH connection tests…
Thu Jun 20 15:33:49 2019 - [debug]
Thu Jun 20 15:33:48 2019 - [debug] Connecting via SSH from root@192.168.4.51(192.168.4.51:22) to root@192.168.4.52(192.168.4.52:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:49 2019 - [debug] Connecting via SSH from root@192.168.4.51(192.168.4.51:22) to root@192.168.4.53(192.168.4.53:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:50 2019 - [debug]
Thu Jun 20 15:33:49 2019 - [debug] Connecting via SSH from root@192.168.4.52(192.168.4.52:22) to root@192.168.4.51(192.168.4.51:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:49 2019 - [debug] Connecting via SSH from root@192.168.4.52(192.168.4.52:22) to root@192.168.4.53(192.168.4.53:22)…
Thu Jun 20 15:33:49 2019 - [debug] ok.
Thu Jun 20 15:33:50 2019 - [debug] ok.
Thu Jun 20 15:33:50 2019 - [debug] Connecting via SSH from root@192.168.4.53(192.168.4.53:22) to root@192.168.4.52(192.168.4.52:22)…
Thu Jun 20 15:33:50 2019 - [debug] ok.
Thu Jun 20 15:33:51 2019 - [info] All SSH connection tests passed successfully.//成功
[root@mgm57 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf//测试主从同步
Thu Jun 20 15:37:46 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
Thu Jun 20 15:37:46 2019 - [info] MHA::MasterMonitor version 0.56.
Thu Jun 20 15:37:47 2019 - [info] GTID failover mode = 0
Thu Jun 20 15:37:47 2019 - [info] Dead Servers:
Thu Jun 20 15:37:47 2019 - [info] Alive Servers:
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.51(192.168.4.51:3306)
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.52(192.168.4.52:3306)
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.53(192.168.4.53:3306)
Thu Jun 20 15:37:47 2019 - [info] Alive Slaves:
Thu Jun 20 15:37:47 2019 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Jun 20 15:37:47 2019 - [info] 192.168.4.53(192.168.4.53:3306) Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Jun 20 15:37:47 2019 - [info] Replicating from 192.168.4.51(192.168.4.51:3306)
Thu Jun 20 15:37:47 2019 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Jun 20 15:37:47 2019 - [info] Current Alive Master: 192.168.4.51(192.168.4.51:3306)
Thu Jun 20 15:37:47 2019 - [info] Checking slave configurations…
Thu Jun 20 15:37:47 2019 - [info] read_only=1 is not set on slave 192.168.4.52(192.168.4.52:3306).
Thu Jun 20 15:37:47 2019 - [info] read_only=1 is not set on slave 192.168.4.53(192.168.4.53:3306).
Thu Jun 20 15:37:47 2019 - [info] Checking replication filtering settings…
Thu Jun 20 15:37:47 2019 - [info] binlog_do_db= , binlog_ignore_db=
Thu Jun 20 15:37:47 2019 - [info] Replication filtering check ok.
Thu Jun 20 15:37:47 2019 - [info] GTID (with auto-pos) is not supported
Thu Jun 20 15:37:47 2019 - [info] Starting SSH connection tests…
Thu Jun 20 15:37:49 2019 - [info] All SSH connection tests passed successfully.
Thu Jun 20 15:37:49 2019 - [info] Checking MHA Node version…
Thu Jun 20 15:37:50 2019 - [info] Version check ok.
Thu Jun 20 15:37:50 2019 - [info] Checking SSH publickey authentication settings on the current master…
Thu Jun 20 15:37:50 2019 - [info] HealthCheck: SSH to 192.168.4.51 is reachable.
Thu Jun 20 15:37:50 2019 - [info] Master MHA Node version is 0.56.
Thu Jun 20 15:37:50 2019 - [info] Checking recovery script configurations on 192.168.4.51(192.168.4.51:3306)…
Thu Jun 20 15:37:50 2019 - [info] Connecting to root@192.168.4.51(192.168.4.51:22)…
Creating /var/tmp if not exists… ok.
Checking output directory is accessible or not…
ok.
Binlog found at /var/lib/mysql, up to master51.000002
Thu Jun 20 15:37:50 2019 - [info] Binlog setting check done.
Thu Jun 20 15:37:50 2019 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers…
Thu Jun 20 15:37:50 2019 - [info] Connecting to root@192.168.4.52(192.168.4.52:22)…
Checking slave recovery environment settings…
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to host52-relay-bin.000006
Temporary relay log file is /var/lib/mysql/host52-relay-bin.000006
Testing mysql connection and privileges…mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output… done.
Cleaning up test file(s)… done.
Thu Jun 20 15:37:51 2019 - [info] Connecting to root@192.168.4.53(192.168.4.53:22)…
Checking slave recovery environment settings…
Opening /var/lib/mysql/relay-log.info … ok.
Relay log found at /var/lib/mysql, up to host53-relay-bin.000006
Temporary relay log file is /var/lib/mysql/host53-relay-bin.000006
Testing mysql connection and privileges…mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output… done.
Cleaning up test file(s)… done.
Thu Jun 20 15:37:52 2019 - [info] Slaves settings check done.
Thu Jun 20 15:37:52 2019 - [info]
192.168.4.51(192.168.4.51:3306) (current master)
±-192.168.4.52(192.168.4.52:3306)
±-192.168.4.53(192.168.4.53:3306)
Thu Jun 20 15:37:52 2019 - [info] Checking replication health on 192.168.4.52…
Thu Jun 20 15:37:52 2019 - [info] ok.
Thu Jun 20 15:37:52 2019 - [info] Checking replication health on 192.168.4.53…
Thu Jun 20 15:37:52 2019 - [info] ok.
Thu Jun 20 15:37:52 2019 - [info] Checking master_ip_failover_script status:
Thu Jun 20 15:37:52 2019 - [info] /etc/mha/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.4.51 --orig_master_ip=192.168.4.51 --orig_master_port=3306
Thu Jun 20 15:37:52 2019 - [info] OK.
Thu Jun 20 15:37:52 2019 - [warning] shutdown_script is not defined.
Thu Jun 20 15:37:52 2019 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK. //成功
]# masterha_stop --conf=/etc/mha/app1.cnf //停止管理服务
Stopped app1 successfully.
]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf
–ignore_last_failover //启动管理服务
Thu Jun 20 17:05:58 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jun 20 17:05:58 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf…
Thu Jun 20 17:05:58 2019 - [info] Reading server configuration from /etc/mha/app1.cnf…
mgm57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:15806) is running(0:PING_OK), master:192.168.4.52 //服务运行,监视服务器52
[root@mgm57 ~]#

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值