管理维护MySQL的SQL语句有哪些_MySQL常用管理SQL语句
前言
DDL
DDL,中文為數(shù)據(jù)定義語(yǔ)言,DDL的特點(diǎn)是對(duì)數(shù)據(jù)庫(kù)內(nèi)部的對(duì)象進(jìn)行create(創(chuàng)建)、alter(修改)、drop(刪除)等操作,負(fù)責(zé)管理數(shù)據(jù)庫(kù)的基礎(chǔ)數(shù)據(jù),不涉及對(duì)表中內(nèi)容的操作和更改。
DCL
DCL,中文為數(shù)據(jù)控制語(yǔ)言,DDL的特點(diǎn)是對(duì)數(shù)據(jù)庫(kù)內(nèi)部的對(duì)象grant(用戶授權(quán))、revoke(權(quán)限回收)、commit(提交)、rollback(回滾)。
DML
DML,中文為數(shù)據(jù)操作語(yǔ)言,DML的特點(diǎn)是對(duì)數(shù)據(jù)庫(kù)內(nèi)部的對(duì)象insert(增)、delete(刪)、update(改)、select(查),主要針對(duì)數(shù)據(jù)庫(kù)中表內(nèi)的數(shù)據(jù)進(jìn)行操作。
1.DDL語(yǔ)句之管理數(shù)據(jù)庫(kù)
1.1.創(chuàng)建數(shù)據(jù)庫(kù)
create database test; #創(chuàng)建默認(rèn)字符集數(shù)據(jù)庫(kù)
create database test_gbk character set gbk collate gbk_chinese_ci; #創(chuàng)建gbk字符集數(shù)據(jù)庫(kù)
create database test_utf8 character set utf8 collate utf8_general_ci; #創(chuàng)建utf8字符集數(shù)據(jù)庫(kù)
show create database testG #查看建庫(kù)語(yǔ)句
1.2.顯示數(shù)據(jù)庫(kù)
show databases; #查看所有數(shù)據(jù)庫(kù)
show databases like 'test'; #匹配test字符串的數(shù)據(jù)庫(kù)
show databases like 'test%'; #%為通配符,表示匹配以test開(kāi)頭的所有數(shù)據(jù)庫(kù)
1.3.切換數(shù)據(jù)庫(kù)
use test; #切換到test庫(kù)
select database(); #查看當(dāng)前管理員所在的庫(kù)名
1.4.查看數(shù)據(jù)庫(kù)包含的表信息
1.4.1.切換到數(shù)據(jù)庫(kù)里面去查看表信息
use test;
show tables;
1.4.2.在庫(kù)外面查看庫(kù)里的表信息
show tables from test; #查看指定庫(kù)test中包含的表
show tables in test_gbk;
show tables from mysql like 'db%'; #匹配包含指定字符開(kāi)頭的表
1.5.刪除數(shù)據(jù)庫(kù)
drop database test_gbk; #刪除test_gbk數(shù)據(jù)庫(kù)
show databases like 'test_gbk';
2.DDL&&DCL語(yǔ)句之管理用戶
2.1.查看當(dāng)前數(shù)據(jù)庫(kù)的用戶列表
查看數(shù)據(jù)庫(kù)用戶列表屬于DML負(fù)責(zé)的部分內(nèi)容。
select user,host from mysql.user; #這里的select關(guān)鍵字表示查詢,是DML語(yǔ)句的關(guān)鍵字之一,user和host為要查找的MySQL表的字段,from表示去哪查,mysql.user是MySQL庫(kù)里的user表。數(shù)據(jù)庫(kù)的標(biāo)準(zhǔn)用戶由“用戶”@“主機(jī)名”共同組成的,兩者加起來(lái)是數(shù)據(jù)庫(kù)用戶的唯一標(biāo)識(shí)。
2.2.創(chuàng)建數(shù)據(jù)庫(kù)用戶
create user test1@localhost identified by '123456';
select user,host from mysql.user; #查看所有數(shù)據(jù)庫(kù)用戶
create user test2@'192.168.1.%' identified by '123456';
select user,host from mysql.user where user='test2; #查看指定數(shù)據(jù)庫(kù)用戶
show grants for test2@'192.168.1.%';
#USAGE表示連接權(quán)限
2.3.刪除數(shù)據(jù)庫(kù)用戶
drop user 'test1'@'localhost'; #刪除數(shù)據(jù)庫(kù)用戶命令
select user,host from mysql.user where user='test1'; #刪除后檢查數(shù)據(jù)庫(kù)用戶
flush privileges; #使得處理用戶后,對(duì)數(shù)據(jù)庫(kù)生效,有數(shù)據(jù)庫(kù)改動(dòng)的情況,執(zhí)行這個(gè)命令
delete from mysql.user where user='test2' and host='192.168.1.%';
flush privileges;
2.4.授權(quán)數(shù)據(jù)庫(kù)用戶
2.4.1.創(chuàng)建test3用戶,對(duì)test庫(kù)具備所有權(quán)限,允許從localhost主機(jī)登錄管理數(shù)據(jù)庫(kù),密碼是123456。
grant all privileges on test.* to 'test3'@'localhost' identified by '123456';
select user,host from mysql.user where user='test3';
show grants for 'test3'@'localhost';
#ALL PRIVILEGES就是授權(quán)的權(quán)限
2.4.2.授權(quán)與root同等地位的system用戶權(quán)限。
show grants for root@localhost; #查看root用戶的權(quán)限
grant all on *.* to 'system'@'localhost' identified by '123456' with grant option;
grant proxy on ''@'' to 'system'@'localhost' with grant option; #允許創(chuàng)建代理用戶
show grants for system@localhost;
2.5.授權(quán)的權(quán)限列表
show grants for 'test3'localhost';
#ALL PRIVILEGES就是授權(quán)的權(quán)限
revoke select on test.* from 'test3'@'localhost';
show grants for 'test'@'localhost';
#權(quán)限ALL被拆分成了更細(xì)的權(quán)限
MySQL的ALL PRIVILEGES的權(quán)限列表
權(quán)限
說(shuō)明SELECT
查詢(數(shù)據(jù))
INSERT
插入(數(shù)據(jù))
UPDATE
修改(數(shù)據(jù))
DELETE
刪除(數(shù)據(jù))
CREATE
創(chuàng)建(數(shù)據(jù)庫(kù)、表等對(duì)象)
DROP
刪除(數(shù)據(jù)庫(kù)、表等對(duì)象)
RELOAD
重載
SHUTDOWN
關(guān)閉
PROCESS
進(jìn)程
FILE
文件
REFERENCES
參考資料
INDEX
索引
ALTER
修改(數(shù)據(jù)庫(kù)、表等對(duì)象)
SHOW DATABASES
查看數(shù)據(jù)庫(kù)
SUPER
超級(jí)權(quán)限
CREATE TEMPORARY TABLES
創(chuàng)建臨時(shí)表
LOCK TABLES
鎖表
EXECUTE
執(zhí)行
REPLICATION SLAVE
從復(fù)制權(quán)限
REPLICATION CLIENT
從客戶端復(fù)制
CREATE VIEW
創(chuàng)建視圖
SHOW VIEW
查看視圖
CREATE ROUTINE
創(chuàng)建存儲(chǔ)過(guò)程
ALTER ROUTINE
修改存儲(chǔ)過(guò)程
CREATE USER
創(chuàng)建用戶
EVENT
事件
TRIGGER
觸發(fā)器
CREATE TABLESPACE
創(chuàng)建表空間
2.6.企業(yè)中g(shù)rant授權(quán)權(quán)限問(wèn)題說(shuō)明
2.6.1.企業(yè)里主數(shù)據(jù)庫(kù)用戶的授權(quán)問(wèn)題說(shuō)明
在企業(yè)生產(chǎn)環(huán)境中,如果是以web形式連接數(shù)據(jù)庫(kù)的用戶,那么盡量不要授予all權(quán)限,最好是分拆授權(quán),比如,授予select、insert、update、delete等適合web使用的DML語(yǔ)句關(guān)鍵字權(quán)限。
grant select,insert,update,delete on oldboy.* to test3@'172.16.1.%' identified by '123456';
#注意:授權(quán)用戶權(quán)限時(shí)有如下3條安全紅線不要輕易跨過(guò)。
1、權(quán)限不能用all,要應(yīng)用select、insert、update、delete等具體權(quán)限。
2、庫(kù)不能用"*.*",而應(yīng)用"oldboy.*"格式具體到庫(kù)。
3、主機(jī)不能用%,而應(yīng)用內(nèi)網(wǎng)IP段,即'192.168.0.%'格式。
PHP程序語(yǔ)言連接MySQL的代碼:
//$link_id=mysql_connect('數(shù)據(jù)庫(kù)主機(jī)名','用戶','密碼');
$link_id=mysql_connect('172.16.1.7')
if($link_id){
echo "mysql successful by oldboy !";
}else{
echo mysql_error();
}
?>
內(nèi)容來(lái)源于網(wǎng)絡(luò)如有侵權(quán)請(qǐng)私信刪除
總結(jié)
以上是生活随笔為你收集整理的管理维护MySQL的SQL语句有哪些_MySQL常用管理SQL语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python模拟ajax请求_短信炸弹—
- 下一篇: MySQL innosetup_使用In