mysql服務的配置和使用
修改mysql管理員的口令:mysqladmin –u root password 密碼字串
如:mysqldmin –u root password 111111
連線mysql伺服器,使用命令: mysql [-h 主機名或ip位址] [-u 使用者名稱] [-p]
如:mysql –u root –p
如已有密碼需修改root密碼用命令: mysqladmin –u root –p password 新密碼字串
如:mysqladmin –u root –p password 111111
建立資料庫格式為:create database 資料庫名稱;
如:mysql>create database abc; 預設建立資料庫儲存在/var/lib/mysql中
檢視資料庫是mysql>show abc;
選擇資料庫是use 資料庫名稱;如:mysql>use abc;
刪除資料庫是drop database 資料庫名稱; 如:mysql>drop database abc;
資料庫的建立和刪除
建立表是 create table 表名稱(欄位1,欄位2,…[表級約束]) [type=表型別];
其中字段(1,2 )格式為:欄位名字段型別 [字段約束]
如建立乙個表student,如下:
mysql>create table student (
sno varchar(7) not null, 欄位不允許為空
sname varchar (20 )not null,
s*** char (1) default 『t』,
sbirthday date,
sdepa char (20),
primary key (sno) 表的主鍵
);可用describe命令檢視表的結構。
預設表的型別為myisam,並在/var/lib/mysql/abc 目錄下建立student.frm(表定義檔案),student.mdy(資料檔案),stedent.
myi(索引檔案)。
複製表 create table 新錶名稱 like 原表名稱;
如:mysql>create table xtable like student;
刪除表 drop table 表名稱1[表名稱2…];
如:mysql> drop table xtale;
修改表 alter table 表名稱更改動作1[動作2];
動作有add(增加) drop(刪除)change、modify(更改欄位名和型別)rename
增加字段:mysql>alter table student add saddress varchar(25);
更改欄位名和字段型別: mysql>alter table student change saddress sremark test;
即使不更改字段型別也要給出字段型別如:
mysql>alter table student change saddress sremark varchar (25);
更改字段型別 :mysql> alter table student modify sremark varchar(25);
刪除字段:mysql>alter table student drop sremark;
更改表名稱: mysql>alter table student rename to xs;
表中資料的插入、刪除和修改
插入記錄: insert into 表名稱(欄位名1,欄位名2…
values(欄位1的值,欄位2的值
如:mysql>insert into student (sno,sname,s***,sbirthday,sdepa)
values(『0321001』,』liu tao』,dagault,19870201,』math』);
檢視表mysql>select * from student;
插入與前面相同的記錄,可用insert命令的縮寫格式,
如: mysql>insert into student values (『0321001』, 『liu tao』, default, 19870201, 『mth』);
如果欄位名列表中沒有給出表中的某些字段,那麼這些字段設定為預設值,
如:mysql>insert into student (sno,sname,sbirthday)
values(『0321002』,』wang jun』,1870112);
乙個單獨的insert語句中可使用多個valuse字句,插入多條記錄,
如:mysql>insert intostudent values
(『0322001』, 『zhang liaoyun』, 『f』 1971102,』***puter』),
(『0322002』, 『li ming』, 『t』 1971105,』***puter』);
刪除記錄: delete from 表名稱 where 條件表示式;
如:mysql>delete from student where sno=』0321002』;
刪除student表中sno欄位值前4位為『0322』的記錄
如:mysql>delete from student where left (sno,4)=』0322』;
刪除所以記錄,可以不帶where字句
如:mysql>delete from student;
刪除所以記錄可以用命令truncate 刪除表,然後重建表,所以比delete命令快
如:mysql>truncate table student;
修改記錄 update 表名稱 set 欄位名1=字段值1
where 條件表示式
如: mysql>update student set sbirthday=1920113, sdepa=』math』 where sno=』0321002』;
索引的建立與刪除
在建立表的同時建立索引
建立表時,可用index字句或unique(字段值必須惟一)字句建立索引
如:建立課程表course, 課程編號**o欄位為主鍵,課程名稱**ame欄位建立乙個名為can的索引
mysql>create table course(
**o varchar(5) not null,
**ame varchar(30) not null,
teacher varchar(20),
primary key (**o),
index can (**ame)
); 向已存在的表新增索引 create [unique ] index 索引名on表名稱 (欄位名1[(長度)]);
如:mysql>create index sna on student (sname);
對於型別為char和varchar的字段建立索引時還可指定長度值,型別為blob和text的字段索引時必須指定長度值。
如mysql>create index sna on student (sname(10));
刪除索引drop index 索引 on表名稱;
如:mysql>drop index sna on student;
使用者的建立和刪除
初始化時有5個mysql授權表,其中host、tables_priv和columnts_priv 是空的,表user和db決定了mysql預設的訪問規則。預設有mysql和test兩個資料庫。
授權表:user使用者從哪些主機可以連線到資料庫伺服器,以及對所以資料庫的訪問許可權(全域性許可權)
db 使用者可以使用哪些許可權,以及對資料庫執行哪些操作(資料庫級許可權)
host當表db 中的host 字段值為空時,使用者從哪些主機可以連線到資料庫伺服器。
tables_priv連線的使用者可以訪問哪些表(表級許可權)
columnts_priv連線的使用者可以訪問哪些字段 (字段級許可權)
建立新使用者
以mysql管理員連線到資料庫伺服器: #mysql –u root –p
建立新使用者guess並設定密碼,同時可以從任何主機連線資料庫伺服器:
mysql>insert into (host,user,password)
values (『%』,』gusee』,password(『guest』)); 使用password()函式,密碼是加密的
過載mysql授權表:mysql>flush privileges;
遠端客戶端連線資料庫伺服器 :#mysql –h 192.168.0.50 –u guess –p 開放伺服器的tcp斷口3306
檢視當前使用者可用資料庫: show database
刪除使用者
mysql>delete from where user=』guest』;
mysql>flush privileges; 過載mysql授權表
更改使用者密碼
如:更改guset密碼為123456
mysql>update set password=password(『123456』)
where user =』guset』;
mysql>flush privileges;
或者是 mysql>set password for guset@』%』=password(『123456』);
使用者許可權的設定
在表user、db和host中,所有字段宣告為enum(『n』,』y』),預設是『n』;
在表tables_priv和columns_priv中,許可權字段宣告為set型別
修改授權表中的訪問許可權有兩中方法,一是使用 insert、update和delete等dml語句,
另一中是grant和grvoke語句
使用grant語句授權:
格式如下:
grant 許可權列表 [(字段列表)] on 資料庫名稱.表名稱
to 使用者名稱@網域名稱或ip位址
[indetified by 『密碼值』] [with crant option];
授權哪個使用者能連線,從哪連線
如:授權使用者guest從任意主機連線資料庫伺服器,並具有完全訪問資料庫abc的許可權。
mysql>grant all on abc.* to guset@』%』 identified by 『guest』
注意幾點:如指定使用者不存在,則建立該新使用者;
『許可權列表』處all表示授予全部許可權,usage表不授予任何許可權。
『資料庫名稱.表名稱』處可以使用萬用字元「*」。如「abc.*」表資料庫abc中所有表
『使用者名稱@網域名稱或ip位址』設定誰能連,從哪連。使用者名稱不能用萬用字元,但可以用『 』空字串,表任何使用者;網域名稱或ip位址可以用萬用字元「%」,使用是用單引號括起來。
授權使用者不同級別的訪問許可權
如:新建使用者tom,能從子網192.168.16.0訪問資料庫伺服器,可以讀取資料庫xsxk,並能修改表course 中欄位teacher的值
mysql>grant select on xsxd.* to tom@』192.168.16.%』 indentifiend by 『123456』;
mysql>grant update(teacher) on to tom@』192.168.16.%』』
注意幾點:資料庫名稱.表名稱用來設定許可權運用的級別,有全域性的(*.*),指定資料庫的(xsxd.*)
和指定表的(xsxd.student);
字段列表設定許可權運用中指定的表中的哪些字段,如update(**ame,teacher)
許可權列表指定的許可權與許可權執行的級別有關,如有寫許可權(file、process、reload、shutdown)作為管理許可權用於全域性級別;對於字段級別只能指定select、insert、update、references
授予使用者管理許可權的權利
如:管理員授予擁護admin可以從本地連線資料庫伺服器,對資料庫xsxk具有完全訪問許可權,並可以
將擁有的許可權賦予其他使用者
mysql>grant all on xsxd.* to admin@localhost indentified by 『123456』 with grant option;
其中with grant option 子句表示擁護擁有的許可權可以賦予其他使用者。
mysql>qrant select on xsxd.student to bill@localhost;授予bill使用者許可權
mysql>show grants for admin@localhost; 檢視使用者許可權
使用revoke語句撤權
格式如下:
revoke 許可權列表[(字段列表)] on資料庫名稱.表名稱
from使用者名稱@網域名稱或ip位址
如:撤消使用者admin@localhost 對資料庫xsxd的建立、刪除資料庫及表的許可權,不撤消使用者賦予其它使用者的許可權
mysql>revoke create,drop on xsxd.* from admin@localhost;
mysql>revoke grant option on xsxd.* from admin@localhost;
常用MySQL語句大全
mysql服務的配置和使用 修改mysql管理員的口令 mysqladmin u root password 密碼字串 如 mysqldmin u root password 111111 連線mysql伺服器,使用命令 mysql h 主機名或ip位址 u 使用者名稱 p 如 mysql u ro...
Mysql基礎常用語句命令
一資料庫命令 1建立資料庫 create database name 2顯示資料庫列 show databases 3刪除資料庫 drop database name 4檢索先用資料庫 select database 5檢視資料庫使用的字元 show variables like character...
mysql常用指令
1.匯出整個資料庫 mysqldump u 使用者名稱 p default character set latin1 資料庫名 匯出的檔名 資料庫預設編碼是latin1 mysqldump u wcnc p smgp apps wcnc 2.匯出乙個表 mysqldump u 使用者名稱 p 資料庫...