四,MySQL
4.1 mysql安裝
#centos7默認(rèn)安裝的是MariaDB-5.5.68或者65,
#查看版本的指令:[root@web01 bbs]# rpm -qa| grep mariadb
#安裝mariadb的最新版,只是更新了軟件版本,不會刪除之前原有的數(shù)據(jù)。
#修改yum源的配置文件
vim /etc/yum.repos.d/mariadb.repo
i[mariadb]
name=mariadb laster version
baseurl=http://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.6/centos7-amd64/
gpgcheck=0
#yum安裝mariadb
yum install mariadb-server -y
#重新啟動mariadb并設(shè)置開機(jī)自啟
systemctl start mariadb
systemctl enable mariadb
# 安裝完之后建議運(yùn)行一下安全初始化的動作:
mysql_secure_installation
4-2 授權(quán)
#授權(quán) ,默認(rèn)情況下mysql和mariadb都是不允許root用戶遠(yuǎn)程連接登錄的。
# grant 操作(增刪改查,all) on 庫名.表名 to 用戶名@'%' identified by '密碼';
grant all on *.* to root@'192.168.31.%' identified by 'aini';
grant all on wordpress.* to wordpress@'192.168.61.%' identified by '123456';
grant all on wordpress.t1 to jaden@'192.168.61.%' identified by '123'; #jaden用戶只能對menu表進(jìn)行操作
# 網(wǎng)站代碼中連接數(shù)據(jù)庫的時候使用的是哪個用戶,那個用戶有什么權(quán)限,那么網(wǎng)站代碼就能對數(shù)據(jù)庫做什么操作。
## 查看某個用戶有哪些權(quán)限
show grants for root@'%';
# 單獨(dú)創(chuàng)建用戶
create user wang@'%' identified by '123';
# 單獨(dú)創(chuàng)建的用戶是沒有任何權(quán)限的,只能登錄,需要授權(quán)
grant all on wordpress.* to wang@'%';
# 上面兩條就等于我們前面授權(quán)加創(chuàng)建用戶的一條指令。
# 刪除用戶
drop user wang@'%';
# 查看用戶的權(quán)限
show grants for jaden@'192.168.61.%';
# 回收權(quán)限: 注意:只有在本機(jī)登錄的root用戶才有這個能力
revoke select on wordpress.t1 from jaden@'192.168.61.%';
show grants for jaden@'192.168.61.%';
4-3 登錄修改密碼
#使用普通用戶登錄
mysql -u wordpress -p123456 -h 10.0.0.7
#默認(rèn)的數(shù)據(jù)文件存儲位置是
/var/lib/mysql/
#/root/.mysql_history 記錄了我們做的歷史sql指令
# 修改用戶密碼
#安全初始化,可以修改root用戶的密碼:mysql_secure_installation
格式:mysql> set password for 用戶名@localhost = password('新密碼');
例子:mysql> set password for root@localhost = password('123');
# 查詢當(dāng)前是在哪個庫里面
MariaDB [mysql]> select database();
#查看表結(jié)構(gòu)
desc songs;
+---------+--------------+------+----
4-4 MySQL數(shù)據(jù)類型
int 整形 數(shù)字 適合存儲:年齡, 加減運(yùn)算
float 浮點(diǎn)型 適合存儲:余額 加減運(yùn)算
char 字符串 適合存儲:不做加減運(yùn)算 身份號碼 密碼,單行信息
text 文本 適合存儲: 適合多行信息,小說,商品描述
enum 枚舉 適合存儲: 固定選項(xiàng),多選一
date 日期類型 適合存儲:時間,一般存儲的是unix時間戳,從1970.1.1 0:0:0到現(xiàn)在過了多少秒,這個時間戳是可以轉(zhuǎn)化為具體的時間 日期的。
boolean 布爾類型 true/false 對應(yīng)數(shù)字就是0/非0
4-5 所有的整型int
4-6 字符串類型
文章來源:http://www.zghlxwxcb.cn/news/detail-743998.html
4-7 text類型
文章來源地址http://www.zghlxwxcb.cn/news/detail-743998.html
4-8 MySQL完整性約束
not null # 不能為空,默認(rèn)是可以為空的
default # default 100,意思是默認(rèn)值為100
unique # 唯一
auto_increment # 自增
primary key #主鍵:not null+unique,還自帶auto_increment自增屬性,但是每個表里面只能有一列能為primary key主鍵列
unsigned #只能存正整數(shù),默認(rèn)是可以存正數(shù)和負(fù)數(shù)的
4-9 MySQL數(shù)據(jù)表操作
#切換庫
use linux;
#創(chuàng)建表 #每個web項(xiàng)目其實(shí)都會創(chuàng)建很多個表來存儲不同的數(shù)據(jù)
create table 表名(
字段名1 類型[(寬度) 約束條件],
字段名2 類型[(寬度) 約束條件],
字段名3 類型[(寬度) 約束條件]
);
示例:
mysql> create table jaden(
-> id int,
-> name varchar(50),
-> age int(3)
-> );
#查看一下mysql幫我們創(chuàng)建表的時候的詳細(xì)指令
show create table jaden;
#創(chuàng)建庫和創(chuàng)建表的時候還可以指定字符集編碼,默認(rèn)字符集是Latin。
DEFAULT CHARACTER SET utf8mb4
create table jaden(id int, name varchar(50)) ENGINE=MyISAM DEFAULT CHARSET=utf8;
# ENGINE=MyISAM這是指定存儲引擎,這個后面說。
#往表里面插入數(shù)據(jù)
insert into jaden(id,name,age) value(1,'xx',18); # 插入單條數(shù)據(jù)
insert into jaden(id,name,age) values(2,'xx2',15),(3,'xx3',19); #插入多條數(shù)據(jù)
#創(chuàng)建只有name列的表t1;
create table t1(name char(6));
#查看表結(jié)構(gòu)
desc t1;
#往表t1插入數(shù)據(jù)
insert t1 value('zhang');
insert t1 value('li');
#查詢t1表中所有數(shù)據(jù)
select * from t1;
#指定字符集的創(chuàng)表語句
create table t2(name char(6),age int(3)) default charset=utf8;
#往表t2插入數(shù)據(jù)
insert t2 value('張三',20);
insert t2 value('李四',60);
#創(chuàng)建表t4
create table t4(name char(6),age int(3) default 0 ) default charset=utf8;
#指定列插入數(shù)據(jù)
insert t4(name) values('張三'),('李四');
#查詢結(jié)果
mysql> select * from t4;
+--------+------+
| name | age |
+--------+------+
| 張三 | 0 |
| 李四 | 0 |
+--------+------+
2 rows in set (0.00 sec)
##修改表
#修改字段的長度
alter table s2 modify name char(10);
#查看創(chuàng)表語句
show create table s2;
#增加字段
alter table s2 add age int(3);
#刪除字段
alter table s2 drop age;
#ALTER TABLE 表名 ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…] FIRST; #添加這個字段的時候,把它放到第一個字段位置去。
#ALTER TABLE 表名 ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…] AFTER 字段名;#after是放到后的這個字段的后面去了,我們通過一個first和一個after就可以將新添加的字段放到表的任意字段位置了。
# 修改表的字符集
alter table 表名 charset=utf8mb4;
#使用where條件刪除單條數(shù)據(jù)
delete from t5 where name='zhangsan';
#刪除所有數(shù)據(jù)
delete from t5;
#單條件修改:
update t5 set password='123' where name='wangwu';
#單條件修改多列:
update t5 set password='123',name='xxx' where name='wangwu';
#多條件修改
update t5 set password='123' where name='wangwu' and id=1;
update t5 set password='123' where name='wangwu' or id=1;
#修改所有數(shù)據(jù)
update t5 set password='123456';
4-10 MySQL查詢數(shù)據(jù)
#sql查詢
## city有多少個中國的城市?
select * from city where CountryCode='CHN';
## 查詢city表中,山西省的城市名?
select * from city where district='shanxi';
## 查詢city表中,山西省和河北省的城市名?
select * from city where district='shanxi' or district='hebei' ;
## 查詢city表中,山西省和河北省的城市中人口大于100w?
select * from city where (district='shanxi' or district='hebei') and Population >1000000 ;
## 查詢city表中,要求只顯示城市名和人口數(shù)量,山西省和河北省的城市名按人口數(shù)量排序,升序?
select Name,Population from city where district='shanxi' or district='hebei'order by Population ;
## 查詢city表中,要求只顯示城市名和人口數(shù)量,山西省和河北省的城市名按人口數(shù)量排序,降序?
select Name,Population from city where district='shanxi' or district='hebei'order by Population desc ;
## 查詢city表中,要求只顯示城市名和人口數(shù)量,山西省和河北省的城市名按人口數(shù)量前5名;
select Name,Population from city where district='shanxi' or district='hebei'order by Population desc limit 5;
## 查詢city表中,要求只顯示城市名和人口數(shù)量,山西省和河北省的城市名按人口數(shù)量第2名和第3名;
select Name,Population from city where district='shanxi' or district='hebei'order by Population desc limit 1,2;
## 查詢city表中,所有中國省份中帶an的城市
select * from city where countrycode='chn' and district like '%an%' ;
## 查詢city表中,所有中國的城市人口在89000和89999之間的城市
select * from city where countrycode='chn' and Population between 89000 and 89999 ;
## 查詢city表中,要求只顯示城市名和人口數(shù)量,查詢CHN人口最多的前5個城市?
## 查詢city表中,要求只顯示城市名和人口數(shù)量,查詢CHN人口最少的前5個城市?
## 查詢中國的城市數(shù)量?
select count(name) as 中國城市總數(shù) from city where countrycode='CHN';
## 查詢世界的國家數(shù)量?
select count(name) from country;
## 查詢中國的總?cè)丝冢?/span>
select sum(population) from city where countrycode='chn';
## 把多行合并成一行
select group_concat(name) from city where countrycode='chn' and district='hebei';
## 把多列合并成一列
select concat(Name,"#",CountryCode,"#",District) from city where countrycode='chn' and district='hebei' ;
4-11 MySQL 索引
#增加主鍵索引(要求結(jié)果唯一)
alter table t100w add PRIMARY KEY(id);
#創(chuàng)建普通索引
alter table t100w add index num(num);
#創(chuàng)建聯(lián)合索引
alter table t100w add index lianhe(k1,k2);
#查看索引
show index from t100w;
#刪除普通索引
alter table t100w drop index lianhe;
#刪除主鍵索引
alter table t100w drop PRIMARY key;
#創(chuàng)建表的時候,指定索引
create table zhu2(id int(8) primary key AUTO_INCREMENT ,name char(10),passwd char(10));
4-12 MySQL Union
#合并兩個select查詢結(jié)果
CREATE TABLE `c1` (
`ID` int NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `c2` (
`ID` int NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into c1(ID,Name,District,Population) select ID,Name,District,Population
from city where CountryCode='CHN' and District='Hebei';
insert into c2(ID,Name,District,Population) select ID,Name,District,Population
from city where CountryCode='CHN' and District='Henan';
select * from c1 union select * from c2 order by Population;
#sql注入中經(jīng)常會使用的
select * from c1 union select 1,2,3,user();
4-13 mysql存儲引擎
MyISAM: ## 讀性能好,寫的性能差 表級鎖 每張表,三個文件
innodb: ## 讀性能微弱,寫的性能好 行級鎖 每張表,兩個文件
4-14 MySQL找回root密碼
#b適用于mariadb 10.6
1.修改配置文件
vim /etc/my.cnf.d/server.cnf
[mysqld]
skip-grant-tables
2.啟動mariadb
systemctl start mariadb
3.空密碼 登錄數(shù)據(jù)庫并執(zhí)行修改密碼
use mysql;
update user set password=password('123') where user='root' and host='localhost';
flush privileges;
4.刪除配置文件中前面增加的skip-grant-tables
5.重啟啟動mariadb
systemctl restart mariadb
6.使用新密碼驗(yàn)證
mysql -uroot -p123
到了這里,關(guān)于【網(wǎng)絡(luò)安全 --- MySQL數(shù)據(jù)庫】網(wǎng)絡(luò)安全MySQL數(shù)據(jù)庫應(yīng)該掌握的知識,還不收藏開始學(xué)習(xí)。的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!