-- 查詢數據庫用戶 select user -- 使用ip鏈接數據庫(%匹配所有) mysql -h192.168.1.% -ulisi -pxxxx; -- 修改host域,使IP可以鏈接上 update user set host='192.168.1.%' where user='root'; flush privileges; -- 修改用戶密碼,password()對密碼加密 update user set password=password('11111') where xxxx; flush privileges; -- 查看用戶以及權限 select * from user where user='lisi' \G; -- 查看庫表的級別和host鏈接狀態 select * from db \G; select * from table \G; -- 新增用戶並賦予全局權限 -- 常用的權限all,create,drop,insert,delete,update,select -- 比如grant all on *.* to lisi@'192.168.1.%' identfied by'1111'; grant [權限1,權限2,權限3.。] on *.* to user@'host' identfied by'password'; -- 回收全局權限 -- 比如revoke all on *.* from lisi@'192.168.1.%'; revoke all on *.* from user@'host' identfied by'password'; -- 針對庫做授權 -- 比如grant all on test.* to lisi@'192.168.1.%'; grant all on tbName.* to lisi@'192.168.1.%'; revoke all on tbName.* to lisi@'192.168.1.%'; -- 針對表做授權 -- 比如grant insert,update,select on test.goods to lisi@'192.168.1.%'; grant insert,update,select on dbName.tbName to lisi@'192.168.1.%'; revoke all on dbName.tbName to lisi@'192.168.1.%'; -- MYSQL添加遠程用戶或允許遠程訪問三種方法 -- 添加遠程用戶admin密碼為password GRANT ALL PRIVILEGES ON *.* TO admin@localhost IDENTIFIED BY password WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO admin@\"%\" IDENTIFIED BY password WITH GRANT OPTION; -- 用root用戶登陸,然后: grant all privileges on *.* to userName @"%" identified by "密碼"; flush privileges; * 刷新剛才的內容* -- 格式:grant 權限 on 數據庫教程名.表名 to 用戶@登錄主機 identified by "用戶密碼";@ 后面是訪問mysql的客戶端ip地址(或是 主機名) -- % 代表任意的客戶端,如果填寫 localhost 為本地訪問(那么 用戶就不能遠程訪問該mysql數據庫了,在服務器上localhost是指服務器主機)。 -- 同時也可以為現有的用戶設置是否具有遠程訪問權限。如下: use mysql; update db set host = '%' where user = 'userName'; (如果寫成 host=localhost 那此用戶就不具有遠程訪問權限) flush privileges; grant all privileges on *.* to 'userName'@'%' identified by 'password' with grant option; -- 使用grant語句添加: -- 首先在數據庫本機上用root用戶登錄mysql(我是用遠程控制linux服務器,相當於在服務器本機登錄mysql了),然后輸入: grant all privileges on *.* to admin@localhost identified by 'password' with grant option; -- 添加一個用戶admin並授權可從任何其它主機(localhost)發起的訪問(通配符%)。使用這一條語句即可。 grant all privileges on *.* to admin@"%" identified by 'password' with grant option; -- 使用insert語句: -- 用戶信息可在mysql數據庫中的users表中查看,這里不在介紹了就。數清y的個數哦。 -- 好了,使用admin帳號連接試試看,我是屢試屢成功哦,呵呵! insert into user values('%','admin',password('password'), 'y','y','y','y','y','y','y','y','y','y','y','y','y','y'); -- 添加遠程用戶admin密碼為password grant all privileges on *.* to admin@localhost identified by 'password' with grant option; grant all privileges on *.* to admin@"%" identified by 'password' with grant option;
由於項目開發的要求數據庫的設計不得不用遠程模式。但是數據庫的遠程設置並沒那么簡單,該項目的數據庫是mysql5.0。剛開始以為只要裝了數據庫服務器就可以進行遠程鏈接了,但是mysql的設置是為了用戶的安全,系統默認的設置是不允許遠程用戶連接,只能本地的用戶連接。只要我們設置下系統的管理員用戶的host這一項的值就可以給遠程的用戶訪問了。數據庫權限管理極其重要,特別是主從數據庫配置以及讀寫分離涉及到賦予表級權限;