国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

Mysql查詢視圖:ERROR 1449 (HY000)解決辦法

2024-07-24 12:38:00
字體:
供稿:網(wǎng)友

問題重現(xiàn):前幾天因為有人刪除了數(shù)據(jù)庫中的記錄,今天關閉了數(shù)據(jù)庫的遠程訪問功能,今天接到開發(fā)報告,說出現(xiàn) The user specified as a definer (‘air’@'%’) does not exist錯誤,他們定位是一張視圖不能訪問,利用實驗重現(xiàn)了他們的情況.

原因分析:因為創(chuàng)建視圖使用的是xff@%用戶(目前已經(jīng)不存在),然后登錄用戶使用的是xff@localhost用戶,導致mysql認為現(xiàn)在的用戶無權限訪問該視圖,解決方法就是在當前用戶下重建該視圖.

我使用的代碼,代碼如下:

  1. [root@ECP-UC-DB1 ~]# mysql -uxff -pxifenfei  
  2. Welcome to the MySQL monitor.  Commands end with ; or g.  
  3. Your MySQL connection id is 8846  
  4. Server version: 5.5.14-log MySQL Community Server (GPL)  
  5.    
  6. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  7.    
  8. Oracle is a registered trademark of Oracle Corporation and/or its  
  9. affiliates. Other names may be trademarks of their respective  
  10. owners.  
  11.    
  12. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.  
  13.    
  14. mysql> select user,host from mysql.user;  
  15. +------+---------------+  
  16. user | host          |  
  17. +------+---------------+  
  18. | xff  | %             |  
  19. | root | 127.0.0.1     |  
  20. | repl | 192.168.11.10 |  
  21. | root | ::1           |  
  22. |      | ECP-UC-DB1    |  
  23. | root | ECP-UC-DB1    |  
  24. | root | localhost     |  
  25. +------+---------------+  
  26. rows in set (0.08 sec)  
  27.    
  28. mysql> use xifenfei;  
  29. Reading table information for completion of table and column names  
  30. You can turn off this feature to get a quicker startup with -A  
  31.    
  32. Database changed  
  33.    
  34. mysql> create view v_users as select * from wp_users;  
  35. Query OK, 0 rows affected (0.14 sec)  
  36.    
  37. mysql> select count(*) from xifenfei.v_users;  
  38. +----------+  
  39. count(*) |  
  40. +----------+  
  41. |        2 |  
  42. +----------+  
  43. 1 row in set (0.03 sec)  
  44.    
  45. mysql> update mysql.user set host='localhost' where user='xff' and host='%';  
  46. Query OK, 1 row affected (0.05 sec)  
  47. Rows matched: 1  Changed: 1  Warnings: 0  
  48.    
  49. mysql> FLUSH PRIVILEGES;                     
  50. Query OK, 0 rows affected (0.12 sec)  
  51.    
  52. mysql> exit  
  53. Bye  
  54. [root@ECP-UC-DB1 ~]# mysql -uxff -pxifenfei  
  55. Welcome to the MySQL monitor.  Commands end with ; or g.  
  56. Your MySQL connection id is 8847  
  57. Server version: 5.5.14-log MySQL Community Server (GPL)  
  58.    
  59. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  60.    
  61. Oracle is a registered trademark of Oracle Corporation and/or its  
  62. affiliates. Other names may be trademarks of their respective  
  63. owners.  
  64.    
  65. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.  
  66.    
  67. mysql> use xff;  
  68. ERROR 1049 (42000): Unknown database 'xff' 
  69. mysql> use xifenfei;  
  70. Reading table information for completion of table and column names  
  71. You can turn off this feature to get a quicker startup with -A  
  72. --Vevb.com 
  73. Database changed  
  74. mysql> select * from v_users ;  
  75. ERROR 1449 (HY000): The user specified as a definer ('xff'@'%') does not exist 

2、解決方法,代碼如下:

  1. mysql> show databases;  
  2. +--------------------+  
  3. Database           |  
  4. +--------------------+  
  5. | information_schema |  
  6. | mysql              |  
  7. | performance_schema |  
  8. | test               |  
  9. | xifenfei           |  
  10. +--------------------+  
  11. rows in set (0.00 sec)  
  12.    
  13. mysql> use information_schema;  
  14. Reading table information for completion of table and column names  
  15. You can turn off this feature to get a quicker startup with -A  
  16.    
  17. Database changed  
  18.    
  19. mysql> desc VIEWS;  
  20. +----------------------+--------------+------+-----+---------+-------+  
  21. | Field                | Type         | Null | Key | Default | Extra |  
  22. +----------------------+--------------+------+-----+---------+-------+  
  23. | TABLE_CATALOG        | varchar(512) | NO   |     |         |       |  
  24. | TABLE_SCHEMA         | varchar(64)  | NO   |     |         |       |  
  25. | TABLE_NAME           | varchar(64)  | NO   |     |         |       |  
  26. | VIEW_DEFINITION      | longtext     | NO   |     | NULL    |       |  
  27. | CHECK_OPTION         | varchar(8)   | NO   |     |         |       |  
  28. | IS_UPDATABLE         | varchar(3)   | NO   |     |         |       |  
  29. | DEFINER              | varchar(77)  | NO   |     |         |       |  
  30. | SECURITY_TYPE        | varchar(7)   | NO   |     |         |       |  
  31. | CHARACTER_SET_CLIENT | varchar(32)  | NO   |     |         |       |  
  32. | COLLATION_CONNECTION | varchar(32)  | NO   |     |         |       |  
  33. +----------------------+--------------+------+-----+---------+-------+  
  34. 10 rows in set (0.02 sec)  
  35.    
  36. mysql> select TABLE_SCHEMA,TABLE_NAME,DEFINER from views;  
  37. +--------------+------------+---------+  
  38. | TABLE_SCHEMA | TABLE_NAME | DEFINER |  
  39. +--------------+------------+---------+  
  40. | xifenfei     | v_users    | xff@%   |  
  41. +--------------+------------+---------+  
  42. 1 row in set (0.16 sec)  
  43.    
  44. mysql> create or replace view v_users as select * from wp_users;  
  45. ERROR 1044 (42000): Access denied for user 'xff'@'localhost' to database 'information_schema' 
  46. mysql> create or replace view xifenfei.v_users as select * from xifenfei.wp_users;  
  47. Query OK, 0 rows affected (0.02 sec)  
  48.    
  49. mysql> select TABLE_SCHEMA,TABLE_NAME,DEFINER from views;  
  50. +--------------+------------+---------------+  
  51. | TABLE_SCHEMA | TABLE_NAME | DEFINER       |  
  52. +--------------+------------+---------------+  
  53. | xifenfei     | v_users    | xff@localhost |  
  54. +--------------+------------+---------------+  
  55. 1 row in set (0.01 sec)  
  56.    
  57. mysql> select count(*) from xifenfei.v_users;  
  58. +----------+  
  59. count(*) |  
  60. +----------+  
  61. |        2 |  
  62. +----------+  
  63. 1 row in set (0.03 sec) 

1.注意事項

創(chuàng)建視圖存在如下注意事項:

(1) 運行創(chuàng)建視圖的語句需要用戶具有創(chuàng)建視圖(CRATE VIEW)的權限,若加了[OR REPLACE]時,還需要用戶具有刪除視圖(DROP VIEW)的權限;

(2) SELECT語句不能包含F(xiàn)ROM子句中的子查詢;

(3) SELECT語句不能引用系統(tǒng)或用戶變量;

(4) SELECT語句不能引用預處理語句參數(shù);

(5) 在存儲子程序內(nèi),定義不能引用子程序參數(shù)或局部變量;

(6) 在定義中引用的表或視圖必須存在。但是,創(chuàng)建了視圖后,能夠舍棄定義引用的表或視圖。要想檢查視圖定義是否存在這類問題,可使用CHECK TABLE語句;

(7) 在定義中不能引用TEMPORARY表,不能創(chuàng)建TEMPORARY視圖;

(8) 在視圖定義中命名的表必須已存在;

(9) 不能將觸發(fā)程序與視圖關聯(lián)在一起;

(10) 在視圖定義中允許使用ORDER BY,但是,如果從特定視圖進行了選擇,而該視圖使用了具有自己ORDER BY的語句,它將被忽略。

補充一下mysql視圖基本知識

創(chuàng)建視圖——CREATE VIEW

1.語法,代碼如下:

CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW [db_name.]view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION]

通過該語句可以創(chuàng)建視圖,若給定了[OR REPLACE],則表示當已具有同名的視圖時,將覆蓋原視圖。select_statement是一個查詢語句,這個查詢語句可從表或其它的視圖中查詢。視圖屬于數(shù)據(jù)庫,因此需要指定數(shù)據(jù)庫的名稱,若未指定時,表示在當前的數(shù)據(jù)庫創(chuàng)建新視圖。

表和數(shù)據(jù)庫共享數(shù)據(jù)庫中相同的名稱空間,因此,數(shù)據(jù)庫不能包含相同名稱的表和視圖,并且,視圖的列名也不能重復.

1.使用舉例

Eg.本例創(chuàng)建一個產(chǎn)品表(product)和一個購買記錄表(purchase),再通過視圖purchase_detail查詢出購買的詳細信息,代碼如下:

  1. CREATE TABLE product 
  2. product_id INT NOT NULL
  3. name VARCHAR(50) NOT NULL
  4. price DOUBLE NOT NULL 
  5. ); 
  6. INSERT INTO product VALUES(1, 'apple ', 5.5); 
  7. CREATE TABLE purchase 
  8. id INT NOT NULL
  9. product_id INT NOT NULL
  10. qty INT NOT NULL DEFAULT 0, 
  11. gen_time DATETIME NOT NULL 
  12. ); 
  13. INSERT INTO purchase VALUES(1, 1, 10, NOW()); 
  14. CREATE VIEW purchase_detail AS SELECT product.name as name, product .price as price, purchase.qty as qty, product .price * purchase.qty as total_value from product, purchase where product.product_id = purchase.product_id; 

創(chuàng)建成功后,輸入,SELECT * FROM purchase_detail;

運行效果如下:

  1. +-------+-------+-----+-------------+ 
  2. name | price | qty | total_value | 
  3. +-------+-------+-----+-------------+ 
  4. | apple | 5.5 | 10 | 55 | 
  5. +-------+-------+-----+-------------+ 
  6. 1 row in set (0.01 sec)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 屯留县| 邻水| 鄂尔多斯市| 全南县| 老河口市| 日照市| 东乡| 玉山县| 大田县| 泸州市| 商洛市| 常宁市| 嘉善县| 湟源县| 永福县| 大渡口区| 克东县| 迭部县| 分宜县| 扎鲁特旗| 赤城县| 金平| 石城县| 汉寿县| 馆陶县| 邳州市| 淮北市| 巴马| 吴川市| 阿尔山市| 出国| 饶平县| 车险| 丰镇市| 田阳县| 宜君县| 金川县| 丹巴县| 德清县| 班戈县| 章丘市|