这里介绍在线备份NDB Cluster,除了可以通过正常mysqldump的工具来备份NDB集群之外,也可以通过ndb_mgm工具来备份NDB Cluster。
- 备份
- mysqldump工具备份
在任意一个SQL计算节点来备份即可,以下来备份cndba数据库。
- 备份cndba库
[root@NDB-SQL1 mysql]# mysqldump -uroot -p cndba > /tmp/cndba.sql
Enter password:
- 查看备份内容
备份内容和备份其他单实例数据库没什么区别。
[root@NDB-SQL1 mysql]# cat /tmp/cndba.sql
-- MySQL dump 10.13 Distrib 5.7.20-ndb-7.5.8, for linux-glibc2.12 (x86_64)
--
-- Host: localhost Database: cndba
-- ------------------------------------------------------
-- Server version 5.7.20-ndb-7.5.8-cluster-gpl
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `sihong`
--
DROP TABLE IF EXISTS `sihong`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `sihong` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL
) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `sihong`
--
LOCK TABLES `sihong` WRITE;
/*!40000 ALTER TABLE `sihong` DISABLE KEYS */;
INSERT INTO `sihong` VALUES (1,'ruyan'),(4,'dongsheng'),(3,'dongsheng'),(2,'wenqing');
/*!40000 ALTER TABLE `sihong` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2017-12-13 16:33:24
- ndb_mgm来备份
官方文档:https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-backup-concepts.html
在管理节点上输入ndb_mgm,然后通过start backup命令来备份。
- 查看帮助信息
ndb_mgm> ?
---------------------------------------------------------------------------
NDB Cluster -- Management Client -- Help
---------------------------------------------------------------------------
HELP Print help text
HELP COMMAND Print detailed help for COMMAND(e.g. SHOW)
SHOW Print information about cluster
CREATE NODEGROUP <id>,<id>... Add a Nodegroup containing nodes
DROP NODEGROUP <NG> Drop nodegroup with id NG
START BACKUP [NOWAIT | WAIT STARTED | WAIT COMPLETED]
START BACKUP [<backup id>] [NOWAIT | WAIT STARTED | WAIT COMPLETED]
START BACKUP [<backup id>] [SNAPSHOTSTART | SNAPSHOTEND] [NOWAIT | WAIT STARTED | WAIT COMPLETED]
Start backup (default WAIT COMPLETED,SNAPSHOTEND)
ABORT BACKUP <backup id> Abort backup
SHUTDOWN Shutdown all processes in cluster
PROMPT [<prompt-string>] Toggle the prompt between string specified
or default prompt if no string specified
CLUSTERLOG ON [<severity>] ... Enable Cluster logging
CLUSTERLOG OFF [<severity>] ... Disable Cluster logging
CLUSTERLOG TOGGLE [<severity>] ... Toggle severity filter on/off
CLUSTERLOG INFO Print cluster log information
<id> START Start data node (started with -n)
<id> RESTART [-n] [-i] [-a] [-f] Restart data or management server node
<id> STOP [-a] [-f] Stop data or management server node
ENTER SINGLE USER MODE <id> Enter single user mode
EXIT SINGLE USER MODE Exit single user mode
<id> STATUS Print status
<id> CLUSTERLOG {<category>=<level>}+ Set log level for cluster log
PURGE STALE SESSIONS Reset reserved nodeid's in the mgmt server
CONNECT [<connectstring>] Connect to management server (reconnect if already connected)
<id> REPORT <report-type> Display report for <report-type>
QUIT Quit management client
<severity> = ALERT | CRITICAL | ERROR | WARNING | INFO | DEBUG
<category> = STARTUP | SHUTDOWN | STATISTICS | CHECKPOINT | NODERESTART | CONNECTION | INFO | ERROR | CONGESTION | DEBUG | BACKUP | SCHEMA
<report-type> = BACKUPSTATUS | MEMORYUSAGE | EVENTLOG
<level> = 0 - 15
<id> = ALL | Any database node id
For detailed help on COMMAND, use HELP COMMAND.
- 备份数据包含三部分
- Metadata. The names and definitions of all database tables
- Table records. The data actually stored in the database tables at the time that the backup was made
- Transaction log. A sequential record telling how and when data was stored in the database
- 开始备份
- 备份数据
ndb_mgm> start backup;
Waiting for completed, this may take several minutes
Node 3: Backup 1
Node 3: Backup 1 started from node 1 completed
StartGCP: 4256 StopGCP: 4259
#Records: 2062 #LogRecords: 0
Data: 51440 bytes Log: 0 bytes
- 指定backup-ID备份数据节点
ndb_mgm> start backup 3;
Waiting for completed, this may take several minutes
Node 3: Backup 3 started from node 1
Node 3: Backup 3 started from node 1 completed
StartGCP: 4515 StopGCP: 4518
#Records: 2062 #LogRecords: 0
Data: 51440 bytes Log: 0 bytes
- WAIT和NOWAIT参数
这两个参数很明显就是是否等待备份完成再返回结果。NOWAIT让备份在后台执行,客户端无需等待信息返回。
如:
- START BACKUP NOWAIT;
- STARTUP BACKUP WAIT STARTED;
- 压缩备份
注意:要加SNAPSHOTSTART参数,并且配置文件中(config.ini)中启用压缩备份(默认是关闭的)。
如:
START BACKUP WAIT STARTED SNAPSHOTSTART;
Config.ini配置文件中的[ndbd default]下添加
CompressedBackup=true
- 说明备份日志
在备份日志中,需要注意"Backup 1",它表示该备份的唯一ID,如果做第二次备份,备份ID会变成"Backup 2"。当日志中显示"Node 2: Backup 1 started from node 1 completed"时,本次备份结束。备份的数据保存在每个NDB(数据节点)下,具体路径在$MYSQL_HOME/data/BACKUP/BACKUP-ID
#数据节点1的备份数据,如下:
[root@NDB-DATA1 BACKUP-1]# pwd
/usr/local/mysql/data/BACKUP/BACKUP-1
[root@NDB-DATA1 BACKUP-1]# ll
total 56
-rw-r--r-- 1 root root 26476 Dec 13 16:37 BACKUP-1-0.2.Data
-rw-r--r-- 1 root root 21892 Dec 13 16:37 BACKUP-1.2.ctl
-rw-r--r-- 1 root root 52 Dec 13 16:37 BACKUP-1.2.log
#数据节点2的备份数据,如下:
[root@NDB-DATA2 bin]# cd /usr/local/mysql/data/BACKUP/BACKUP-1/
[root@NDB-DATA2 BACKUP-1]# ll
total 56
-rw-r--r-- 1 root root 25508 Dec 13 16:37 BACKUP-1-0.3.Data
-rw-r--r-- 1 root root 21892 Dec 13 16:37 BACKUP-1.3.ctl
-rw-r--r-- 1 root root 52 Dec 13 16:37 BACKUP-1.3.log
可以发现备份文件的名称中有“.3”“.2”意思是各个节点的nodeid。
可以通过show来查看:
id=2
id=3
- 其他备份参数
- BackupDataBufferSize
The amount of memory used to buffer data before it is written to disk.
- BackupLogBufferSize
The amount of memory used to buffer log records before these are written to disk.
- BackupMemory
The total memory allocated in a data node for backups. This should be the sum of the memory allocated for the backup data buffer and the backup log buffer.
- BackupWriteSize
The default size of blocks written to disk. This applies for both the backup data buffer and the backup log buffer.
- BackupMaxWriteSize
The maximum size of blocks written to disk. This applies for both the backup data buffer and the backup log buffer.
- 恢复
恢复时是一个单独的工具,而不是集成在ndb-mgm中的命令,是ndb_restore恢复程序。该工具位于bin/目录下,复制过来即可。
cp /usr/local/mysql/bin/ndb_restore /usr/local/bin/
官方文档:https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-programs-ndb-restore.html
- 删除表sihong
mysql> drop table sihong;
Query OK, 0 rows affected (0.23 sec)
- 在数据节点1(192.168.1.123)执行恢复命令--恢复元数据
#ndb_restore -b 1 -n 2 -c host=192.168.1.120:1186 -m -r --backup_path=/usr/local/mysql/data/BACKUP/BACKUP-1/
Backup Id = 1
Nodeid = 2
backup path = /usr/local/mysql/data/BACKUP/BACKUP-1/
2017-12-13 17:54:12 [restore_metadata] Read meta data file header
Opening file '/usr/local/mysql/data/BACKUP/BACKUP-1/BACKUP-1.2.ctl'
File size 21892 bytes
Backup version in files: ndb-6.3.11 ndb version: mysql-5.7.20 ndb-7.5.8
2017-12-13 17:54:12 [restore_metadata] Load content
Stop GCP of Backup: 4258
2017-12-13 17:54:12 [restore_metadata] Get number of Tables
2017-12-13 17:54:12 [restore_metadata] Validate Footer
Connected to ndb!!
2017-12-13 17:54:12 [restore_metadata] Restore objects (tablespaces, ..)
2017-12-13 17:54:12 [restore_metadata] Restoring tables
Successfully restored table `cndba/def/sihong`
Successfully restored table event REPL$cndba/sihong
2017-12-13 17:54:12 [restore_metadata] Save foreign key info
Create foreign keys
Create foreign keys done
2017-12-13 17:54:12 [restore_data] Start restoring table data
2017-12-13 17:54:12 [restore_data] Read data file header
Opening file '/usr/local/mysql/data/BACKUP/BACKUP-1/BACKUP-1-0.2.Data'
File size 26476 bytes
2017-12-13 17:54:12 [restore_data] Restore fragments
_____________________________________________________
Processing data in table: mysql/def/ndb_index_stat_sample(7) fragment 0
_____________________________________________________
Processing data in table: sys/def/NDB$EVENTS_0(3) fragment 0
_____________________________________________________
Processing data in table: mysql/def/ndb_apply_status(9) fragment 0
_____________________________________________________
Processing data in table: mysql/def/ndb_index_stat_head(6) fragment 0
_____________________________________________________
Processing data in table: cndba/def/sihong(10) fragment 0
_____________________________________________________
Processing data in table: mysql/def/NDB$BLOB_4_3(5) fragment 0
_____________________________________________________
Processing data in table: sys/def/SYSTAB_0(2) fragment 0
_____________________________________________________
Processing data in table: mysql/def/ndb_schema(4) fragment 0
2017-12-13 17:54:12 [restore_log] Read log file header
Opening file '/usr/local/mysql/data/BACKUP/BACKUP-1/BACKUP-1.2.log'
File size 52 bytes
2017-12-13 17:54:12 [restore_log] Restore log entries
Restored 2 tuples and 0 log entries
NDBT_ProgramExit: 0 - OK
参数说明:
-b | Backupid |
-n | nodeid |
-c | --connectstring的别名 |
-m | 元数据 |
-r | restore恢复 |
--backup_path | 备份文件所在目录 |
更多参数参考官方文档:
https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-programs-ndb-restore.html
- 在数据节点2(192.168.1.124)恢复数据内容
注意:这里不要加“-m”参数了,因为不需要再恢复元数据了。
[root@NDB-DATA2 BACKUP-1]# ndb_restore -b 1 -n 3 -c host=192.168.1.120:1186 -r /usr/local/mysql/data/BACKUP/BACKUP-1/
Backup Id = 1
Nodeid = 3
backup path = /usr/local/mysql/data/BACKUP/BACKUP-1/
2017-12-13 17:58:00 [restore_metadata] Read meta data file header
Opening file '/usr/local/mysql/data/BACKUP/BACKUP-1/BACKUP-1.3.ctl'
File size 21892 bytes
Backup version in files: ndb-6.3.11 ndb version: mysql-5.7.20 ndb-7.5.8
2017-12-13 17:58:00 [restore_metadata] Load content
Stop GCP of Backup: 4258
2017-12-13 17:58:00 [restore_metadata] Get number of Tables
2017-12-13 17:58:00 [restore_metadata] Validate Footer
Connected to ndb!!
2017-12-13 17:58:01 [restore_metadata] Restore objects (tablespaces, ..)
2017-12-13 17:58:01 [restore_metadata] Restoring tables
2017-12-13 17:58:01 [restore_metadata] Save foreign key info
2017-12-13 17:58:01 [restore_data] Start restoring table data
2017-12-13 17:58:01 [restore_data] Read data file header
Opening file '/usr/local/mysql/data/BACKUP/BACKUP-1/BACKUP-1-0.3.Data'
File size 25508 bytes
2017-12-13 17:58:01 [restore_data] Restore fragments
_____________________________________________________
Processing data in table: mysql/def/ndb_index_stat_sample(7) fragment 1
_____________________________________________________
Processing data in table: sys/def/NDB$EVENTS_0(3) fragment 1
_____________________________________________________
Processing data in table: mysql/def/ndb_apply_status(9) fragment 1
_____________________________________________________
Processing data in table: mysql/def/ndb_index_stat_head(6) fragment 1
_____________________________________________________
Processing data in table: cndba/def/sihong(10) fragment 1
_____________________________________________________
Processing data in table: mysql/def/NDB$BLOB_4_3(5) fragment 1
_____________________________________________________
Processing data in table: sys/def/SYSTAB_0(2) fragment 1
_____________________________________________________
Processing data in table: mysql/def/ndb_schema(4) fragment 1
2017-12-13 17:58:01 [restore_log] Read log file header
Opening file '/usr/local/mysql/data/BACKUP/BACKUP-1/BACKUP-1.3.log'
File size 52 bytes
2017-12-13 17:58:01 [restore_log] Restore log entries
Restored 2 tuples and 0 log entries
NDBT_ProgramExit: 0 - OK
- 查看表及表中数据
mysql> show tables;
+-----------------+
| Tables_in_cndba |
+-----------------+
| sihong |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from sihong;
+------+-----------+
| id | name |
+------+-----------+
| 1 | ruyan |
| 4 | dongsheng |
| 3 | dongsheng |
| 2 | wenqing |
+------+-----------+
4 rows in set (0.00 sec)
可以看到,数据已经回来了。
还有更多的恢复参数,
例如:
#只恢复指定数据库
--include-databases=db1 --exclude-tables=db1.t1
#只恢复数据库中的指定表
--exclude-tables=db1.t1 --include-databases=db1
更多内容,移步官方文档:
https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-programs-ndb-restore.html
- 问题记录
- 在恢复过程中遇到错误
错误信息:
[root@NDB-DATA1 BACKUP-1]# ndb_restore -b 1 -n 2 -c NDB-MGM:1186 -m -r --backup_path=/usr/local/mysql/data/BACKUP/BACKUP-1/
Backup Id = 1
Nodeid = 2
backup path = /usr/local/mysql/data/BACKUP/BACKUP-1/
2017-12-13 17:41:09 [restore_metadata] Read meta data file header
Opening file '/usr/local/mysql/data/BACKUP/BACKUP-1/BACKUP-1.2.ctl'
File size 21892 bytes
Backup version in files: ndb-6.3.11 ndb version: mysql-5.7.20 ndb-7.5.8
2017-12-13 17:41:09 [restore_metadata] Load content
Stop GCP of Backup: 4258
2017-12-13 17:41:09 [restore_metadata] Get number of Tables
2017-12-13 17:41:09 [restore_metadata] Validate Footer
Configuration error: Error: Could not alloc node id at NDB-MGM port 1186: Connection done from wrong host ip 192.168.1.123.
Failed to initialize consumers
NDBT_ProgramExit: 1 - Failed
原因:
Config.ini配置文件中,没有空闲的mysqld节点。那么在配置文件中添加[mysqld]即可
解决:
[root@NDB-MGM mysql-cluster]# cat config.ini
[ndbd default]
NoOfReplicas=2
DataMemory=80M
IndexMemory=18M
...略
[mysqld]
NodeId=5
#HostName=198.168.1.122
HostName=NDB-SQL2
[mysqld] --新增这句即可
重新生效配置文件:
这里没有什么好方法,只能重启管理节点。
ndb_mgm> shutdown
ndb_mgmd -f /var/lib/mysql-cluster/config.ini --reload
ndb_mgm> show
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from NDB-DATA1)
id=3 (not connected, accepting connect from NDB-DATA2)
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.120 (mysql-5.7.20 ndb-7.5.8)
[mysqld(API)] 3 node(s)
id=4 (not connected, accepting connect from NDB-SQL1)
id=5 (not connected, accepting connect from NDB-SQL2)
id=6 (not connected, accepting connect from any host) --可以看到新增了一个节点
其他节点重启进程,重新加入到集群中即可。