Mysql創(chuàng)建、刪除用戶、查詢所有用戶等教程,提升您的MYSQL安全度
建議您不要使用mysql的root賬號來為您的web頁面連接數(shù)據(jù)庫,這可能會導致用戶通過網(wǎng)頁獲取到您的數(shù)據(jù)庫賬號密碼,存在嚴重的安全隱患。
建議新建一賬號,權限設置基本夠用,然后使用那新建的賬號來連接您的數(shù)據(jù)庫。
1、連接mysql
[html]
mysql -uroot -p密碼
(-p后面無需空格,從-p開始即為密碼開始)
如果您的MySql在遠程服務器上,需要加上 -hIP地址,如
mysql -uroot -p123456 -h111.111.111.111
2、查看MYSQL數(shù)據(jù)庫中所有用戶,仔細檢查是否有未經(jīng)您授權的用戶存在
[html]
SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
3、創(chuàng)建新用戶(針對某個數(shù)據(jù)庫的用戶)
格式:grant 權限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機 identified by "密碼"
如,增加一個用戶user1密碼為password1,讓其可以在本機上登錄, 并對所有數(shù)據(jù)庫有查詢、插入、修改、刪除的權限。首先用以root用戶連入mysql,然后鍵入以下命令:
[html]
grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";
注意:此處的"localhost",是指該用戶只能在本地登錄,不能在另外一臺機器上遠程登錄。如果想遠程登錄的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登錄。也可以指定某臺機器可以遠程登錄。
如果你不想user1有密碼,可以再打一個命令將密碼去掉。
[html]
grant select,insert,update,delete on mydb.* to user1@localhost identified by "";
如果說用戶原來存在,您只需
[html]
grant select,insert,update,delete on mydb.* to user1@localhost
權限列表
grant 權限1,權限2,...權限n on 數(shù)據(jù)庫名稱.表名稱 to 用戶名@用戶地址 identified by '連接口令';
權限1,權限2,...權限n代表select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個權限。
當權限1,權限2,...權限n被all privileges或者all代替,表示賦予用戶全部權限。
當數(shù)據(jù)庫名稱.表名稱被*.*代替,表示賦予用戶操作服務器上所有數(shù)據(jù)庫所有表d權限。
[html]
mysql>grant all privileges on testDB.* to test@localhost identified by '1234';
mysql>flush privileges;//刷新系統(tǒng)權限表
格式:grant 權限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機 identified by "密碼";
4、刪除用戶
@>mysql -u root -p
@>密碼
mysql>Delete FROM user Where User='test' and Host='localhost';
mysql>flush privileges;
mysql>drop database testDB; //刪除用戶的數(shù)據(jù)庫
刪除賬戶及權限:>drop user 用戶名@'%';
>drop user 用戶名@ localhost;
5、修改指定用戶密碼
@>mysql -u root -p
@>密碼
mysql>update mysql.user set password=password('新密碼') where User="test" and Host="localhost";
mysql>flush privileges;
如刪除sa這個用戶,我前面設置host為localhost:
[html]
drop user sa@localhost;
bitsCN.com聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com