第11章 MariaDB數(shù)據(jù)庫2020-06-17

?MariaDB數(shù)據(jù)庫

yum groupinstall MariaDB? ? 安裝MariaDB?

systemctl enable mariadb.service? ? ?開機自啟

systemctl restart mariadb.service? ? ?啟動一下

netstat -antulp | grep mysql? ? 看一下端口

firewall-cmd --permanent --add-service=mysql? ? ?防火墻設(shè)置一下

firewall-cmd --reload? ? ?重新讀配置

vim /etc/my.cnf????MariaDB的配置文件?官方默認

?MariaDB整了四套配置文件

ll /usr/share/mysql/????四套配置文件在這里


根據(jù)數(shù)據(jù)規(guī)模選擇配置文件

ll /var/lib/mysql/? ? 存放數(shù)據(jù)文件徐钠,一個文件夾就是一個數(shù)據(jù)庫

剛裝完默認沒有密碼,直接mysql就進入MariaDB界面

show databases;? ? 查看有幾個數(shù)據(jù)庫


use mysql? ? 使用mysql數(shù)據(jù)庫

show tables;? ? 查看有幾張表

desc user;? ? 查看user表的結(jié)構(gòu)

select database();? ? 查看當(dāng)前所在數(shù)據(jù)庫

select user();? ? ?查看當(dāng)前身份

show variables like 'innodb%';? ? 查看環(huán)境變量

ctrl + d 退出MariaDB

mysqladmin -u root password '123456'? ? 給root設(shè)置登陸MariaDB的密碼

mysql -u root -p? ? 設(shè)置密碼后就這樣子登陸

MariaDB [(none)]> status? ? 查看數(shù)據(jù)庫信息

出去locale 查看當(dāng)前語言環(huán)境

MariaDB [(none)]> system ls -l /? ? 調(diào)用系統(tǒng)命令

MariaDB [(none)]> source /scott.sql? ? 調(diào)用腳本建庫https://my.oschina.net/iamhere/blog/357809?p={{currentPage-1}}

MariaDB [scott]> show create database scott;? ? 查看數(shù)據(jù)庫語言環(huán)境

MariaDB [scott]>show create table emp;? ? 查看表的語言環(huán)境

alter database scott charset utf8;? ? 修改數(shù)據(jù)庫語言環(huán)境

alter table emp charset utf8;? ? 修改表的語言環(huán)境

這樣子改太麻煩 vim /etc/my.cnf直接去配置文件定義


systemctl restart mariadb.service? ? 重啟服務(wù)就語言環(huán)境就改過來了

但是改了配置文件以后是以后建立的才有效博个,之前的不行

drop database scott;? ? 刪除scott數(shù)據(jù)庫,然后再建立一次就好了哈哈

/////////////////////////

select * from xxx;? ?顯示全表

select ename,sal? from xxx桨武;? ? 就顯示?xxx表中的ename,sal

select ename,sal+10000 from xxx景东;? ? 給每條sal加10000

select ename,sal+10000 as newsal from xxx闻察;? ? ?給,sal起個別名

select ename,sal+10000? newsal from xxx 邻梆;? ? as不寫也可以

select ename,(sal+200)*3 from xxx 守伸;? ? 支持括號

select ename,sal+ifnull(comm绎秒,0)??as zhonshouru from xxx浦妄;? ? ?如果ifnull()里面是空值就改成0(null空值加減乘除會變null)

select distinct deptno from tmp;? ? ? ? 合并重復(fù)行

select * from xxx where?deptno=30 ;? ?加條件

select * from xxx where?sal>3000 剂娄;? ?加判斷

select * from xxx where?ename like ‘%TT%’蠢涝;? ?模糊查找(字符和日期要加單引號)

select * from xxx where?ename like ‘_TT%’;? ? 下劃線明確前面只有一個字符阅懦,一個_等于一個字符

select * from emp where deptno=30 and sal>2000;? ? 與? ?支持與或非

select * from emp where deptno=30 or sal>2000;???或??支持與或非

select * from emp where not sal>2000;????非????支持與或非

select * from emp order by sal;? ? 排序和二,默認升序,從小到大

select * from emp order by sal desc;? ? 降序

select * from emp order by deptno,sal;? ? 先按deptno排序耳胎,再按sal排序

select concat(ename,sal,comm) from emp;? ? 連接ename,sal,comm結(jié)果集

select concat(ename,sal,ifnull(comm,0)) from emp;? ? 函數(shù)嵌套

select concat(ename,'\'s sal is ',sal) from emp;? ? 連接字符串惯吕,引號需要轉(zhuǎn)義符 \

select count(*) from emp;? ? 統(tǒng)計幾行

select sum(sal),min(sal),max(sal),avg(sal) from emp;? ?總和, 最小怕午,最大废登,平均值,多行函數(shù)

select deptno,sum(sal),min(sal),max(sal),avg(sal) from emp group by deptno;? ? 分類group by?


select ename,dname from emp,dept where emp.deptno=dept.deptno;? ? 多表查詢

select y.ename,j.ename from emp y,emp j where y.mgr=j.empno;? ? 同一張表多表查詢郁惜,起了別名y和j

select ename,sal,grade from emp,salgrade where sal between losal and hisal;?多表查詢堡距,sal在losal和?hisal之間對應(yīng)列出

select ename,dname,sal,grade from emp,dept,salgrade where emp.deptno=dept.deptno and emp.sal between losal and hisal;三表查詢

create database qindatabase;? ? 創(chuàng)建數(shù)據(jù)庫

create table qintable(id int(2),name varchar(10),mail varchar(20));? ? 建表


查看表結(jié)構(gòu)

insert into qintable(id,name,mail) values(1,'qin1','qin@qin.com');? ? 插入數(shù)據(jù)

insert into qintable values(2,'qin2','qin2@qin.com');? ? 每一列都插前面可以不寫

insert into qintable(id,name) values(3,'qin3');? ? 插入個別列

insert into qintable(id,mail) values(4,'qin4@qin.com');????? 插入個別列

insert into qintable values(5,'qin5','qin5@qin.com'),(6,'qin6','qin6@qin.com'),(7,'qin7','qin7@qin.com');? ? 一次插入多行數(shù)據(jù)

delete from qintable where id=4;? ? 刪除第4行

update qintable set name='bing' where id=2;? ? 把id等2的name改成bing

create table qin1 like qintable;? ? 采用qintable的表結(jié)構(gòu)建立qin1表

insert into qin1 select * from qintable;? ? 把?qintable表的數(shù)據(jù)插入到qin1表

create table qin2 as select * from qintable;? ? 直接創(chuàng)建一個結(jié)構(gòu)數(shù)據(jù)跟qintable都一樣的表qin2

create table qin3 as select * from qintable where 0=1;? ??采用qintable的表結(jié)構(gòu)建立qin1表,where 0=1不成立所以沒數(shù)據(jù)

alter table qintable add newlist varchar(20);? ? 給表qintable添加一列叫newlist

alter table qintable drop newlist;? ? 刪除newlist這列

alter table qintable add firstlist varchar(20) first;? ? 在第一列(first)添加一列叫firstlist?


alter table qintable add afterid varchar(20) after id;? ? 在id列后面(after id)添加一列afterid

delete from qin1;? ? 刪除qin1表的所有內(nèi)容

drop table qin1;? ? 直接刪除qin1表

truncate qin3;????刪除qin3表的所有內(nèi)容? 兆蕉,真刪羽戒,磁盤上也沒了

drop database qindatabase;? ? 刪除數(shù)據(jù)庫

//////////////外部數(shù)據(jù)導(dǎo)入

vim /qin.txt????建數(shù)據(jù)

MariaDB [(none)]> create database qindatabase;? ? 從新建庫

MariaDB [(none)]> use qindatabase;? ? 進入

create table qintable(id int(4),name varchar(10),email varchar(20));? ? 建表

load data infile '/qin.txt' into table qintable fields terminated by ',' lines terminated by '\n';? ? 將qin.txt的數(shù)據(jù)導(dǎo)入表qintable,后面的fields terminated是數(shù)據(jù)的分隔符是逗號虎韵,lines terminated 結(jié)束符是回車\n

updata qintable set name='bing' where id=2;? ? 還能修改內(nèi)容易稠,但是外面qin.txt的數(shù)據(jù)不變

select * from qintable into outfile '/var/lib/mysql/qindatabase/qintable.txt' fields terminated by ',' lines terminated by '\n';? ? 將qintable表的數(shù)據(jù)導(dǎo)出到'/var/lib/mysql/qindatabase/下的qintable.txt? ??

導(dǎo)出位置注意權(quán)限問題,還要注意SElinux上下文

?mkdir /backup

semanage fcontext -a -t mysqld_db_t '/backup{.*}?'? ? 修改上下文

restorecon -RFv /backup/????還得還原一下才生效

////////////////備份還原

MariaDB [(none)]>use scott;? ? 進入scott數(shù)據(jù)庫

退出MariaDB 操作

mysqldump -u root -p scott > /scott.dump? ? 使用root身份將數(shù)據(jù)庫scott備份到/scott.dump? ?包蓝,需要輸入root密碼

mysqldump -u root -p scott emp > /scott.emp.dump????將數(shù)據(jù)庫scott的emp表備份到/scott.emp.dump

mysqldump -u root -p scott dept salgrade > /scott.dept+salgrade.dump? ? ?同時備份dept缩多,salgrade表,多表備份

進入MariaDB 操作

MariaDB [(none)]> drop database scott;? ? 刪了scott數(shù)據(jù)庫

MariaDB [(none)]> create database qin;? ? 建立空數(shù)據(jù)庫qin

退出MariaDB 操作

mysql -u root -p qin < /scott.dump? ? ?使用root身份還原數(shù)據(jù)庫qin养晋,數(shù)據(jù)來源/scott.dump

進入MariaDB 查看發(fā)現(xiàn)數(shù)據(jù)回來了

////////////////單表還原

MariaDB [qin]> drop database qin;????刪了qin數(shù)據(jù)庫

MariaDB [(none)]> create database qin;?????建立空數(shù)據(jù)庫qin

退出MariaDB 操作

mysql -u root -p qin < /scott.emp.dump? ? ?單表導(dǎo)入

mysql -u root -p qin < /scott.dept+salgrade.dump

進入查看數(shù)據(jù)回來了

////////////////////MariaDB用戶權(quán)限

user mysql? ? 進入mysql數(shù)據(jù)庫

show tables;? ? 其中user表存放MariaDB的用戶


MariaDB [mysql]> create user qin@'%' identified by '123456';? ? ?創(chuàng)建用戶qin衬吆,登陸位置所有ip都可以連接過來,密碼123456绳泉,

去登陸發(fā)現(xiàn)不行逊抡,因為它需要所有網(wǎng)絡(luò)登陸才能進入

去2號機安裝MariaDB

yum install mariadb

mysql -h 192.168.100.1 -u qin -p? ? 通過遠程登陸192.168.100.1使用qin賬戶就可以登陸了

MariaDB [(none)]> create user qin@'localhost' identified by '123456';? ? 再回主機創(chuàng)建一個localhost用戶,這個用戶就可以直接登陸


MariaDB [mysql]> drop user qin@'%';? ? 把這qin百分百用戶給刪了

MariaDB [mysql]>?create user qin@'192.168.100.2' identified by '123456';? ? 創(chuàng)建了一個可以通過192.168.100.2登陸的用戶

MariaDB [mysql]> create user qin@'192.168.100.0/255.255.255.0' identified by '123456';? ? 創(chuàng)建網(wǎng)段用戶

MariaDB [(none)]> show privileges;? ? 查看權(quán)限

MariaDB [(none)]>grant select on scott.* to qin@'localhost';? ? 將對scott數(shù)據(jù)庫下的所有表的查詢權(quán)限(select)授權(quán)給?qin@'localhost'用戶

MariaDB [(none)]> flush privileges;? ? 如果權(quán)限沒有生效,刷新權(quán)限表

再次使用qin登陸MariaDB 就可以看到scott數(shù)據(jù)庫了零酪,查詢表冒嫡,但是不能刪改

MariaDB [(none)]> show grants;? ? 查看自己的權(quán)限

MariaDB [(none)]> show grants forqqin@'localhost';? ? 查看qin@'localhost'的權(quán)限

MariaDB [(none)]> revoke select on scott.* from qin@'localhost';? ? 收回qin@'localhost對?scott.*的select權(quán)限

MariaDB [(none)]> grant all on *.* to qinbing@'localhost' identified by '123456'? ? 創(chuàng)建用戶qinbing@'localhost'并且授權(quán)全部(all)權(quán)限對所有數(shù)據(jù)庫(?*.* );

MariaDB [(none)]> grant all on *.* to qinbing@'%' identified by '123456';? ? 增加遠程登陸

退出MariaDB

mysqladmin -u root -p password 'redhat';? ? 修改登陸密碼為redhat

MariaDB [mysql]> update user set password='123456' where user='root' and host='localhost';? ? 在數(shù)據(jù)庫里改密碼四苇,將root(localhost)的密碼改為123456? ? 但是發(fā)現(xiàn)密碼是明文的

update user set password=password('123456') where user='root' and host='localhost';? ? 增加password('123456')變密文

MariaDB [mysql]> set password=password('redhat');? ? 直接更改當(dāng)前用戶密碼為redhat

MariaDB [mysql]> set password for qin@'localhost'=password('redhat');? ? 對別的用戶改密碼

///////////////破解MariaDB密碼

systemctl stop mariadb.service? ? ?先關(guān)了MariaDB

vim /etc/my.cnf? ? 修改配置文件

跳過權(quán)限表

systemctl restart mariadb.service? ? ?啟動數(shù)據(jù)庫

mysql -u root -p? ? 直接登陸孝凌,不用輸入密碼回車就進入數(shù)據(jù)庫了

MariaDB [(none)]> use mysql? ? 進入mysql?

MariaDB [mysql]> select host,user,password from user;

MariaDB [mysql]> update user set password=password('123456') where user='root';? ? 把密碼改了就好了

然后退出去把配置文件里的skip-grant-tables給刪了

systemctl restart mariadb.service? ? ?重啟數(shù)據(jù)庫

///////////////破解MariaDB密碼 2

systemctl stop mariadb.service ????先關(guān)了MariaDB

mysqld_safe --skip-grant-tables? ? 輸入這個跳過權(quán)限,然后打開另一個窗口

直接mysql就進入數(shù)據(jù)庫了月腋,然后就是同上操作改掉密碼就好了

systemctl restart mariadb.service? ? ?最后重啟數(shù)據(jù)庫

////////////////////

mysql_secure_installation????提高安全性安全安裝(用于生產(chǎn)環(huán)境設(shè)置)

systemctl restart mariadb.service? ? ?重啟數(shù)據(jù)庫

vim /etc/my.cnf? ? 修改配置文件

禁止網(wǎng)絡(luò)用戶登陸


沒禁止網(wǎng)絡(luò)用戶之前

重啟機器蟀架,systemctl reboot

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末瓣赂,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子片拍,更是在濱河造成了極大的恐慌煌集,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捌省,死亡現(xiàn)場離奇詭異苫纤,居然都是意外死亡,警方通過查閱死者的電腦和手機纲缓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門卷拘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人祝高,你說我怎么就攤上這事恭金。” “怎么了褂策?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵横腿,是天一觀的道長。 經(jīng)常有香客問我斤寂,道長耿焊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任遍搞,我火速辦了婚禮罗侯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘溪猿。我一直安慰自己钩杰,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布诊县。 她就那樣靜靜地躺著讲弄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪依痊。 梳的紋絲不亂的頭發(fā)上避除,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機與錄音胸嘁,去河邊找鬼瓶摆。 笑死,一個胖子當(dāng)著我的面吹牛性宏,可吹牛的內(nèi)容都是我干的群井。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼毫胜,長吁一口氣:“原來是場噩夢啊……” “哼书斜!你這毒婦竟也來了诬辈?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤菩佑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后凝化,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體稍坯,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年搓劫,在試婚紗的時候發(fā)現(xiàn)自己被綠了瞧哟。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡枪向,死狀恐怖勤揩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情秘蛔,我是刑警寧澤陨亡,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站深员,受9級特大地震影響负蠕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜倦畅,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一遮糖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧叠赐,春花似錦欲账、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至罢洲,卻和暖如春俄删,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奏路。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工畴椰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鸽粉。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓斜脂,卻偏偏與公主長得像,于是被迫代替她去往敵國和親触机。 傳聞我的和親對象是個殘疾皇子帚戳,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354