1. 程式人生 > >MySQL資料庫的高階操作

MySQL資料庫的高階操作

1.資料備份與還原

(1)備份
mysqldump:
mysqldump -u username -p password dbname [tbname1 [tbname2....] ]> filename.sql

             mysqldump -u root -p mydb2 > C:\mysql\dump\mydb2_dump.sql

(2)恢復:只能恢復資料庫的表和記錄,不能恢復資料庫本身

    mysql:方法一
              mysql -u username -p password [dbname] < filename.sql

mysql:方法二,source命令
在MySQL命令提示符下:建立資料庫 進入資料庫 source xxx.sql 檔案 將備份的sql檔案在當前位置執行
source filename.sql //路徑

2.user表

3.建立普通使用者

(1)使用GRANT語句建立使用者

GRANT privileges ON dtabase.table
TO 'username' @ 'hostname' [IDENTIFIED BY [PASSWORD] 'password'] [,'username' @ 'hostname' [IDENTIFIED BY [PASSWORD] 'password']]...............


grant select on mydb2.* to 'haha' @'localhost' identified by '123456';

//錯誤

(2)使用CREATE語句

CREATE USER 'username'@'hostname' [IDENTIFIED BY [PASSWORD] 'password'] [,'username' @ 'hostname' [IDENTIFIED BY [PASSWORD] 'password']]...............

create user 'haha'@'localhost' identified by '123456';

(3) 使用INSERT語句

4.刪除普通使用者

~ROP USER 'username'@'hostname' [,'username'@'hostname'];

drop user 'ha'@'localhost';

~ELETE FROM mysql.user WHERE Host= 'hostname' AND User = 'username';

delete from mysql.user where host = 'localhost' and user = 'ha';

flush privileges; #由於直接對user表執行操作,要重新載入使用者許可權

5.修改使用者密碼

(1)修改root使用者密碼

+++++++++++++++++++++++++++++++++++++++++++++++

UPDATE mysql.user set Password = PASSWORD('new_password') WHERE User='username' and Host='hostname';

FLUSH PRIVILEGES;

update mysql.user set password=PASSWORD('[email protected]#') where user='root' and host='localhost';

flush privileges;
+++++++++++++++++++++++++++++++++++++++++++++
//不成功

(2)root使用者修改普通使用者密碼
SET PASSWORD FOR 'username'@'hostname'=PASSWORD('new_password');

set password for 'haha'@'localhost'=PASSWORD('123');
+++++++++++++++++++++++++++++++++++++++
//不成功

(3)普通使用者修改密碼

SET PASSWORD=PASSWORD('new_password');

6.授予許可權:使不同使用者有不同許可權

(1)GRANT privileges [ (columns) ] [,privileges[(columns)]] ON database.table TO 'username'@'hostname' [IDENTIFIED BY [PASSWORD] 'password' ] [ 'username'@'hostname' [IDENTIFIED BY [PASSWORD] 'password' ]]...........
[WITH with_option [with_option]...]

with_option引數如下:
(1)GRANT OPTION:將自己的許可權授予其他使用者
(2)MAX_QUERIES_PER_HOUR count:設定每小時最大查詢次數count。
(3)MAX_UPDATES_PER_HOUR count:設定每小時最多可執行多少次更新
(4)MAX_CONNECTIONS_PER_HOUR count:設定每小時最大連線數量
(5)MAX_USER_CONNECTIONS:設定每個使用者最多可以同時建立連線數量

GRANT INSERT,SELECT ON mydb2.star TO 'haha'@'localhost' IDENTIFIED BY '123456'
WITH GRANT OPTION;

++++++++++++++++++++++++++++++++++++++++++++
//不成功

7.檢視許可權

(1) SHOW GRANTS FOR 'username'@'hostname';
show grants for 'haha'@'localhost';

8.收回許可權

REVOKE privileges [ (columns) ] [,privileges[(columns)]] ON database.table FROM 'username'@'hostname' [,'username'@'hostname' ]....