mysql如何建立程式使用者 MySql建立使用者

2021-10-18 22:11:22 字數 2331 閱讀 4692

mysql建立使用者的方法分成三種:insert user表的方法、create user的方法、grant的方法。

一、賬號名稱的構成方式

賬號的組成方式:使用者名稱+主機(所以可以出現重複的使用者名稱,跟其他的資料庫不一樣)

使用者名稱:16字元以內.

二、通過create user命令進行建立使用者

指令碼:create user 'username@host' [identified by 'password'] 其中密碼是可選項;

例子:create user '[email protected]' identified by "123";

create user '[email protected].%' identified by "123";

create user 'john@' ;

說明:該方法建立出來的使用者只有連線資料庫的許可權,需要後續繼續授權;

注意:使用者與@後主機位址是一體的,用乙個分號連線,否則會報錯,error 1396 (hy000): operation create user failed for 'remote'@'%'

三、通過grant命令建立使用者

當資料庫存在使用者的時候grant會對使用者進行授權,但當資料庫不存在該使用者的時候,就會建立相應的使用者並進行授權。(說明上面那步是多餘的)

指令碼:grant on

[object] [identified by 'password']

[with grant option];

max_queries_per_hour count

max_updates_per_hour count

max_connections_per_hour count

max_user_connections count

說明:priv代表許可權select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個許可權

例子:mysql>grant select,insert,update,delete,create,drop on test.hr to [email protected] identified by '123';

說明:給主機為192.168.10.1的使用者john分配可對資料庫test的hr表進行select,insert,update,delete,create,drop等操作的許可權,並設定口令為123。

mysql>grant all privileges on test.* to [email protected] identified by '123';

說明:給主機為192.168.10.1的使用者john分配可對資料庫test所有表進行所有操作的許可權,並設定口令為123。

mysql>grant all privileges on . to [email protected] identified by '123';

說明:給主機為192.168.10.1的使用者john分配可對所有資料庫的所有表進行所有操作的許可權,並設定口令為123。

mysql>grant all privileges on . to john@localhost identified by '123';

說明:使用者john分配可對所有資料庫的所有表進行所有操作的許可權,並設定口令為123。

mysql>grant insert,delete,update,select on . to 'saas'@'%' identified by '123';

說明:使用者john分配可對所有資料庫的所有表進行所有操作的許可權,並設定口令為123。

檢視許可權:

show grants for你的使用者;

show grants forroot@'localhost';

show grants for [email protected];

show createdatabase dbname; 這個可以看到建立資料庫時用到的一些引數。

showcreatetabletickets; 可以看到建立表時用到的一些引數

撤銷許可權:

revoke all on . from dba@localhost;

四、直接向mysql.user表插入記錄(該方法個人很少用)

因為資料庫的使用者資訊都是儲存在mysql.user這張表的,所以直接對該錶進行插入語句,即可完成使用者的建立;

mysql> insert into user (host,user,password) values ('%','john',password('123'));

五、完成使用者的建立後,請記得重新整理系統許可權表;

mysql>flush privileges;

mysql 建立使用者指令碼 Mysql使用者建立指令碼

我試圖自動化mysql使用者建立過程。我想到建立乙個包含mysql使用者建立語句的臨時檔案,那麼我會這樣稱呼 mysql u root proot 這裡是我的臨時檔案的內容 drop database if exists mytestdatabase create database mytestda...

mysql 建立使用者

mysql grant all privileges on to root identified by with grant option query ok,0 rows affected 0.02 sec 表示是所有的外部機器,如果指定某一台機,就將 改為相應的機器名 mysql grant al...

MYSQL 建立使用者

一,建立使用者 命令 create user username host identified by password 說明 username 你將建立的使用者名稱,host 指定該使用者在哪個主機上可以登陸,如果是本地使用者可用localhost,如果想讓該使用者可以從任意遠端主機登陸,可以使用萬...