標籤:des style os ar for 檔案 sp 資料 on
前一段時間,將項目改成SAAS的架構,每個billing account都可以獲得一個子域,一個單獨的資料庫,一個單獨的使用者和對應資料可以的許可權。
現在有時間了,將資料庫相關的命令用部落格備份一下。其中有些沒有嘗試過,如有錯誤請指正。
1. 建立使用者
create user ‘username‘@‘host‘ identity by ‘password‘;
insert into mysql.user(Host,User,Password) values("host", "username", password("password"));
如果希望指定的使用者只能從某台指定的域(domain)或主機訪問,可以在建立使用者時指定host,例如10.10.10.22,如果希望能夠從本地登入,可以將host設成localhost,如果希望在個台機器上都能連結,可以將host設為%。
2. 建立查看資料庫
show databases;//顯示資料庫
create database dbname; //建立一個資料庫
use dbname;//進入資料庫
show tables;//顯示表
desc tablename;//顯示表結構
source sql/file/path;匯入sql檔案
3. 使用者授權
grant all privileges on dbname.* to [email protected]‘%‘ identified by ‘password‘;//授權username使用者擁有dbname資料庫的所有許可權
grant select, update on dbname.* to [email protected]‘%‘ identified by ‘password‘;//授權username使用者擁有dbname資料庫的指定部分許可權
4. 重新整理授權
flush privileges;
5. 刪除使用者和撤銷許可權
drop user [email protected];//取消一個賬戶和其許可權
revoke privilege on dbname.tablename FROM ‘username‘@‘host‘;//取消授權使用者
delete from user where user = "username" and host = "host";//刪除使用者
6. 修改使用者密碼
mysqladmin -uroot -proot password 123;//將root使用者的密碼改為123
update mysql.user set password=password(‘新密碼‘) where user="username" and host="localhost";
set password for ‘username‘@‘host‘ = password(‘newpassword‘);
7. 刪除資料庫
drop database dbname;//刪除一個已經確定存在的資料庫
alter table 表名 ENGINE=儲存引擎名;//修改表的儲存引擎
alter table 表名 drop 屬性名稱;//刪除欄位
alter table 舊錶名 rename to 新表名;//修改表名
alter table 表名 modify 屬性名稱 資料類型;//修改欄位資料類型
alter table 表名 change 舊屬性名稱 新屬性名稱 新資料類型;//修改欄位名
alter table 表名 drop FOREING KEY 外鍵別名;//刪除子表外鍵約束
alter table example add phone VARCHAR(20);//增加無約束的欄位
alter table example add age INT(4) NOT NULL;//增加有約束的欄位
alter table example add num INT(8) PRIMARY KEY FIRST;//表的第一個位置增加欄位
alter table example add address VARCHAR(30) NOT NULL AFTER phone;//表的指定位置之後增加欄位
alter table example modify name VARCHAR(20) FIRST;//把欄位修改到第一位
alter table example modify num INT(8) ATER phone;//把欄位修改到指定欄位之後
MySQL的學習--使用者建立授權