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

來源:互聯網
上載者:User

問題重現

前幾天因為有人刪除了資料庫中的記錄,今天關閉了資料庫的遠端存取功能,今天接到開發報告,說出現 The user specified as a definer (‘air’@'%’) does not exist錯誤,他們定位是一張視圖不能訪問。利用實驗重現了他們的情況


原因分析

因為建立視圖使用的是xff@%使用者(目前已經不存在),然後登入使用者使用的是xff@localhost使用者,導致mysql認為現在的使用者無許可權訪問該視圖,解決方案就是在目前使用者下重建該視圖

我使用的代碼

 代碼如下 複製代碼


[root@ECP-UC-DB1 ~]# mysql -uxff -pxifenfei
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8846
Server version: 5.5.14-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 
mysql> select user,host from mysql.user;
+------+---------------+
| user | host          |
+------+---------------+
| xff  | %             |
| root | 127.0.0.1     |
| repl | 192.168.11.10 |
| root | ::1           |
|      | ECP-UC-DB1    |
| root | ECP-UC-DB1    |
| root | localhost     |
+------+---------------+
7 rows in set (0.08 sec)
 
mysql> use xifenfei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
 
mysql> create view v_users as select * from wp_users;
Query OK, 0 rows affected (0.14 sec)
 
mysql> select count(*) from xifenfei.v_users;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.03 sec)
 
mysql> update mysql.user set host='localhost' where user='xff' and host='%';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> FLUSH PRIVILEGES;                   
Query OK, 0 rows affected (0.12 sec)
 
mysql> exit
Bye
[root@ECP-UC-DB1 ~]# mysql -uxff -pxifenfei
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8847
Server version: 5.5.14-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 
mysql> use xff;
ERROR 1049 (42000): Unknown database 'xff'
mysql> use xifenfei;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select * from v_users ;
ERROR 1449 (HY000): The user specified as a definer ('xff'@'%') does not exist


2、解決方案

 代碼如下 複製代碼

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| xifenfei           |
+--------------------+
5 rows in set (0.00 sec)
 
mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
 
mysql> desc VIEWS;
+----------------------+--------------+------+-----+---------+-------+
| Field                | Type         | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+-------+
| TABLE_CATALOG        | varchar(512) | NO   |     |         |       |
| TABLE_SCHEMA         | varchar(64)  | NO   |     |         |       |
| TABLE_NAME           | varchar(64)  | NO   |     |         |       |
| VIEW_DEFINITION      | longtext     | NO   |     | NULL    |       |
| CHECK_OPTION         | varchar(8)   | NO   |     |         |       |
| IS_UPDATABLE         | varchar(3)   | NO   |     |         |       |
| DEFINER              | varchar(77)  | NO   |     |         |       |
| SECURITY_TYPE        | varchar(7)   | NO   |     |         |       |
| CHARACTER_SET_CLIENT | varchar(32)  | NO   |     |         |       |
| COLLATION_CONNECTION | varchar(32)  | NO   |     |         |       |
+----------------------+--------------+------+-----+---------+-------+
10 rows in set (0.02 sec)
 
mysql> select TABLE_SCHEMA,TABLE_NAME,DEFINER from views;
+--------------+------------+---------+
| TABLE_SCHEMA | TABLE_NAME | DEFINER |
+--------------+------------+---------+
| xifenfei     | v_users    | xff@%   |
+--------------+------------+---------+
1 row in set (0.16 sec)
 
mysql> create or replace view v_users as select * from wp_users;
ERROR 1044 (42000): Access denied for user 'xff'@'localhost' to database 'information_schema'
mysql> create or replace view xifenfei.v_users as select * from xifenfei.wp_users;
Query OK, 0 rows affected (0.02 sec)
 
mysql> select TABLE_SCHEMA,TABLE_NAME,DEFINER from views;
+--------------+------------+---------------+
| TABLE_SCHEMA | TABLE_NAME | DEFINER       |
+--------------+------------+---------------+
| xifenfei     | v_users    | xff@localhost |
+--------------+------------+---------------+
1 row in set (0.01 sec)
 
mysql> select count(*) from xifenfei.v_users;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.03 sec)


1.注意事項

建立視圖存在如下注意事項:
(1) 運行建立視圖的語句需要使用者具有建立視圖(CRATE VIEW)的許可權,若加了[OR REPLACE]時,還需要使用者具有刪除視圖(DROP VIEW)的許可權;
(2) SELECT語句不能包含FROM子句中的子查詢;
(3) SELECT語句不能引用系統或使用者變數;
(4) SELECT語句不能引用預先處理語句參數;
(5) 在儲存子程式內,定義不能引用子程式參數或局部變數;
(6) 在定義中引用的表或視圖必須存在。但是,建立了視圖後,能夠捨棄定義引用的表或視圖。要想檢查視圖定義是否存在這類問題,可使用CHECK TABLE語句;
(7) 在定義中不能引用TEMPORARY表,不能建立TEMPORARY視圖;
(8) 在視圖定義中命名的表必須已存在;
(9) 不能將觸發程式與視圖關聯在一起;
(10) 在視圖定義中允許使用ORDER BY,但是,如果從特定視圖進行了選擇,而該視圖使用了具有自己ORDER BY的語句,它將被忽略。


補充一下mysql視圖基本知識

建立視圖——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]

通過該語句可以建立視圖,若給定了[OR REPLACE],則表示當已具有同名的視圖時,將覆蓋原視圖。select_statement是一個查詢語句,這個查詢語句可從表或其它的視圖中查詢。視圖屬於資料庫,因此需要指定資料庫的名稱,若未指定時,表示在當前的資料庫建立新視圖。
表和資料庫共用資料庫中相同的名稱空間,因此,資料庫不能包含相同名稱的表和視圖,並且,視圖的列名也不能重複。
1.使用舉例
Eg. 本例建立一個產品表(product)和一個購買記錄表(purchase),再通過視圖purchase_detail查詢出購買的詳細資料。

 代碼如下 複製代碼
CREATE TABLE product
(
product_id INT NOT NULL,
name VARCHAR(50) NOT NULL,
price DOUBLE NOT NULL
);
INSERT INTO product VALUES(1, 'apple ', 5.5);
CREATE TABLE purchase
(
id INT NOT NULL,
product_id INT NOT NULL,
qty INT NOT NULL DEFAULT 0,
gen_time DATETIME NOT NULL
);
INSERT INTO purchase VALUES(1, 1, 10, NOW());
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;

建立成功後,輸入:SELECT * FROM purchase_detail;
運行效果如下:

 代碼如下 複製代碼

+-------+-------+-----+-------------+
| name | price | qty | total_value |
+-------+-------+-----+-------------+
| apple | 5.5 | 10 | 55 |
+-------+-------+-----+-------------+
1 row in set (0.01 sec)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.