1. 程式人生 > >mysqldump全量備份與恢復

mysqldump全量備份與恢復

一個 like secure exit extra ssi 統計表 建議 per

mysql用戶管理

創建授權用戶和密碼 ?grant all on . to ‘user1‘ identified by ‘pwd@1234‘;
在mysql中創建連接管理數據庫的用戶,創建用戶可以為這個用戶訪問指定庫分配相應的權限
創建用戶並授權用戶訪問某個庫,有通過本地訪問所有庫的權限,並查看該用戶再數據庫中保存的權限記錄

mysql> grant all on *.* to ‘user1‘ identified by ‘pwd@1234‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show grants for user1;
+--------------------------------------------+
| Grants for user1@% ? ? ? ? ? ? ? ? ? ? ? ? |
+--------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘user1‘@‘%‘ |
+--------------------------------------------+
1 row in set (0.00 sec)

創建用戶並和密碼指定訪問權限和主機 ?grant SELECT,UPDATE,INSERT on nice. to ‘user1‘@‘192.168.1.234‘ identified by ‘pwd@123‘;
創建某個用戶時為其指定特定的權限,只允許部分sql操作,控制其權限訪問
使用show grants for ?可以查看對應的某條授權數據,如對user這個mysql用戶授權訪問的主機、權限、訪問庫的信息,mysql5.7版本不會再顯示授權用戶的加密密碼*

mysql> grant SELECT,UPDATE,INSERT on nice.* to ‘user1‘@‘192.168.1.234‘ identified by ‘pwd@123‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show grants for user1@‘192.168.1.234‘;
+---------------------------------------------------------------------+
| Grants for [email protected] ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  |
+---------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user1‘@‘192.168.1.234‘ ? ? ? ? ? ? ? ? ? ? ? |
| GRANT SELECT, INSERT, UPDATE ON `nice`.* TO ‘user1‘@‘192.168.1.234‘ |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

查詢授權用戶的創建數據 ?show grants for user2@‘192.168.1.234‘;
在mysql5.7版本後,show grants查詢出來的用戶授權語句不再顯示授權用戶加密密碼這個信息了,在show grants查詢出來的結果中,可以使用復制其結果的方式復用來創建其他用戶或授權,但由於不再顯示加密密碼,還是建議只采用之前創建用戶使用的權限和指定授權庫,使用grant最初的方式創建
使用show grants for ?只能查詢出授權用戶時的授權語句,語句中不包含用戶登錄時需要認證的加密後的密碼
查詢的授權用戶的記錄數據如下:

mysql> show grants for user2@‘192.168.1.234‘;
+---------------------------------------------------------------------+
| Grants for [email protected] ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  |
+---------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user2‘@‘192.168.1.234‘ ? ? ? ? ? ? ? ? ? ? ? |
| GRANT SELECT, INSERT, UPDATE ON `nice`.* TO ‘user2‘@‘192.168.1.234‘ |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

常用sql語句

統計數據表裏的數據行數 *select count() from mysql.user;*
統計表的行數,不建議數據過多時執行統計,因為統計大量數據會需要很長查詢時間和消耗服務器資源*

mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 6 ? ? ?  |
+----------+
1 row in set (0.00 sec)

查詢指定數據表裏存儲的所有內容 select from mysql.db\G;
查詢數據庫的所有內容,如這裏查看授權的數據,不建議這樣的查詢操作,因為如果數據較多的話,會造成查詢時間過長,耗費很高的服務器資源,\G以更易讀的方式顯示
查詢結果顯示了user2用戶通過1.234的主機訪問nice庫的授權

mysql> select * from mysql.db\G;
*************************** 3. row ***************************
 ? ? ? ?  Host: 192.168.1.234
 ? ? ? ?  Db: nice
 ? ? ? ?  User: user2
 ? ? ? ?  Select_priv: Y
 ? ? ? ?  Insert_priv: Y
 ? ? ? ?  Update_priv: Y
 ? ? ? ?  Delete_priv: N

查詢數據庫中的某幾個字段 select db,user from mysql.db;
查詢一個表中的某一個或兩個字段,from指定查詢的庫和表,select後指定查詢的字段。會列出查詢字段下的所有數據

mysql> select db,user from mysql.db;
+--------------------+---------------+
| db ? ? ? ? ? ? ? ? | user ? ? ? ?  |
+--------------------+---------------+
| nice ? ? ? ? ? ? ? | user1 ? ? ? ? |
| nice ? ? ? ? ? ? ? | user2 ? ? ? ? |
| performance_schema | mysql.session |
| sys ? ? ? ? ? ? ?  | mysql.sys ? ? |
+--------------------+---------------+
4 rows in set (0.00 sec)

模糊匹配查詢 ? ?select from mysql.db where host like ‘192.168.%‘\G;
模糊匹配查詢某些字段,如要查詢mysql的授權用戶的信息,查詢結果包括了授權用戶名和允許訪問的主機ip地址,\G以易讀的方式顯示

mysql> select * from mysql.db where host like ‘192.168.%‘\G;
*************************** 1. row ***************************
 ? ? ? ?  Host: 192.168.1.234
 ? ? ? ?  Db: nice
 ? ? ? ?  User: user1
 ? ? ? ?  Select_priv: Y
 ? ? ? ?  Insert_priv: Y

在數據表中插入一條數據 ? insert into holle.tb1 values (23, ‘2233‘);
查詢數據表中的字段結構,並在數據表中插入一條數據,這張演示的數據表只有數字和字符兩種數據類型

mysql> use holle;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_holle |
+-----------------+
| tb1 ? ? ? ? ? ? |
+-----------------+
1 row in set (0.00 sec)
mysql> desc holle.tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type ? ? | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id ?  | int(4) ? | YES  | ? ? | NULL ?  | ? ? ? |
| name  | char(40) | YES  | ? ? | NULL ?  | ? ? ? |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into holle.tb1 values (23, ‘2233‘);
Query OK, 1 row affected (0.01 sec)
mysql> select * from  holle.tb1;
+------+------+
| id ? | name |
+------+------+
| ? 23 | 2233 |
+------+------+
1 row in set (0.00 sec)

更新(更改)所有數據 ? update holle.tb1 set id=33 where name=‘23‘;
這個是更新表裏所有的數據,需要謹慎操作,會把表裏數據更新成同樣的數值,mysql5.7上無效。。在mysql5.7上執行操作語句,表中內容並未更新/更改

mysql> update holle.tb1 set id=33 where name=‘23‘;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> select * from holle.tb1;
+------+------+
| id ? | name |
+------+------+
| 23 ? | 2233 |
| 66 ? | 233  |
+------+------+
2 rows in set (0.00 sec)

清空指定表中的所有內容 ?truncate table holle.tb1;
操作會清空指定的表裏所有的數據,這裏清空holle庫裏的tb1表

mysql> select * from holle.tb1;
+------+------+
| id ? | name |
+------+------+
| 23 ? | 2233 |
| 66 ? | 233  |
+------+------+
2 rows in set (0.00 sec)
mysql> truncate table holle.tb1;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from holle.tb1;
Empty set (0.00 sec)

刪除數據表 ? drop table holle.tb1;
刪除指定的數據表,表內數據和結構會一起刪除掉**

mysql> show tables;
+-----------------+
| Tables_in_holle |
+-----------------+
| tb1 ? ? ? ? ? ? |
+-----------------+
1 row in set (0.00 sec)
mysql> drop table holle.tb1;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
Empty set (0.00 sec)

刪除某個數據庫 ??drop database holle;
刪除holle庫後,數據庫數量發生變化,由之前的6個庫變為5個*

mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| holle ? ? ? ? ? ?  |
| mysql ? ? ? ? ? ?  |
| nice ? ? ? ? ? ? ? |
| performance_schema |
| sys ? ? ? ? ? ? ?  |
+--------------------+
6 rows in set (0.00 sec)
mysql> drop database holle;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ?  |
| nice ? ? ? ? ? ? ? |
| performance_schema |
| sys ? ? ? ? ? ? ?  |
+--------------------+
5 rows in set (0.00 sec)

# mysql數據庫備份恢復

備份mysql庫 **?mysqldump -uroot -ppwd@123 mysql >/tmp/mysql.sql**
mysqldump 以全量方式備份,這種備份方式在linux系統中操作,只適合小量的數據庫備份,如果數據庫很大的話,使用這個備份方式會很耗費時間

[root@aaa ~]# mysqldump -uroot -ppwd@123 mysql >/tmp/mysql.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@aaa ~]# ll -h /tmp/mysql.sql 
-rw-r--r-- 1 root root 1.1M 8月 17 13:11 /tmp/mysql.sql

恢復mysql庫
首先需要在數據庫中創建一個空的庫,用於恢復數據庫存放的地方,恢復庫使用mysql命令,創建庫後退出數據庫登錄,使用mysql命令將備份文件重定向到新創建的空庫中,然後查看庫中的數據表

mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)
mysql> exit
Bye
[root@aaa ~]# mysql -uroot -ppwd@123 mysql2 < /tmp/mysql.sql 
mysql> use mysql2
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql2 ? ? ? ?  |
+---------------------------+
| columns_priv ? ? ? ? ? ?  |
| db ? ? ? ? ? ? ? ? ? ? ?  |
| engine_cost ? ? ? ? ? ? ? |

mysql備份表
備份數據表會連同表裏的數據一起備份到文件中

[root@aaa ~]# mysqldump -uroot -ppwd@123 mysql2 user > /tmp/user.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@aaa ~]# cat /tmp/user.sql 
-- MySQL dump 10.13 Distrib 5.7.22, for Linux (x86_64)
--
-- Host: localhost Database: mysql
-- ------------------------------------------------------
-- Server version   5.7.22
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

恢復user表

mysql> use mysql2;
mysql> drop table user;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@aaa ~]# mysql -uroot -ppwd@123 mysql2 < /tmp/user.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@aaa ~]# mysql -uroot -ppwd@123 
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 31
Server version: 5.7.22 Source distribution
mysql> use mysql2
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql2 ? ? ? ?  |
+---------------------------+
| columns_priv ? ? ? ? ? ?  |
| db ? ? ? ? ? ? ? ? ? ? ?  |
+----------省略部分顯示------+
| time_zone_transition_type |
| user ? ? ? ? ? ? ? ? ? ?  |
+---------------------------+
30 rows in set (0.01 sec)

備份所有的數據庫

[root@aaa ~]# mysqldump -uroot -p -A > /tmp/all.sql
Enter password: 
[root@aaa ~]# ll -h /tmp/all.sql 
-rw-r--r-- 1 root root 1.9M 8月 17 17:02 /tmp/all.sql

只備份數據庫表結構
備份時不存儲表中的數據,只把表的創建結構和語句備份出來,這裏查看部分表,columns_priv這個表的結構

[root@aaa ~]# mysqldump -uroot -ppwd@123 -d mysql2 > /tmp/user2.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@aaa ~]# cat /tmp/user2.sql |less
-- MySQL dump 10.13 Distrib 5.7.22, for Linux (x86_64)
--
-- Host: localhost Database: mysql2
-- ------------------------------------------------------
-- Server version 5.7.22
/*!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 `columns_priv`
--
DROP TABLE IF EXISTS `columns_priv`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `columns_priv` (
 ?`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
 ?`Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
 ?`User` char(32) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
 ?`Table_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
 ?`Column_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,
 ?`Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 ?`Column_priv` set(‘Select‘,‘Insert‘,‘Update‘,‘References‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘‘,
  PRIMARY KEY (`Host`,`Db`,`User`,`Table_name`,`Column_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘Column privileges‘;
/*!40101 SET character_set_client = @saved_cs_client */;

mysqldump全量備份與恢復