mysql cp複製和mysqldump備份測試分析

來源:互聯網
上載者:User

備份策略

針對不同的情境下, 我們應該制定不同的備份策略對資料庫進行備份, 一般情況下, 備份策略一般為以下三種:

直接cp,tar複製資料庫檔案
mysqldump 複製BIN LOGS
lvm2快照 複製BIN LOGS
xtrabackup
以上的幾種解決方案分別針對於不同的情境
如果資料量較小, 可以使用第一種方式, 直接複製資料庫檔案
如果資料量還行, 可以使用第二種方式, 先使用mysqldump對資料庫進行完全備份, 然後定期備份BINARY LOG達到增量備份的效果
如果資料量一般, 而又不過分影響業務運行, 可以使用第三種方式, 使用lvm2的快照對資料檔案進行備份, 而後定期備份BINARY LOG達到增量備份的效果
如果資料量很大, 而又不過分影響業務運行, 可以使用第四種方式, 使用xtrabackup進行完全備份後, 定期使用xtrabackup進行增量備份或差異備份
實戰演練

cp複製資料檔案備份及恢複

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.03 sec)

mysql> create database test;
Query OK, 1 row affected (0.03 sec)

mysql> use test
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table a(id int,name varchar(10));
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
+----------------+
1 row in set (0.00 sec)

mysql> insert into a(id,name)values(1,'gao')
    -> ;
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from a
    -> ;
+------+------+
| id   | name |
+------+------+
|    1 | gao  |
+------+------+
1 row in set (0.00 sec)
 
[root@my57 mysql]# mkdir /backup

[root@my57 mysql]# cp -a /data/mysql/data/* /backup/

[root@my57 mysql]# ls /backup/
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  my57.err  my57.pid  mysql  mysqld_safe.pid  performance_schema  sys  test  xtrabackup_info

[root@my57 mysql]# rm -rf /data/mysql/data/*
[root@my57 mysql]# service mysql restart

 ERROR! MySQL server PID file could not be found!
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/data//my57.pid).
這時啟動不了,我們再把備份檔案拷貝回來,在啟動就可以了。

cp -a /backup/* /data/mysql/data/
mysqldump的複製與恢複

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.02 sec)

mysql> use test;
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> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
| b              |
+----------------+
2 rows in set (0.00 sec)
mysql> select * from b;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

# 記住備份前position的值
mysql> show master status
    -> ;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| bin-log.000001 |      567 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
開始備份

[root@my57 data]# mysqldump --all-databases --lock-all-tables  > /backup/backup.sql
再建立一個資料庫做增量測試

mysql> create database test1;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
| test1              |
+--------------------+
6 rows in set (0.00 sec)

# 再記下現在的position位置
mysql> SHOW MASTER STATUS;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| bin-log.000001 |      869 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
備份二進位日誌

cp bin-log.000001 /backup/
停止mysql在啟動,編譯安裝的啟動不了,必須重新初始化

[root@my57 data]# service mysql stop
Shutting down MySQL. SUCCESS!

# 類比刪除資料檔案
[root@my57 data]# rm -rf *

# 編譯安裝的資料庫刪除資料檔案後啟動不了
[root@my57 data]# service mysql start
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/data//my57.pid).

# 重新初始化mysql
[root@my57 data]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql
2016-06-14T02:43:04.084864Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-06-14T02:43:04.084944Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2016-06-14T02:43:04.084952Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2016-06-14T02:43:04.459619Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-06-14T02:43:04.502310Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-06-14T02:43:04.567746Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b7fa4c2e-31d9-11e6-983a-080027fff0b0.
2016-06-14T02:43:04.571117Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-06-14T02:43:04.575259Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

# 啟動
[root@my57 data]# service mysql start
Starting MySQL. SUCCESS!

# 利用原來的別分還原,發現還原了 但是缺少test1
mysql> source backup.sql

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

# 利用mysqlbinlog二進位恢複test1,這時上面的開始位置和結束位置就有用了
[root@my57 backup]# mysqlbinlog --start-position=567 --stop-position=869 /backup/bin-log.000001 |mysql -uroot -p
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
| test1              |
+--------------------+
6 rows in set (0.00 sec)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.