查看视图报错ERROR 1356 (HY000): View 'bdi_gj1121.y' references invalid table(s) or column(s) or function

问题说明:

业务说导入的新数据库有几个视图查不了,报错:

mysql> select count(*) from bdi_gj1121.beaf_company;
ERROR 1356 (HY000): View 'bdi_gj1121.beaf_company' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

业务背景:

由于业务需要测试用,让我从已有数据库bdi_gj复制另一个数据库bdi_gj1121,具体语句为:

create database bdi_gj1121;

mysqldump -uroot -pxxx bdi_gj |mysql -uroot -pxxx bdi_gj1121

复制数据库后,我对两个数据库进行了数据比对,看是否复制完整,经检查后,发现有几张表报上面的错误,查不了记录,经发现,这几张表是视图。于是我对两个库的视图个数进行了比对,使用语句为:

 SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '数据库名' AND  TABLE_TYPE ='VIEW';

发现两个库的视图个数是一样的,即数据库复制的操作是正确的。现在需要在视图方面查找原因。

总共有22个视图,其中有6个视图能正常使用,还有16个视图不能查看,报上面的错误。

于是把能正常使用的视图与不能使用的视图创建语句进行比对,发现了一个原因:

能正常使用的视图创建语句:

mysql> show create table bdi_gj1121.bds_prd_manager_monitor_view;
| bds_prd_manager_monitor_view | CREATE ALGORITHM=UNDEFINED DEFINER=`bde_cubigdata`@`%` SQL SECURITY DEFINER VIEW `bdi_gj1121`.`bds_prd_manager_monitor_view` AS select `t1`.`cycle_type` AS `cycle_type`,`t1`.`prd_all` AS `prd_all`,`t1`.`use_prd` AS `use_prd`,`t1`.`no_use_prd` AS `no_use_prd`,`t3`.`new_prd` AS `new_prd`,`t1`.`use_sum` AS `use_sum`,`t4`.`service_users_sum` AS `service_users_sum`,`t2`.`exe_task` AS `exe_task`,`b`.`MENU_NAME` AS `cycle_type_name` from ((((((select `tt`.`cycle_type` AS `cycle_type`,count(`tt`.`pid`) AS `prd_all`,sum((case when  | utf8                 | utf8_general_ci      

不能正常使用的视图创建语句:

mysql> show create table bdi_gj1121.beaf_company                       ;
| beaf_company | CREATE ALGORITHM=UNDEFINED DEFINER=`bdi_gj`@`%` SQL SECURITY DEFINER VIEW `bdi_gj1121`.`beaf_company` AS select `o`.`ORGANIZATION` AS `oid`,NULL AS `account_name`,NULL AS `company_name`,`z`.`NAME` AS `region_name`,0 AS `delete_flag`,1 AS `run_status`,sysdate() AS `create_date`,`o`.`ORGANIZATION` AS `organization`,NULL AS `organization_code`,1 AS `company_type`,`o`.`ZONE` AS `zone`,`z`.`CODE` AS `zone_code`,`z`.`TENANT_ID` AS `TENANT_ID` from (`bdi_gj1121`.`beaf_organization_ext` `o` join `bdi_gj1121`.`beaf_zone` `z`) where (`o`.`ZONE` = `z`.`OID`) | utf8                 | utf8_general_ci |

发现:新复制的这个库的视图,它前面定义的用户也不相同。而新复制的库整体是个新环境,上面的库也都是从其他主机上的mysql迁移过来的。以前的mysql库上是有bdi_gj用户的,但是现在这个新库没有bdi_gj用户,但是有bde_cubigdata用户,所以前面定义为bde_cubigdata的视图是能正常访问的,而bdi_gj定义的视图是查看不了的。

解决办法:重新建一个bdi_gj用户,或者将视图的定义用户修改为现有用户。

我的操作是重新建一个bdi_gj用户,并给用户赋权:

mysql> create user bdi_gj identified by 'xxx';

mysql> grant all privileges on *.* to bdi_gj@'%';

mysql> flush privileges;

现在所有的视图都可以正常访问啦!!!

 

你可能感兴趣的:(MySQL)