mysqldump全量恢复_mysqldump全量备份与恢复

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 user1@192.168.1.234                                     |

+---------------------------------------------------------------------+

| 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 user2@192.168.1.234                                     |

+---------------------------------------------------------------------+

| 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 */;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值