轉(zhuǎn)自:https://blog.csdn.net/houxuerong/article/details/72420323
數(shù)據(jù)庫(kù)的安裝及初始安全配置
1.安裝及配置
yum install mariadb-server -y ##安裝數(shù)據(jù)庫(kù)服務(wù)軟件
systemctl start mariadb ##開(kāi)啟數(shù)據(jù)庫(kù)
mysql ##進(jìn)入數(shù)據(jù)庫(kù)
netstat -antlpe |grep mysql ##數(shù)據(jù)庫(kù)的網(wǎng)絡(luò)端口
vim /etc/my.cnf ##禁止網(wǎng)絡(luò)登陸
skip-networking=1 ##跳過(guò)網(wǎng)絡(luò)服務(wù)
systemctl restart mariadb ##重啟數(shù)據(jù)庫(kù)
mysql_secure_installation ##數(shù)據(jù)庫(kù)初始安全配置
mysql -uroot -p ##進(jìn)入數(shù)據(jù)庫(kù)片任,-u是登陸用戶牌里,-p該用戶密碼
數(shù)據(jù)庫(kù)的基本sql語(yǔ)句
2.查詢
show databases; ##顯示數(shù)據(jù)庫(kù)
use mysql; ##進(jìn)入mysql數(shù)據(jù)庫(kù)
show tables; ##顯示當(dāng)前庫(kù)中表的名稱
select * from user; ##查詢user表中的所有內(nèi)容
desc user; ##查詢user表的結(jié)構(gòu)
3.數(shù)據(jù)庫(kù)以及表的建立
create database westos; ##創(chuàng)建westos數(shù)據(jù)庫(kù)
create table linux( ##創(chuàng)建表linux
username varchar(15) not null, ##username字段椒丧,字符最大長(zhǎng)度為15,且不為空
password varchar(15) not null
);
insert into linux values ('user1','passwd1'); ##給linux表中添加數(shù)據(jù)
insert into linux values ('user1',password('passwd1')); ##給linux表中添加數(shù)據(jù),且密碼加密
4.更新數(shù)據(jù)庫(kù)信息
update linux set password=password('passwd2') where username='user1'; ##更新名字是user1的密碼
update linux set password=password('123') where (username='user1' or username='user2'); ##更新user1,user2密碼
delete from linux where username='user1'; ##刪除用戶user1
alter table linux add age varchar(4); ##在末尾增加age字段
alter table linux add age varchar(5) after name; ##在name字段后增加age字段
alter table linux drop age; ##刪除linux表中的age字段
5.刪除數(shù)據(jù)庫(kù)
delete from linux where username='user1'; ##從linux表中刪除user1用戶
drop table linux; ##刪除linux表
drop database westos; ##刪除westos數(shù)據(jù)庫(kù)
6.數(shù)據(jù)庫(kù)的備份
mysqldump -u root -pwestos --all-database ##備份所有表中的所有數(shù)據(jù)
mysqldump -u root -pwestos --all-database --no-date ##備份所有表,但不備份數(shù)據(jù)
mysqldump -u root -pwestos westos ##備份westos數(shù)據(jù)庫(kù)
mysqldump -u root -pwestos westos > /mnt/westos.sql ##備份westos庫(kù)砸泛,并將數(shù)據(jù)保存到westos.sql文件中
mysqldump -u root -pwestos westos linux > /mnt/linux.sql ##備份westos數(shù)據(jù)庫(kù)中的linux表
mysql -uroot -pwestos -e "create database westos;" ##還原數(shù)據(jù)時(shí),應(yīng)先建立相應(yīng)的westos庫(kù)
mysql -uroot -pwestos westos < /mnt/westos.sql ##將數(shù)據(jù)導(dǎo)入westos庫(kù)
7.用戶授權(quán)
create user hello@localhost identified by 'hello'; ##建立用戶hello蛆封,此用戶只能通過(guò)本機(jī)登陸
create user hello@'%' identified by 'hello'; ##建立用戶hello唇礁,此用戶可以通過(guò)網(wǎng)絡(luò)登陸
grant insert,update,delete,select on westos.linux to hello@localhost; ##用戶授權(quán)
grant select on westos.* to hello@'%';
show grants for hello@'%'; ##查看用戶授權(quán)
show grants for hello@localhost;
revoke delete on westos.linux from hello@localhost; ##去除用戶授權(quán)權(quán)力
drop user hello@'%'; ##刪除用戶
8.密碼修改
mysqladmin -uroot -pwestos passwd hello ##已知超級(jí)用戶密碼,直接修改密碼
#########當(dāng)超級(jí)用戶忘記密碼####
systemctl stop mariadb ##關(guān)閉mysql
mysqld_safe --skip-grant-tables & ##開(kāi)啟mysql登陸接口并忽略授權(quán)表
mysql ##直接登陸惨篱,不需密碼
update mysql.user set Password=password('hello') where User='root' ##更新超級(jí)用戶密碼
ps aux |grep mysql ##過(guò)濾所有mysql的進(jìn)程盏筐,并結(jié)束這些進(jìn)程
killall -9 mysql
kill -9 mysqlpid
systemctl start mariadb ##重新開(kāi)啟mysql
mysql -uroot -phello ##登陸測(cè)試