以下所有操作都是在Red-hat 7.0上
前言:在這裏先簡單的介紹一下mariadb,其實本質上mariadb就是mysql,只不過甲骨文公司將其賣給紅帽公司了,紅帽公司就叫其爲mairadb。不管mariadb還是mysql都是數據庫。
|###################數據庫的安裝########################|
1.yum install mariadb-server -y ##安裝mariadb數據庫
2.systemctl start mariadb ##開啓數據庫服務
3.測試:mysql ##進入數據庫
4.初始化數據庫
|###################數據庫sql語句基本操作#################|
1.登陸
mysql -uroot -pwestos ##以root登陸數據庫,密碼爲westos
2.查詢
show databases; ##顯示數據庫
show tables ##顯示錶的信息
use mysql ##進入mysql庫(命令後面不帶封號)
select * from user;##查詢user表中的所有信息(*代表全部)
desc user; ##查詢user中的結構(顯示所有字段的名稱)
|###################數據庫及表的建立#####################|
1.創建庫
create database westos; ##創建名爲westos的庫
2.創建表
create table linux( ##創建linux表,並且linux表含有兩個字段,字符長度最大爲15
username varchar(15) not null,
password varchar(15) not null);
insert into linux values (‘user1’,’password1’); ##插入數據(不加密類型)
insert into linux values (‘user2’,password{‘123’}); ##插入數據(加密類型)
|###################數據庫的更新#####################|
1.更新用戶密碼
update linux set password=password(‘passwd2’) where username=user1; ##更新user1的密碼
2.刪除用戶密碼
delete from linux where username=user1; ##刪除user1的信息
3.添加字段到表格上
alter table linux add age varchar(4); ##添加字段到linux表上
alter table linux add age varchar(4) after name; ##添加字段到linux表上,並在name信息之後
4.刪除字段
alter table linux drop age ##刪除age的字段
|###################刪除數據庫#####################|
delete from linux where username=’user1’; ##刪除linux表中的user1的信息
drop table linux; ##刪除linux這個表
drop database westos; ##刪除westos庫
|###################數據庫的備份#####################|
1.備份文件的分類
mysqldump -uroot -pwestos –all-database ##備份所以表中的文件
mysqldump -uroot -pwestos –all-database –no-data ##備份所以表,但不備份數據
mysqldump -uroot -pwestos westos ##備份westos庫
mysqldump -uroot -pwestos westos > /mnt/linux.sql##備份westos庫,並將數據存到/mnt目錄下
2.文件的還原
mysql -uroot -pwestos -e “create database westos;””*要想恢復westos中數據,先創建一個westos的庫
mysql -uroot -pwestos westos < /mnt/index.sql##備份westos庫
|###################數據庫的授權#####################|
1.創建用戶
create user lee@localhost identified by ‘lee’; ##創建本地用戶(ps:lee是通過密碼)
create user lee@’%’ identified by ‘lee’; ##創建網絡用戶
2.用戶授權
grant insert,update,delete,select on westos.test to lee@localhost ##對本地用戶進行授權
**grant insert on westos.test to lee@’%’ ##對網絡用戶進行授權;
3.顯示授權
show grants for lee@’%’ ##顯示網絡用戶的授權
show grants for lee@localhost ##顯示普通用戶的授權
4.刪除授權
revoke delete on westos.test from lee@local;
|#############數據庫中忘記root密碼的補救方法################|
1.製作實驗環境
**mysqladmin -uroot -pwestos password lee ##修改root密碼爲lee(模擬改掉密碼。忘記的情景)
##當超級用戶密碼忘記
2.進行補救
**systemctl stop mariadb ##先關掉數據庫服務
mysql_safe –skip-grant-tables & ##開啓mysql接口,並忽略授權表
mysql ##直接不用密碼就可以登陸
**update mysql.user set Password=password(’123’) where User=root; ##修改root密碼爲123
3.退出單用戶模式
ps aux | grep mysql
kill -9 mysqlpid ##強行關閉mysql的進程
4.重新登入
mysql -uroot -p123
|#############數據庫的網頁管理工具################|
1.安裝必要的服務
yum install httpd php php-mysql -y
systemctl start httpd
systemctl enable httpd
systemctl stop firewalld
systemctl disable firewalld
2.下載必要的工具包
phpMyAdmin-3.4.0-all-languages.tar.bz2
tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html ##解壓此壓縮包到此目錄
mv phpMyAdmin-3.4.0-all-languages.tar.bz2/ mysqladmin ##更改壓縮包名字(不是必須的,只是後來用起來方便)
3.修改配置文件
cd mysqladmin
cp -p config.sample.inc.php config.inc.php
vim config.inc.php
17 $cfg[‘blowfish_secret’]=’mysql’; ##添加此mysql在17行。
4.測試:
訪問 htttp://172.25.254.10/mysqladmin ##你的主機是什麼ip,就填什麼ip