1、新建用戶
創建test用戶,密碼是1234。
mysql -u root -p
#輸入密碼
#本地登錄
mysql>CREATE USER 'test'@'localhost' IDENTIFIED BY '1234';
遠程登錄
mysql>CREATE USER 'test'@'%' IDENTIFIED BY '1234'; mysql>quit ;
#測試是否創建成功
mysql -u test -p
2、為用戶授權
a.授權格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by '密碼';
MySQL grant 權限,分別可以作用在多個層次上。
#grant 作用在整個 MySQL 服務器上: mysql>grant select on *.* to root@localhost; # 可以查詢 MySQL 中所有數據庫中的表。 mysql>grant all on *.* to root@localhost; # 可以管理 MySQL 中的所有數據庫 #grant 作用在單個數據庫上: mysql>grant select on testdb.* to root@localhost; # 可以查詢 testdb 中的表。 #grant 作用在單個數據表上: mysql>grant select, insert, update, delete on testdb.orders to test@localhost;
查看權限
#查看當前用戶(自己)權限: mysql>show grants; #查看其他 MySQL 用戶權限: mysql>show grants for 'test'@localhost;
撤銷已經賦予給 MySQL 用戶權限的權限.
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
mysql>grant all on *.* to test@localhost; mysql>revoke all on *.* from test@localhost;
b.登錄MYSQL,這里以ROOT身份登錄:
mysql -u root -p
為用戶創建一個數據庫(testDB):
mysql>create database testDB;
mysql>create database testDB default charset utf8 collate utf8_general_ci;
授權test用戶擁有testDB數據庫的所有權限:
mysql>grant all privileges on testDB.* to 'test'@localhost identified by "123456"; mysql>flush privileges; #刷新系統權限表
指定部分權限給用戶:
mysql>grant select,update on testDB.* to 'test'@localhost identified by "123456"; mysql>flush privileges; #刷新系統權限表
授權test用戶擁有所有數據庫的某些權限:
mysql>grant select,delete,update,create,drop on . to test@"%" identified by by "123456";
”%” 表示對所有非本地主機授權,不包括localhost
刪除用戶、數據庫
mysql>mysql -u root -p Delete FROM mysql.user Where User="test" and Host="localhost"; mysql>flush privileges; mysql>drop database testDB;
刪除賬戶及權限:
mysql>drop user test@'%'; mysql>drop user test@localhost;
修改指定用戶密碼
mysql>mysql -u root -p update mysql.user set authentication_string=password("123456") where User="guest" and Host="localhost";
mysql>flush privileges;