天天看點

mariadb使用

mariadb安裝

yum install mariadb-server.x86_64 -y
systemctl start mariadb
systemctl enable mariadb
           

1.使用netstat -antlpe | grep mysql指令檢視mysql程式運作的端口。

mariadb使用

2.編輯/etc/my.cnf配置檔案,添加如下行。

3.運作mysql_secure_installation安全配置向導,設定root使用者密碼,以及進行一系列初始化操作,提升mysql庫的安全性。詳情參考http://www.jb51.net/article/47727.htm

基本sql指令

在實作mariadb的全部安裝後,執行mysql -uroot -p指令,輸入密碼,進入mysql。

mariadb使用

進入mysql後,執行以下指令,展示資料庫及基本表。

//資料庫的查詢、表結構展示
show databases;       ##展示資料庫
use mysql;            ##進入mysql庫
show tables;          ##顯示目前庫中的所有表
desc user;            ##展示user表結構
select * from user;   ##展示user表的全部内容
           

這裡可以看到,安裝mysql後,會有三個自帶資料庫。information_schema是一個資訊資料庫,儲存着關于MySQL伺服器所維護的所有其他資料庫的資訊。如資料庫名,資料庫的表,表欄的資料類型及通路權限等。performance_schema是一個存儲引擎,主要用于收集資料庫伺服器性能參數。mysql是核心資料庫,主要負責存儲資料庫的使用者、權限設定、關鍵字等mysql自己需要使用的控制和管理資訊。

mariadb使用

使用use mysql指令進入mysql庫,然後檢視目前庫中的所有表。

mariadb使用

1.資料庫的建立及基本表查詢

//建立westos資料庫,并進入westos庫,建立student表,包括name、id、class和age字段
create database westos;
use westos;
create table student (
name varchar() not null,
id varchar() not null,
class varchar() not null,
age varchar()
);
           

檢視student表結構

mariadb使用

2.表資料的插入、删除及修改

//向student表插入數行資料
insert into student values ('Cecilia','04131087','1303','20');
insert into student values ('Ilona','04131088','1303','19');
insert into student values ('Briony','04131089','1303','21');
           

列印student表全部内容

mariadb使用
//修改student表資料
alter table student rename linux;                       ##将student表重命名為linux
alter table student add password varchar();           ##向student表添加password字段
alter table student add password varchar() after id;  ##向student表添加password字段于id字段後
update student set class='1304';                        ##将class字段的值統一修改為'1304'
update student set class='1305' where id='04131087';    ##将id為'04131087'的學生的班級修改為'1305'
           

列印student表全部内容,可以看到修改項

mariadb使用
//删除表中資料、表及庫
delete from student where name='Ilona' and id='04131088';   ##删除姓名為'Ilona'且學号是'04131088'學生的資訊
drop table student;         ##删除student表
drop database westos;       ##删除westos資料庫
           

3.資料庫的備份及恢複

mysqldump -uroot -predhat westos > /mnt/westos.sql    ##備份westos資料庫至/mnt/westos.sql下
drop database westos;          ##删除westos庫
mysql -uroot -predhat -e "create database westos;"    ##建立庫
mysql -uroot -predhat westos < /mnt/westos.sql        ##将原有資料恢複
           

4.使用者及通路權限的設定

create user [email protected] identified by 'redhat';     ##添加使用者Lily,并設定其密碼為redhat
grant insert,update,delete,select on westos.* to [email protected];    ##賦予Lily對westos資料庫的所有表insert,update,delete,select權限
show grants for [email protected];   ##檢視使用者Lily的授權情況
revoke insert,update,delete,select on westos.* from [email protected];      ##撤銷使用者Lily的若幹權限
drop user [email protected];   ##删除使用者Lily
           
mariadb使用

5.資料庫密碼恢複

當我們忘記資料庫密碼時,可以通過以下方法恢複。

systemctl stop mariadb.service      ##關閉mariadb服務
mysqld_safe --skip-grant-tables &   ##進入安全模式
mysql -uroot                        ##免密碼進入mysql
use mysql                           ##進入mysql資料庫
update user set Password=password('redhat') where User='root';             ##重置密碼
           

随後退出mysql,利用ps aux | grep mysql 指令檢視程序号,然後使用kill指令殺死該程序,最後重新開機mariadb服務。

mariadb使用

繼續閱讀