溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL添加用戶、刪除用戶與授權(quán)

發(fā)布時間:2020-07-19 06:13:23 來源:網(wǎng)絡(luò) 閱讀:324 作者:終而有始 欄目:系統(tǒng)運(yùn)維

MySQL添加用戶、刪除用戶與授權(quán)
MySql中添加用戶,新建數(shù)據(jù)庫,用戶授權(quán),刪除用戶,修改密碼(注意每行后邊都跟個;表示一個命令語句結(jié)束):

1.新建用戶

1.1 登錄MYSQL:

@>mysql -u root -p

@>密碼

1.2 創(chuàng)建用戶:

mysql> insert into mysql.user(Host,User,Password) values(" localhost","test",password("1234"));

這樣就創(chuàng)建了一個名為:test 密碼為:1234 的用戶。

注意:此處的"localhost",是指該用戶只能在本地登錄,不能在另外一臺機(jī)器上遠(yuǎn)程登錄。如果想遠(yuǎn)程登錄的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登錄。也可以指定某臺機(jī)器可以遠(yuǎn)程登錄。

1.3 然后登錄一下:

mysql>exit;

@>mysql -u test -p

@>輸入密碼

mysql>登錄成功

2.為用戶授權(quán)

授權(quán)格式:grant 權(quán)限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機(jī) identified by "密碼";

2.1 登錄MYSQL(有ROOT權(quán)限),這里以ROOT身份登錄:

@>mysql -u root -p

@>密碼

2.2 首先為用戶創(chuàng)建一個數(shù)據(jù)庫(testDB):

mysql>create database testDB;

2.3 授權(quán)test用戶擁有 testDB數(shù)據(jù)庫的所有權(quán)限(某個數(shù)據(jù)庫的所有權(quán)限):

mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

mysql>flush privileges;//刷新系統(tǒng)權(quán)限表

格式:grant 權(quán)限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機(jī) identified by "密碼";

2.4 如果想指定 部分權(quán)限給一用戶,可以這樣來寫:

mysql>grant select,update on testDB.* to test@localhost identified by '1234';

mysql>flush privileges; //刷新系統(tǒng)權(quán)限表

2.5 授權(quán)test用戶擁有 所有數(shù)據(jù)庫的某些權(quán)限:

mysql>grant select,delete,update,create,drop on . to test @"%" identified by "1234";

 //test用戶對所有數(shù)據(jù)庫都有select,delete,update,create,drop 權(quán)限。

// @"%" 表示對所有非本地主機(jī)授權(quán),不包括localhost。(localhost地址設(shè)為127.0.0.1,如果設(shè)為真實(shí)的本地地址,不知道是否可以,沒有驗證。)

//對localhost授權(quán):加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可。

  1. 刪除用戶

    @>mysql -u root -p

@>密碼

mysql>Delete FROM user Where User='test' and Host='localhost';

mysql>flush privileges;

mysql>drop database testDB; //刪除用戶的數(shù)據(jù)庫

刪除賬戶及權(quán)限:>drop user 用戶名@'%';

drop user 用戶名@ localhost;

  1. 修改指定用戶密碼

    @>mysql -u root -p

    @>密碼

    mysql>update mysql.user set password=password('新密碼') where User="test" and Host="localhost";

    mysql>flush privileges;

  2. 列出所有數(shù)據(jù)庫

mysql>show database;

  1. 切換數(shù)據(jù)庫

mysql>use '數(shù)據(jù)庫名';

  1. 列出所有表

mysql>show tables;

  1. 顯示數(shù)據(jù)表結(jié)構(gòu)

mysql>describe 表名;

  1. 刪除數(shù)據(jù)庫和數(shù)據(jù)表

mysql>drop database 數(shù)據(jù)庫名;

mysql>drop table 數(shù)據(jù)表名;

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI