【Linux】數據庫管理

數據庫(Database)是按照數據結構來組織、存儲和管理數據的,是建立在計算機存儲設備上的倉庫。

簡單來說是本身可視爲電子化的文件櫃——存儲電子文件的處所,用戶可以對文件中的數據進行新增、截取、更新、刪除等操作。
一、安裝部署http://www.daiqiyang.com

#系統默認已經安裝該數據庫,如果沒有安裝,使用以下命令進行安裝

[root@mail ~]# yum install -y mariadb

#啓動數據庫服務

[root@mail ~]# systemctl restart mariadb

#初始化數據庫

[root@mail ~]# mysql_secure_installation                                         

這裏第一次直接回車:

爲數據庫的root設置密碼: 

 後面做一些初始化設定,一般都選Y就可以了。

#在防火牆添加永久允許策略

[root@mail ~]# firewall-cmd --permanent --add-service=mysql

#重新加載防火牆配置

[root@mail ~]# firewall-cmd --reload

二、登陸使用

#數據庫系統登陸

[root@mail ~]# mysql -uroot -predhat                     //注意這裏的命令與參數之間沒有空格

[root@mail ~]# mysql -uroot -p                                //這樣登錄可以隱藏密碼  

[root@mail ~]# mysql -u root -h localhost -p [DATABASE NAME]

-u:連接mysql服務器的用戶名;

-h:mysql服務器的ip地址或主機名;

-p:連接mysql服務器的密碼;

#查看系統有多少數據庫

MariaDB [(none)]> show databases;                                          //在數據庫中的命令都以;結尾

#退出數據庫系統

MariaDB [(none)]> quit

MariaDB [(none)]> exit

#創建一個數據庫

MariaDB [(none)]> create database luntan;

#切換到某個數據庫下

MariaDB [mysql]> use mysql;

#查看數據庫的表

MariaDB [mysql]> show tables;

#查看數據表的表結構

MariaDB [mysql]> desc user;

#查詢user表中的某些數據

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

#創建一張表

MariaDB [mysql]> create table person (

    -> number int(11),

    -> name varchar(255),

    -> birthday date);

#查詢創建好的表的表結構

MariaDB [mysql]> desc person;

#插入幾條數據

MariaDB [mysql]> insert into person (number,name,birthday) values (1,"haha",20191225);

MariaDB [mysql]> insert into person (number,name,birthday) values (2,"xixi",20191226);

MariaDB [mysql]> insert into person (number,name,birthday) values (3,"hehe",20191227);

#查詢表的內容

MariaDB [mysql]> select * from person;

#刪除表的內容

MariaDB [mysql]> delete from person where name="haha";

MariaDB [mysql]> delete from person where number=3;

#更新表中的數據

MariaDB [mysql]> update person set name="haha"  where name="xixi";

MariaDB [mysql]> update person set number=1 where birthday=20191226;

三、用戶的管理和訪問權限的控制

創建數據庫登陸用戶

MariaDB [mysql]> create user xiaoming@localhost identified by 'redhat';

MariaDB [mysql]> create user xiaohong@localhost identified by "redhat";

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

查看當前使用用戶:

MariaDB [(none)]> select user();

查看當前用戶的數據庫:

MariaDB [(none)]> select database();

使用小明用戶登錄數據庫:

[root@localhost ~]# mysql -u xiaoming -p

#查看可以訪問的數據庫

MariaDB [(none)]> show databases;

#以root用戶登錄給xiaoming用戶一張表的權限

MariaDB [(none)]> grant select,update,insert,delete on mysql.person to xiaoming@localhost; 

退出數據庫系統,並使用xiaoming用戶重新登陸

[root@localhost ~]# mysql -u xiaoming -p

MariaDB [(none)]> use mysql;

#測試各種權限

MariaDB [mysql]> select * from person;

MariaDB [mysql]> insert person (number,name,birthday) value (3,"xiaoming",20181228);

MariaDB [mysql]> update person set name="xixi" where number=1

MariaDB [mysql]> delete from person where number=1;

#使用root用戶登錄,改變xiaoming用戶的權限

MariaDB [(none)]> revoke delete on mysql.person from xiaoming@localhost;

#使用select語句進行刪除表數據,確認權限已被禁用

MariaDB [mysql]> delete from person where number=3 ;

四、備份和還原

備份整個數據庫的所有表

[root@mail ~]# mysqldump -u root -p mysql > /mysql_backup_20160510.dump     //做一個備份文件,位置可以選擇

#使用root用戶登錄數據庫,刪除person表

MariaDB [mysql]> drop table person;

#退出系統,進行還原操作

[root@mail ~]# mysql -u root -p mysql < /mysql_backup_20160510.dump

或者使用source命令讀入表信息。

#登陸數據庫系統

[root@mail ~]# mysql -u root -p

#查看person表

MariaDB [mysql]> select * from person;

————————————————
版權聲明:本文爲CSDN博主「悲觀的樂觀主義者」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_43997530/article/details/103634828

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章