啟動(dòng):
service?mysqld?start
service?mysqld?restart
service?mysqld?stop
/usr/bin/mysqld_safe?&
mysqladmin?-uroot?shutdown?-p
mariadb: yum安裝
systemctl start mariadb
連接:
mysql?-u?root?-p123123
指定數(shù)據(jù)庫連接:mysql?-u?root?-D?test?-p123123
指定套接字連接:mysql?-u?root?-p?-S?/var/lib/mysql/mysql.sock
遠(yuǎn)程主機(jī)連接:mysql?-u?root?-p?-h?192.168.1.103?-P?3306
連接并執(zhí)行sql語句:mysql?-u?root?-p123123?-e?'use?mysql;?select?user,host,password?from?user;'
mysql?-uroot?-p123123?-e?'create?database?if?not?exists?testdb;?show?databases;'
獲取幫助:
help
help create
help create database
查看幫助分為哪幾類:help contents
查看MySQL變量:
show global variables \G;
show global variables like '%version%' \G;
show global variables where variable_name like '%log%' and value = 'off'
全局變量 會(huì)話變量
show session variables \G;
查看單個(gè)變量:
select @@global.pid_file;
select @@session.warning_count;
select @@warning_count;
查看狀態(tài)變量:
show status;
show global status;
show session status;
修改/設(shè)定變量值:
set @@session.autocommit=0;
set global var_name = value;
set @@global.var_name = value;
mysql 用戶賬號(hào)格式:
username@host
_:表示任意單個(gè)字符
%:表示任意長度的任意字符
eg. root@'10.1.%.%'
查詢當(dāng)前庫中的用戶:
use mysql
selet user,host,password from user;
創(chuàng)建用戶:
create user 'mds'@'localhost';
create user 'mds'@'localhost' identified by '123123';
在授權(quán)數(shù)據(jù)庫時(shí)子巾,如果對(duì)應(yīng)用戶不存在,則會(huì)自動(dòng)創(chuàng)建:
grant?all?on?zsythink.*?to?zsy@127.0.0.1?identified?by?'zsythink';
刪除用戶:
drop user mds@'localhost';
重命名用戶:
rename?user?OldName?to?NewName;
使用mysqladmin設(shè)置密碼:
mysqladmin -u username -p oldpassword password newpassword
使用root用戶來設(shè)置密碼:
set password for 'user'@'localhost' = PASSWOR('newpassword');
在mysql5.6及以前版本可用:
update mysql.user set password = password('密碼') where user = '用戶名';
在mysql5.7及其以后的版本可用:
update mysql.user set authentication_string = password('密碼') where user = '用戶名';
找回root密碼:
1. 停止mysql進(jìn)程
2. 使用mysqld_safe --skip-grant-table & 可繞過用戶驗(yàn)證啟動(dòng)。
3. 用mysql -uroot 登陸數(shù)據(jù)庫
4.?mysql>?UPDATE?mysql.user?SET?password=PASSWORD("new?password")?WHERE?user='root';
5.?mysql>?FLUSH?PRIVILEGES;
6. 停止mysql服務(wù),并重啟。
授權(quán)命令:
GRANT?ALL?[PRIVILEGES]?ON?db.tbl?TO?'username'@'host'?IDENTIFIED?BY?'password';
grant?all?privileges?on?zsythink.*?to?zsy@127.0.0.1?identified?by?'zsythink';
grant?all?on?zsythink.*?to?zsy@127.0.0.1?identified??by??'zsythink';
給遠(yuǎn)程用戶授權(quán):
grant?all?on?zsythink.*?to?zsy@127.0.0.1?identified??by??'zsythink';
記住使用FLUSH?PRIVILEGES 命令刷新祝迂。
grant?insert,delete,update,select?on?zsythink.*?to?zsy@'192.168.%.%';
grant?select?on?hellodb.*?to?zsy@localhost,zsythink@localhost;
指明授權(quán)某個(gè)函數(shù)test的權(quán)限給某個(gè)用戶:
grant?select?on?hellodb.*?to?zsy@localhost,zsythink@localhost;
指明授權(quán)某個(gè)存儲(chǔ)過程test的權(quán)限給某個(gè)用戶:
grant?execute?on?procedure?zsythink.test?to?zsy@'192.168.%.%';
當(dāng)一個(gè)用戶被創(chuàng)建時(shí),自動(dòng)獲得usage權(quán)限,usage權(quán)限只能用于登陸數(shù)據(jù)笛谦,不能執(zhí)行其他操作。
強(qiáng)制遠(yuǎn)程用戶使用ssl建立會(huì)話:
grant?usage?on?*.*?to?'zsy'@'222.222.222.222'?require?ssl;
撤銷強(qiáng)制使用ssl :
grant?usage?on?*.*?to?'zsy'@'222.222.222.222'?require?none;
查看授權(quán):
show?grants?for?用戶名;
show?grants?for?zsy@localhost;
從數(shù)據(jù)庫的角度查看授權(quán):
select?*?from?mysql.db?where?Db="你要查看的數(shù)據(jù)庫"
刪除授權(quán):
revoke?"要移除的權(quán)限"?on?數(shù)據(jù)庫.表?from?用戶@host;
revoke?all?on?word.*?from?zsy@www.zsythink.net;
創(chuàng)建數(shù)據(jù)庫:
create?database?testdb;
create?database?if?not?exists?testdb;
create?database?if?not?exists?testdb?default?character?set?utf16;
查看所有數(shù)據(jù)庫:
show?databases;
查看建表語句:
show?create?database?testdb;
查看可用字符集:
show?character?set;
查看排序方式:
show?collation;
查看當(dāng)前數(shù)據(jù)庫當(dāng)前連接信息:status
修改數(shù)據(jù)庫一般指修改數(shù)據(jù)庫的字符集和排序規(guī)則:
alter?database?testdb?character?set?utf8;
alter?database?testdb?default?character?set?utf8;
刪除數(shù)據(jù)庫:
drop?database?if?exists?testdb;
查看表:
show?tables;
查看表的具體屬性:
show?table?status\G;
查看某個(gè)具體的表的狀態(tài):
show table status like 'test' \G;
查看表結(jié)構(gòu):
desc?Table_Name;
查看建表語句:
show?create?table?table_name;
建表:
create table test1 (id int(11) key, name varchar(65) not null comment 'student name ');
create table test1 (id int(11) , name varchar(65) not null comment 'student name ', primary key(id));
create table test2 (id int primary key , name varchar(65) not null comment 'student name ', tid int, foreign key(tid) reference test3(id));
創(chuàng)建表時(shí)就創(chuàng)建索引:
create table test4 (id int(11), name varchar(50), primary key(id),
key ind_name(name));
create table students ( id int primary key auto_increment, name varchar(66) not null, age tinyint unsigned, gender enum('f','m') default 'm', index(name) );
復(fù)制一個(gè)表(只復(fù)制表結(jié)構(gòu)):
create table test00 like test11;
復(fù)制一個(gè)表(只復(fù)制表數(shù)據(jù)):
create table test00? select * from test11;
表選項(xiàng):
create table tt (id int primary key auto_increment, name varchar(55) not null, index ind_name(name) ) engine=InnoDB auto_increment=2 default charset=utf8;
使用help create table可查看更多表選項(xiàng)昌阿。
刪除表:
drop table if exists tt,ttt;
修改表名:
alter?table?test1?rename?as?test2;
添加字段:
alter?table?ttt?add?column?age?int;
alter?table?ttt?add?age?int;
添加字段的同時(shí)添加約束:
alter?table?ttt?add?age?int?not?null?default?0;
alter?table?ttt?add?column?age?int?not?null?default?0;
添加字段的同時(shí)指定位置:
alter?table?ttt?add??id?int?first;
alter?table?ttt?add?column?age??int?after?name;
刪除字段:
alter?table?tt?drop?stuname;
重命名字段:
alter?table?testtable?change?name?name1?char(5);
修改字段類型:
alter?table?testtable?change??age?age?char(10);
alter?table?testtable?modify?age?int;
主鍵:不能為空饥脑,不能相同,只能有一個(gè)主鍵懦冰,可由多個(gè)字段組成
唯一鍵:可為空灶轰,不能相同,可有多個(gè)主鍵刷钢,可由多個(gè)字段組成
外鍵:一個(gè)表中外鍵字段所能插入的數(shù)據(jù)范圍笋颤,取決于引用的另一個(gè)表主鍵字段上已經(jīng)存在的數(shù)據(jù)集合。
檢查約束條件:check 自定義的邏輯表達(dá)式内地。
在創(chuàng)建主鍵伴澄,唯一鍵時(shí)會(huì)自動(dòng)創(chuàng)建索引。
添加非空約束:
alter?table?testtb?modify?name?varchar(100)?not?null;
刪除非空約束:
alter?table?testtb?modify?name?varchar(100)?null;
添加自動(dòng)增長:
alter?table?testtb?modify?id?int?auto_increment;
alter?table?testtb?change?id?id?int?auto_increment;
刪除自動(dòng)增長:
alter?table?testtb?change?id?id?int;
alter?table?testtb?modify?id?int;
添加主鍵約束:
alter?table?testtb4?add?primary?key(id);
alter?table?testtb4?add?constraint?primary?key(id);
刪除主鍵約束:
要先刪除自動(dòng)增長阱缓,若已被其他字段當(dāng)做外鍵非凌,則還需要先刪除外鍵
alter?table?testtb?drop?primary?key;
添加唯一鍵:
alter?table?testtb?add?unique?key(uid);
添加唯一鍵時(shí)起個(gè)名稱:alter?table?testtb?add?unique?key(uid);
刪除唯一鍵:
alter?table?testtb?drop?index?uni_test;
查看約束(所有約束):
select?*?from?information_schema.key_column_usage?where?table_name='test1';
查看主鍵:
desc?test1
查看表的外鍵:
select?TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
from?information_schema.KEY_COLUMN_USAGE
where?TABLE_NAME?=?'test1'?and?REFERENCED_TABLE_NAME?is?not?null;
select?REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME,CONSTRAINT_NAME,TABLE_NAME,COLUMN_NAME
from?information_schema.KEY_COLUMN_USAGE
where?REFERENCED_TABLE_NAME?=?'test2';
添加外鍵約束:
alter?table?testtb?add?column?tid?int?default?0?not?null;
alter?table?testtb?add?constraint?testtb_tid_fk?foreign?key(tid)?references?testtb2(id);
刪除外鍵約束:
先查詢到外鍵名稱,再刪除
select?TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
from?information_schema.KEY_COLUMN_USAGE
where?TABLE_NAME?=?'test4'?and?REFERENCED_TABLE_NAME?is?not?null;
alter?table?test4?drop?foreign?key?test_tid_fk;
添加索引:
alter?table?testtb?add?index?ind_name(name);
create?index?ind_name?on?testtb?(name(20)?desc);
create?index?ind_name?on?testtb?(name(20));
create?index?ind_name?on?testtb?(name);
查看索引:
show index from testtb;
創(chuàng)建聯(lián)合索引:
create?index?ind_id_name?on?testtb1?(id,name);
create?index?ind_id_name?on?testtb1?(id,name(20));
查看以ind開頭的索引:
show?index?from?testtb?where?key_name?like?'ind%';
創(chuàng)建視圖前確認(rèn)有創(chuàng)建視圖的權(quán)限:
select create_view_priv,select_priv from mysql.user where user='root' and host='localhost';
創(chuàng)建視圖:
create view testvi as select * from classes where classid < 3;
create?or?replace?view?testvi?as?select?*?from?classes?where?classid?<=?4;
create view testvi(a,b,c) as select * from classes
指定算法創(chuàng)建視圖:
create algorithm=merge view testvi as select name,age from students;
算法有:merge? temptable? undefined(它自己選擇)
刪除視圖:
drop?view?testvi;
drop?view?if?exists?testvi;
查看視圖:
select * from information_schema.views where table_schema='U_DB_NAME';
desc testvi;
select * from testvi;
修改視圖:
alter view testvi as select name,age,gender from students;
一般不更新視圖中的數(shù)據(jù)荆针。
insert語句:敞嗡、
insert?into?tb1?(name,age)?values('tom',33);
insert?into?tb1?(name,age)?values('jerry',22),('naruto',28);
insert?into?tb1?values?(4,'Sasuke',28),(5,'hinata',25);
insert?into?tbl2?set?id=2,name="test",age=18,gender='M';
mysql默認(rèn)的sqlmode為寬松模式颁糟,即使插入的數(shù)據(jù)不是完全符合類型要求,也有可能插入數(shù)據(jù)喉悴,但是會(huì)被截?cái)唷?/p>
應(yīng)該講global.sql_mode變量的值設(shè)為TRADITIONAL使用的存儲(chǔ)引擎為innodb棱貌。
sql_mode常用模式:
ansi:寬松模式
strict_trans_tables:只對(duì)事務(wù)性表進(jìn)行嚴(yán)格限制。
strict_all_tables:對(duì)所有表嚴(yán)格限制粥惧。
traditional:嚴(yán)格模式键畴。
清空表中數(shù)據(jù):
delete from tb1;
刪除數(shù)據(jù):
delete?from?tb1?where?age=22;
delete?from?tb1?where?name?rlike?'^t.*';
delete?from?tb1?where?age?>?30?order?by?age?desc?limit?1;
更新數(shù)據(jù):
update?tb1?set?age?=?28;
update?tb1?set?name='luffy'?where?id=13;
update?tb1?set?name='luffy',age=25?where?id=13;
select語句:
select?*?from?tb1?limit?3;
‘_'表示任意單個(gè)字符:
select?*?from?tb1?where?name?like?'t__';
正則表達(dá)式:
select?*?from?tb1?where?name?rlike?'^t.*';
select?*?from?tb1?where?age?in?(22,23,24,25);
select?*?from?tb1?where?age?not?in?(28,33,43);
select?*?from?tb1?order?by?age;
select?*?from?tb1?order?by?age?asc;
select?*?from?tb1?order?by?age?desc,name?asc;
select?distinct?age?from?students;
select?name?as?StuName,age?from?tb1;
select中的分組與聚合:
select avg(age),gender from students group by gender;
常用聚合函數(shù):
min(col)? ? max(col)? ? avg(col)? ? count(col)? ? sum(col)? ? group_concat(col)
對(duì)分組后的信息再次過濾可以使用having關(guān)鍵字。
select?classid,avg(age)?as?avgage?from?students?group?by?classid?having?avgage?>?25;
select?sum(age)?from?students?where?age?>?19?group?by?gender;
多表查詢:
交叉連接突雪,即沒有任何限制條件的連接起惕。“笛卡爾乘積” cross join
select?*?from?t1?cross?join?t2?cross?join?t3;
select?*?from?t1,t2,t3;
內(nèi)連接:inner join
兩張表中同時(shí)符合某種條件的數(shù)據(jù)記錄的組合咏删。
select * from t1,t2 where t1.t1id = t2.t2id;
自連接:
自連接把同一張表當(dāng)做兩張表連接起來
select * from students s1, students t1 where s1.tid = t1.id;
inner join = join
select * from t1? join t2 on t1.t1id=t2.t2id;
外連接:
左外連接:left outer join?
包含左表以及左表和右表公共的惹想。
查屬于t1但不屬于t2的:
select * from t1 left join t2 on t1id=t2id where t2id is null;
聯(lián)合查詢:
即把多個(gè)查詢語句的結(jié)果集中在一起顯示。兩個(gè)語句查出的字段數(shù)量必須相同督函,否則無法使用union進(jìn)行聯(lián)合查詢嘀粱。
select * from t2 union select t3str2 t3str1 from t3;
union all? 不合并相同的項(xiàng)。
全連接:
mysql 不支持全連接辰狡,但可通過union聯(lián)合 左連接和右連接實(shí)現(xiàn)全連接锋叨。
查詢緩存:
看看有沒有開:show variables like '%query_cache%';
清除緩存:reset?query?cache;
存儲(chǔ)引擎:
myisam:支持表級(jí)鎖,不支持行級(jí)鎖宛篇,不支持事務(wù)娃磺,不支持外鍵約束,支持全文索引叫倍,表空間相對(duì)小偷卧。
innodb:支持表級(jí)鎖,行級(jí)鎖吆倦,支持事務(wù)听诸,支持外鍵,不支持全文索引蚕泽,表空間文件相對(duì)較大晌梨。
show? engines;
innodb 與myisam的數(shù)據(jù)文件:
innodb: 后綴為frm的是存儲(chǔ)了表的表結(jié)構(gòu)信息。
后綴為ibd的存放了表的數(shù)據(jù)信息與索引信息须妻。
myisam:
后綴為frm的存放了表結(jié)構(gòu)信息派任。
后綴為MYD的存放了數(shù)據(jù)信息。
后綴為MYI的存放了索引信息璧南。
事務(wù):
ACID
A:atomicity 原子性掌逛,要么全成功要么全失敗回滾到最初狀態(tài)。
C:consistency 一致性司倚,總是從一個(gè)一致性狀態(tài)轉(zhuǎn)為另一個(gè)一致性狀態(tài)豆混。
I:isolation 隔離性篓像,一個(gè)事務(wù)在提交之前所作出的操作能否為其他事務(wù)可見,有不同的隔離級(jí)別皿伺。
D:durability 持久性员辩,事務(wù)一旦提交所作出的修改是永久保存。