Linuxunit8-mariadb數據庫

第八單元-數據庫mariadb


一、安裝登陸


1.安裝,開啓服務
yum install mariadb-server -y
systemctl start mariadb

2.屏蔽mysql的入口
1>監聽端口屏蔽

netstat -antple | grep mysql ##檢測監聽端口,端口顯示爲80
示圖:監聽端口

vim /etc/my.cnf  ##編輯文件設置檢測時略過mysql,監聽端口就不會查到mysql的端口
示圖:文件編輯內容

syetmctl restart mariadb
netstat -antple | grep mysql  ##修改後檢查可以觀察到沒有mysql的端口

2>設置用戶密碼登陸
mysql  ##命令登陸,還可以無用戶密碼登陸
示圖:無用戶密碼登陸

mysql_secure_installation  ##設置mysql登陸
#####設置內容########
Enter current password for root (enter for none):  ##當前的密碼設置是給root設置的->回車表示確定    
Set root password? [Y/n] y  ##建立密碼
New password:
Re-enter new password:
Password updated successfully!
Remove anonymous users? [Y/n] y  ##刪除其他用戶登陸
 ... Success!
Disallow root login remotely? [Y/n] y  ##
 ... Success!
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reload privilege tables now? [Y/n] y
 ... Success!

mysql  

示圖:設置後直接登陸失敗

mysql -uroot -pwestos
示圖:使用用戶密碼登陸

二、mysql使用命令


1.基本使用
show databases;  ##顯示databases中所有的庫,任何命令必須以“;”結尾

use mysql;  ##進入mysql數據庫
show tables;  ##顯示當前數據庫的所有表
示圖:顯示當前庫的所有表

select Host,User from user;  ##只顯示user表中Host和User字段的數據(字段指列,*指所有)
示圖:顯示指定字段表的內容

desc user;  ##顯示當前數據庫的所有字段,查看此庫結構

2.數據庫及表的建立
create database westos ##創建一個新的庫名字是westos
create table linux(   ##在westos庫中建立一個表,字段包括username和password
username varchar(15) not null,  ##指定username的字符長度不可超過15個,字符類型爲varchar,並且不許爲空
password varchar(15) not null  ##類似上
);
insert into linux values ('user1','123');  ##插入一個名叫user1,密碼爲123的數據(如果需要加米密碼,數據的輸入格式爲:('user2',password('123'));)

示圖:當前所有庫信息


示圖:新建庫westos中所有表信息


示圖:新建表內容

3.數據庫及表的更新
update linux set password=password('456') where username=user1;  ##在linux表中將user1的密碼改爲456
delete from linux where username=user1;  ##刪除linux表中user1的數據
alter table linux add age varchart(4);  ##在linux表中添加新的字段,爲age,字符長度不可超過4
alter table linux add age varchart(5) after name;  ##在linux表中添加新的字段,位置在name字段後,字符大小不超過5

alter table linux drop age;  ##在linux表中刪除age字段

drop table linux;  ##刪除表

drop database westos;  ##刪除庫

示圖:刪除表


示圖:刪除庫


示圖:刪除字段


刪除:表內信息


示圖:linux當前信息


三、數據庫及表的備份與和恢復


mysqldump -uroot -pwestos --all-data   ##備份所有表中的數據,若無指定備份位置,將會把備份的內容備份到輸出端口
mysqldump -uroot -pwestos --all-data --no-data  ##備份所有表不包括數據
mysqldump -uroot -pwestos westos   ##備份庫westos
mysqldump -uroot -pwetsos westos > /mnt/westos.sql  ##將westos庫備份到/mnt下wetsos.sql文件中
示圖:備份庫數據

mysqldump -uroot -pwestos westos linux > /mnt/linux.sql  ##將westos中的表linux備份到/mnt下的linux.sql文件中
示圖:備份表數據

mysqldump -uroot -pwestos -e "create database westos;"  ##-e可以在終端進行mysql操作
mysqldump -uroot -pwestos westos < /mnt/westos.sql  ##如果westos庫數據損壞,可以用此命令將/mnt下的westos庫數據恢復
示圖:恢復庫數據檢測

四、用戶管理


create user redhat@localhost identified by 'redhat';   ##建立用戶redhat,此用戶只可以本地登陸,密碼是‘redhat’
示圖:用戶建立

create user redhat@'%' identified by 'redhat';  ##建立用戶redhat,此用戶可以通過網絡登陸
示圖:用戶建立

drop user harry@'%';  ##刪除可以用網絡登陸的用戶harry

示圖:刪除用戶


grant insert,update,delete,select on westos.linux to redhat@localhost;  ##給只許本地登陸的redhat用戶授予insert,update,delete,select權力
show grants for redhat@'%';  ##查看用戶被授予的權力
示圖:用戶授權

revoke delete on westos.linux from redhat@localhost;  ##移除用戶某項權力
示圖:用戶移權

mysql -uredhat -predhat -h localhost  ##本地登陸用戶
示圖:本地登陸用戶

mysql -uredhat -predhat -h 172.25.254.198  ##通過網絡登陸用戶,前提必須將/etc/my.cnf文件中的skip-networkig=1變爲=0

示圖:/etc/my.cnf文件查看


示圖:通過網絡登陸用戶

五、數據庫密碼修改


mysqladmin -uroot -pwestos password linux  ##終端處修改用戶密碼

##當用戶密碼忘記時

mysqld_safe --skip-grant-tables &  ##開啓mysql登陸接口並忽略授權表
mysql                              ##不需要密碼直接登陸
示圖:忽略授權表後登陸

update mysql.user set Password=password('westos') where User='root';  ##修改root密碼
示圖:修改密碼

ps aux |grep mysql  ##過濾mysql的所有進程並用kill -9結束這些進程
kill -9 mysqlpid
示圖:過濾並結束mysql相關的進程

systemctl start mariadb  ##重新開啓服務
mysql -uroot -predhat  ##登陸測試

示圖:測試結果



發佈了68 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章