目錄mysql
Linux-mysql備份linux
//第一步 下載二進制格式的mysql軟件包 [root@localhost ~]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz //第二步 建立用戶和組 [root@localhost ~]# groupadd -r mysql [root@localhost ~]# useradd mysql -M -s /sbin/nologin -g mysql //第三步 解壓軟件至/usr/local/ [root@localhost ~]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@localhost ~]# ls /usr/local/ bin etc games include lib lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 sbin share src // 第四步 將解壓後的mysql二進制文件軟鏈接到mysql [root@localhost local]# ln -s mysql-5.7.22-linux-glibc2.12-x86_64/ mysql //注意這個地方儘可能就用mysql這個名字,我在用其餘名字時初始化時會報錯 [root@localhost local]# ll 總用量 0 drwxr-xr-x. 2 root root 6 12月 15 2017 bin lrwxrwxrwx. 1 root root 36 4月 29 12:05 cwhmysql -> mysql-5.7.22-linux-glibc2.12-x86_64/ // 第五步 修改目錄/usr/local/mysql的屬主屬組 [root@localhost local]# chown mysql:mysql /usr/local/mysql [root@localhost local]# ll /usr/local/mysql/ -d drwxr-xr-x. 9 mysql mysql 129 4月 29 11:55 /usr/local/cwhmysql/ // 第六步添加環境變量 [root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile.d/mysql.sh [root@localhost local]# source /etc/profile.d/mysql.sh // 第七步創建數據存放目錄(數據庫數據存放的位置) [root@localhost local]# mkdir /opt/data [root@localhost local]# chown -R mysql.mysql /opt/data/ [root@localhost local]# ll -d /opt/data/ drwxr-xr-x. 2 mysql mysql 6 4月 29 13:58 /opt/data/ // 第八步初始化數據庫 [root@localhost local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/ 2019-04-29T07:08:48.132084Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-04-29T07:08:48.620302Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-04-29T07:08:48.686189Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-04-29T07:08:48.749259Z 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: a2c71314-6a4d-11e9-98ac-000c2938f528. 2019-04-29T07:08:48.750621Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-04-29T07:08:48.752402Z 1 [Note] A temporary password is generated for root@localhost: JX5JLlCryl(w //注意最後這個JX5JLlCryl(w 是初始化後生成的臨時隨機密碼在第一次登錄時須要用到 // 第九步生成配置文件 [root@localhost local]# cat > /etc/my.cnf << EOF > [mysqld] > basedir = /usr/local/mysql > datadir = /opt/data > socket = /tmp/mysql.sock > port = 3306 > pid-file = /opt/data/mysql.pid > user = mysql > skip-name-resolve //若開啓該選項,則全部遠程主機鏈接受權都要使用IP地址方 式不然MySQL將沒法正常處理鏈接請求 > EOF // 第十步配置服務啓動腳本 [root@localhost local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld //將mysql的服務文件複製到init.d中去 [root@localhost local]# vim /etc/init.d/mysqld basedir=/usr/local/mysql datadir=/opt/data //找到上面兩行加入mysql的路徑和數據庫存放的路徑 // 第十一步啓動服務 [root@localhost local]# service mysqld start Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'. . SUCCESS! // 第十二步使用臨時密碼登陸 [root@localhost local]# cat /root/passwd JX5JLlCryl(w //初始化密碼我將他保存在這個文件裏 [root@localhost local]# mysql -uroot -p Enter password: //此處密碼爲當時初始化時所給的密碼 mysql> // 第十三步修改密碼 mysql> set password = password('cwh123!'); Query OK, 0 rows affected, 1 warning (0.00 sec)
配置文件查找次序:若在多個配置文件中均有設定,則最後找到的最終生效sql
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
mysql經常使用配置文件參數:數據庫
參數 | 說明 |
---|---|
port = 3306 | 設置監聽端口 |
socket = /tmp/mysql.sock | 指定套接字文件位置 |
basedir = /usr/local/mysql | 指定MySQL的安裝路徑 |
datadir = /data/mysql | 指定MySQL的數據存放路徑 |
pid-file = /data/mysql/mysql.pid | 指定進程ID文件存放路徑 |
user = mysql | 指定MySQL以什麼用戶的身份提供服務 |
skip-name-resolve | 禁止MySQL對外部鏈接進行DNS解析 使用這一選項能夠消除MySQL進行DNS解析的時間. 若開啓該選項,則全部遠程主機鏈接受權都要使用IP地址方式不然MySQL將沒法正常處理鏈接請求 |
備份方案 | 特色 |
---|---|
全量備份 | 全量備份就是指對某一個時間點上的全部數據或應用進行的一個徹底拷貝 數據恢復快 備份時間長 |
增量備份 | 增量備份是指在一次全備份或上一次增量備份後,之後每次的備份只需備份 與前一次相比增長和者被修改的文件。這就意味着,第一次增量備份的對象 是進行全備後所產生的增長和修改的文件;第二次增量備份的對象是進行第一次增量 備份後所產生的增長和修改的文件,如此類推 |
差別備份 | 備份上一次的徹底備份後發生變化的全部文件 差別備份是指在一次全備份後到進行差別備份的這段時間內 對那些增長或者修改文件的備份。在進行恢復時,咱們只需對第一次全量備份和最後一次差別備份進行恢復 |
/語法: mysqldump [OPTIONS] database [tables ...] mysqldump [OPTIONS] --all-databases [OPTIONS] mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] //經常使用的OPTIONS: -uUSERNAME //指定數據庫用戶名 -hHOST //指定服務器主機,請使用ip地址 -pPASSWORD //指定數據庫用戶的密碼 -P# //指定數據庫監聽的端口,這裏的#需用實際的端口號代替,如-P3306
1. 備份整個數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> quit Bye [root@localhost ~]# mysqldump -uroot -p -hlocalhost --all-databases > /mysql_bak/all-201904291618.sql Enter password: [root@localhost ~]# ls /mysql_bak/ all-201904291618.sql // -h後能夠不加這個選項默認是本地 備份恢復操做 mysql> show databases ; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> drop database chengweihong; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) //而後在進行恢復操做 [root@localhost ~]# mysql -uroot -p < /mysql_bak/all-201904291618.sql Enter password: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
2.備份chengweihong庫的cwh表和ddl表 mysql> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | +------------------------+ 1 row in set (0.00 sec) mysql> create table ddl(name varchar(50) not null,age tinyint not null); Query OK, 0 rows affected (0.01 sec) mysql> insert into ddl value('dd',30); Query OK, 1 row affected (0.00 sec) mysql> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | | ddl | +------------------------+ 2 rows in set (0.00 sec) mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | | ddl | +------------------------+ 2 rows in set (0.00 sec) mysql> quit Bye [root@localhost ~]# mysqldump -uroot -p chengweihong cwh ddl > /mysql_bak/table-cwhandddl-201904291701.sql Enter password: [root@localhost ~]# ls /mysql_bak/ all-201904291618.sql table-cwhandddl-201904291701.sql //備份恢復操做 //首先備份chengweihong庫下得兩個表 [root@localhost ~]# mysqldump -uroot -p chengweihong cwh ddl > /mysql_bak/table-201904291716.sql //模擬一個誤刪除得操做刪除cwh表 mysql> use chengweihong; 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_chengweihong | +------------------------+ | cwh | | ddl | +------------------------+ 2 rows in set (0.00 sec) mysql> drop table cwh; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | ddl | +------------------------+ 1 row in set (0.00 sec) //在進入mysql進行恢復 mysql> use chengweihong 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> source /mysql_bak/table-201904291716.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) . . . Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | | ddl | +------------------------+ 2 rows in set (0.00 sec)
第一步,開啓MySQL服務器的二進制日誌功能vim
[mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve server-id = 1 //設置服務器標識符 log-bin = runtime_binlog //開啓二進制日誌功能 [root@localhost ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! //重啓服務 //此時在/opt/data下會有一個runtime_binlog.000001得文件就是同個這個文件進行備份恢復
第二步,對現有數據庫進行進行差別備份得徹底備份服務器
[root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.7.22-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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 databses; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databses' at line 1 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use chengweihong; 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_chengweihong | +------------------------+ | cwh | | ddl | +------------------------+ 2 rows in set (0.00 sec) mysql> select * from cwh; +--------+-----+ | name | age | +--------+-----+ | tom | 10 | | jerry | 20 | | lisi | 30 | | wangwu | 40 | +--------+-----+ 4 rows in set (0.00 sec) mysql> select * from ddl; +------+-----+ | name | age | +------+-----+ | dd | 30 | +------+-----+ 1 row in set (0.00 sec) mysql> insert into ddl value('cc',40); Query OK, 1 row affected (0.01 sec) mysql> select * from ddl; +------+-----+ | name | age | +------+-----+ | dd | 30 | | cc | 40 | +------+-----+ 2 rows in set (0.00 sec) mysql> quit Bye [root@localhost ~]# mysqldump -uroot -p --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > /mysql_bak/all-201904301411.sql Enter password: [root@localhost ~]# ls /opt/data/ auto.cnf ib_logfile0 mysql runtime_binlog.index chengweihong ib_logfile1 mysql.pid sys ib_buffer_pool ibtmp1 performance_schema ibdata1 localhost.localdomain.err runtime_binlog.000004
第三步,往數據庫得表裏增長新內容併發
// 原有得表 mysql> use chengweihong; mysql> select * from cwh; +--------+-----+ | name | age | +--------+-----+ | tom | 10 | | jerry | 20 | | lisi | 30 | | wangwu | 40 | +--------+-----+ 4 rows in set (0.00 sec) //往表裏新增內容 mysql> insert into cwh value('zhaoliu',50); Query OK, 1 row affected (0.00 sec) mysql> select * from cwh; +---------+-----+ | name | age | +---------+-----+ | tom | 10 | | jerry | 20 | | lisi | 30 | | wangwu | 40 | | zhaoliu | 50 | +---------+-----+ 5 rows in set (0.00 sec)
第四步,模擬誤刪除數據庫app
mysql> drop database chengweihong; Query OK, 2 rows affected (2.29 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
第五步,用普通徹底備份恢復數據庫dom
[root@localhost ~]# mysql -uroot -p < /mysql_bak/all-201904301430.sql Enter password: [root@localhost ~]# mysql -uroot -p -e "select * from chengweihong.cwh;" Enter password: +--------+-----+ | name | age | +--------+-----+ | tom | 10 | | jerry | 20 | | lisi | 30 | | wangwu | 40 | +--------+-----+ //發現備份以後增長得數據並由被恢復
第六步,用差別備份恢復數據庫socket
[root@localhost ~]# mysql -uroot -p -e "show binlog events in 'runtime_binlog.000006';" |grep drop Enter password: runtime_binlog.000006 497 Query 1 613 drop database chengweihong [root@localhost ~]# mysqlbinlog --stop-position=497 /opt/data/runtime_binlog.000006 |mysql -uroot -pcwh123! mysql: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 35 Server version: 5.7.22-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> use chengweihong; 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 * from cwh; +---------+-----+ | name | age | +---------+-----+ | tom | 10 | | jerry | 20 | | lisi | 30 | | wangwu | 40 | | zhaoliu | 50 | +---------+-----+ 5 rows in set (0.00 sec) //能夠看出在徹底備份以後添加得也恢復了
[root@cwh ~]# vim /etc/my.cnf [mysqld] skip-grant-tables //增長這一行,而後重啓服務
[root@cwh ~]# mysql mysql> use mysql; mysql> select * from user \G *************************** 1. row *************************** ----- authentication_string: *376C8403A2C3233B69F4763C3E428E4ED7314D42 ----- //在5.7中密碼步在時password二十authentication_string mysql> UPDATE user SET password(或authentication_string)=password("123456") WHERE user='root'; mysql> flush privileges; mysql> exit;
往/etc/.my.cnf 中寫入篇日誌文件
[root@cwh ~]# vim .my.cnf [client] user=root //你要無祕登陸的用戶 password=cl //用戶的密碼
給不一樣不一樣的客戶端使用
[root@cwh ~]# vim .my.cnf mysql] //給/usr/local/mysql/bin/mysql使用 user="root" password="123456" [mysqladmin] //給/usr/local/mysql/bin/mysqladmin使用 user="root" password="123456"
Xtrabackup
percona提供的mysql數據庫備份工具,唯一開源的可以對innodb和xtradb數據庫進行熱備的工具
特色:
Xtrabackup2.2版以前包括4個可執行文件:
注意:
xtrabackup 是用來備份 InnoDB 表的,不能備份非 InnoDB 表,和 mysqld server 沒有交互;innobackupex 腳本用來備份非InnoDB 表,同時會調用 xtrabackup 命令來備份 InnoDB 表,還會和 mysqld server 發送命令進行交互,如加讀鎖(FTWRL)、獲取位點(SHOW SLAVESTATUS)等。即innobackupex是在xtrabackup 之上作了一層封裝實現的。
雖然目前通常不用 MyISAM 表,只是mysql 庫下的系統表是MyISAM 的,所以備份基本都經過 innobackupex 命令進行
xtrabackup的特性:
使用innobakupex備份時,其會調用xtrabackup備份全部的InnoDB表,複製全部關於表結構定義的相關文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相關文件,同時還會備份觸發器和數據庫配置信息相關的文件。這些文件會被保存至一個以時間命名的目錄中,在備份時,innobackupex還會在備份目錄中建立以下文件:
xtrabackup用法
備份:innobackupex [option] BACKUP-ROOT-DIR 選項說明: --user:該選項表示備份帳號 --password:該選項表示備份的密碼 --host:該選項表示備份數據庫的地址 --databases:該選項接受的參數爲數據名,若是要指定多個數據庫,彼此間須要以空格隔開;如:"xtra_test dba_test",同時,在指定某數據庫時,也能夠只指定其中的某張表。如:"mydatabase.mytable"。該選項對innodb引擎表無效,仍是會備份全部innodb表 --defaults-file:該選項指定了從哪一個文件讀取MySQL配置,必須放在命令行第一個選項的位置 --incremental:該選項表示建立一個增量備份,須要指定--incremental- basedir --incremental-basedir:該選項表示接受了一個字符串參數指定含有full backup的目錄爲增量備份的base目錄,與--incremental同時使用 --incremental-dir:該選項表示增量備份的目錄 --include=name:指定表名,格式:databasename.tablename
Prepare:innobackupex --apply-log [option] BACKUP-DIR 選項說明: --apply-log:通常狀況下,在備份完成後,數據尚且不能用於恢復操做,由於備份的數據中可能會包含還沒有提交的事務或已經提交但還沒有同步至數據文件中的事務。所以,此時數據文件仍處理不一致狀態。此選項做用是經過回滾未提交的事務及同步已經提交的事務至數據文件使數據文件處於一致性狀態 --use-memory:該選項表示和--apply-log選項一塊兒使用,prepare 備份的時候,xtrabackup作crash recovery分配的內存大小,單位字節。也可(1MB,1M,1G,1GB),推薦1G --defaults-file:該選項指定了從哪一個文件讀取MySQL配置,必須放在命令行第一個選項的位置 -export:表示開啓可導出單獨的表以後再導入其餘Mysql中 --redo-only:這個選項在prepare base full backup,往其中merge增量備份時候使用
Prepare:innobackupex --apply-log [option] BACKUP-DIR 選項說明: --apply-log:通常狀況下,在備份完成後,數據尚且不能用於恢復操做,由於備份的數據中可能會包含還沒有提交的事務或已經提交但還沒有同步至數據文件中的事務。所以,此時數據文件仍處理不一致狀態。此選項做用是經過回滾未提交的事務及同步已經提交的事務至數據文件使數據文件處於一致性狀態 --use-memory:該選項表示和--apply-log選項一塊兒使用,prepare 備份的時候,xtrabackup作crash recovery分配的內存大小,單位字節。也可(1MB,1M,1G,1GB),推薦1G --defaults-file:該選項指定了從哪一個文件讀取MySQL配置,必須放在命令行第一個選項的位置 -export:表示開啓可導出單獨的表以後再導入其餘Mysql中 --redo-only:這個選項在prepare base full backup,往其中merge增量備份時候使用
注意事項