MySQL--mysqldump的权限说明 - 蒋乐兴的技术随笔

时间:2024-03-10 07:48:49

MySQL--mysqldump的权限说明

mysqldump 所需要的权限说明:
  1、对于table 来说mysqldump 最少要有select 权限。
  2、对于view 来说mysqldump 要有show view 权限。
  3、对于trrigger 来说mysqldump 要有trriger 权限。
  4、如果要产生一份一致的备份 mysqldump 要有lock tables 权限。

 

相关用户创建与授权语句:

create user dumper@\'127.0.0.1\';
grant select on tempdb.* to dumper@\'127.0.0.1\';
grant show view on tempdb.* to dumper@\'127.0.0.1\';
grant lock tables on tempdb.* to dumper@\'127.0.0.1\';
grant trigger on tempdb.* to dumper@\'127.0.0.1\';

 

 

例子:

1、创建一个用于备份的用户dumper@\'127.0.0.1\'

create user dumper@\'127.0.0.1\' identified by \'123456\';

 

2、用dumper@’127.0.0.1\'去备份tempdb这个数据库、可以发生这个时候报没有select权限

mysqldump -h127.0.0.1 -udumper -p123456 --databases tempdb
Warning: Using a password on the command line interface can be insecure.
-- MySQL dump 10.13  Distrib 5.6.31, for linux-glibc2.5 (x86_64)
--
-- Host: 127.0.0.1    Database: tempdb
-- ------------------------------------------------------
-- Server version    5.6.31

/*!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 */;
mysqldump: Got error: 1044: Access denied for user \'dumper\'@\'127.0.0.1\' to database \'tempdb\' when selecting the database

 

3、为dumper 增加tempdb库上的select 权限

grant select on tempdb.* to dumper@\'127.0.0.1\';

 

4、用dumper@‘127.0.0.1’备份tempdb库

[root@workstudio ~]# mysqldump -h127.0.0.1 -udumper -p123456 --databases tempdb
Warning: Using a password on the command line interface can be insecure.
-- MySQL dump 10.13  Distrib 5.6.31, for linux-glibc2.5 (x86_64)
--
-- Host: 127.0.0.1    Database: tempdb
-- ------------------------------------------------------
-- Server version    5.6.31

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

--
-- Current Database: `tempdb`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `tempdb` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `tempdb`;
/*!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 */;

 

5、为tempdb 创建一个view 用来测试mysqldump备份view时要用到的权限

create view view_a as select 1 as number;

 

6、再次试着用dumper@‘127.0.0.1’去备份tempdb、日了狗了可以看到这个报错说是和lock tables 相关。

[root@workstudio ~]# mysqldump -h127.0.0.1 -udumper -p123456 --databases tempdb
Warning: Using a password on the command line interface can be insecure.
-- MySQL dump 10.13  Distrib 5.6.31, for linux-glibc2.5 (x86_64)
--
-- Host: 127.0.0.1    Database: tempdb
-- ------------------------------------------------------
-- Server version    5.6.31

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

--
-- Current Database: `tempdb`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `tempdb` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `tempdb`;
mysqldump: Got error: 1044: Access denied for user \'dumper\'@\'127.0.0.1\' to database \'tempdb\' when using LOCK TABLES

 

7、给dumper@\'127.0.0.1\'这个用户一个lock tables 权限

grant lock tables on tempdb.* to dumper@\'127.0.0.1\';

 

8、再次试着用dumper@‘127.0.0.1’去备份tempdb、这下可以看到了它报没有show view 权限了。

[root@workstudio ~]# mysqldump -h127.0.0.1 -udumper -p123456 --databases tempdb
Warning: Using a password on the command line interface can be insecure.
-- MySQL dump 10.13  Distrib 5.6.31, for linux-glibc2.5 (x86_64)
--
-- Host: 127.0.0.1    Database: tempdb
-- ------------------------------------------------------
-- Server version    5.6.31

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

--
-- Current Database: `tempdb`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `tempdb` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `tempdb`;
mysqldump: Couldn\'t execute \'show create table `view_a`\': SHOW VIEW command denied to user \'dumper\'@\'localhost\' for table \'view_a\' (1142)

 

9、为dumper@\'127.0.0.1\'这个用户加上show view 权限

grant show view on tempdb.* to dumper@\'127.0.0.1\';

 

10、再用dumper@’127.0.0.1‘用户备份tempdb库

[root@workstudio ~]# mysqldump -h127.0.0.1 -udumper -p123456 --databases tempdb
Warning: Using a password on the command line interface can be insecure.
-- MySQL dump 10.13  Distrib 5.6.31, for linux-glibc2.5 (x86_64)
--
-- Host: 127.0.0.1    Database: tempdb
-- ------------------------------------------------------
-- Server version    5.6.31

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

--
-- Current Database: `tempdb`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `tempdb` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `tempdb`;

--
-- Temporary view structure for view `view_a`
--

DROP TABLE IF EXISTS `view_a`;
/*!50001 DROP VIEW IF EXISTS `view_a`*/;
SET @saved_cs_client     = @@character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE VIEW `view_a` AS SELECT 
 1 AS `number`*/;
SET character_set_client = @saved_cs_client;

--
-- Current Database: `tempdb`
--

USE `tempdb`;

--
-- Final view structure for view `view_a`
--

/*!50001 DROP VIEW IF EXISTS `view_a`*/;
/*!50001 SET @saved_cs_client          = @@character_set_client */;
/*!50001 SET @saved_cs_results         = @@character_set_results */;
/*!50001 SET @saved_col_connection     = @@collation_connection */;
/*!50001 SET character_set_client      = utf8 */;
/*!50001 SET character_set_results     = utf8 */;
/*!50001 SET collation_connection      = utf8_general_ci */;
/*!50001 CREATE ALGORITHM=UNDEFINED */
/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */
/*!50001 VIEW `view_a` AS select 1 AS `number` */;
/*!50001 SET character_set_client      = @saved_cs_client */;
/*!50001 SET character_set_results     = @saved_cs_results */;
/*!50001 SET collation_connection      = @saved_col_connection */;
/*!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 2016-07-19  5:56:17

 

11、如果是用mysqldump 来做备份、那么备份用户的相关权限如下:

create user dumper@\'127.0.0.1\';
grant select on tempdb.* to dumper@\'127.0.0.1\';
grant show view on tempdb.* to dumper@\'127.0.0.1\';
grant lock tables on tempdb.* to dumper@\'127.0.0.1\';
grant trigger on tempdb.* to dumper@\'127.0.0.1\';