【Linux】數(shù)據(jù)庫管理

數(shù)據(jù)庫(Database)是按照數(shù)據(jù)結構來組織、存儲和管理數(shù)據(jù)的,是建立在計算機存儲設備上的倉庫。?
簡單來說是本身可視為電子化的文件柜——存儲電子文件的處所,用戶可以對文件中的數(shù)據(jù)進行新增、截取、更新、刪除等操作。?
一、安裝部署http://www.daiqiyang.com?
#系統(tǒng)默認已經(jīng)安裝該數(shù)據(jù)庫,如果沒有安裝,使用以下命令進行安裝?
[root@mail ~]# yum install -y mariadb#啟動數(shù)據(jù)庫服務?
[root@mail ~]# systemctl restart mariadb#初始化數(shù)據(jù)庫?
[root@mail ~]# mysql_secure_installation ????????????????????????????????????????這里第一次直接回車:為數(shù)據(jù)庫的root設置密碼: ?后面做一些初始化設定,一般都選Y就可以了。#在防火墻添加永久允許策略?
[root@mail ~]# firewall-cmd --permanent --add-service=mysql#重新加載防火墻配置?
[root@mail ~]# firewall-cmd --reload二、登陸使用?
#數(shù)據(jù)庫系統(tǒng)登陸?
[root@mail ~]# mysql -uroot -predhat ????????????????????//注意這里的命令與參數(shù)之間沒有空格[root@mail ~]# mysql -uroot -p ???????????????????????????????//這樣登錄可以隱藏密碼 ?[root@mail ~]# mysql -u root -h localhost -p [DATABASE NAME]-u:連接mysql服務器的用戶名;?
-h:mysql服務器的ip地址或主機名;?
-p:連接mysql服務器的密碼;?
#查看系統(tǒng)有多少數(shù)據(jù)庫?
MariaDB [(none)]> show databases; ?????????????????????????????????????????//在數(shù)據(jù)庫中的命令都以;結尾#退出數(shù)據(jù)庫系統(tǒng)?
MariaDB [(none)]> quitMariaDB [(none)]> exit#創(chuàng)建一個數(shù)據(jù)庫?
MariaDB [(none)]> create database luntan;#切換到某個數(shù)據(jù)庫下?
MariaDB [mysql]> use mysql;#查看數(shù)據(jù)庫的表?
MariaDB [mysql]> show tables;#查看數(shù)據(jù)表的表結構?
MariaDB [mysql]> desc user;#查詢user表中的某些數(shù)據(jù)?
MariaDB [mysql]> select host,user,password from user;#創(chuàng)建一張表?
MariaDB [mysql]> create table person ( ???-> number int(11), ???-> name varchar(255), ???-> birthday date);#查詢創(chuàng)建好的表的表結構?
MariaDB [mysql]> desc person;#插入幾條數(shù)據(jù)?
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);#查詢表的內(nèi)容?
MariaDB [mysql]> select * from person;#刪除表的內(nèi)容?
MariaDB [mysql]> delete from person where name="haha";MariaDB [mysql]> delete from person where number=3;#更新表中的數(shù)據(jù)?
MariaDB [mysql]> update person set name="haha" ?where name="xixi";MariaDB [mysql]> update person set number=1 where birthday=20191226;三、用戶的管理和訪問權限的控制?
創(chuàng)建數(shù)據(jù)庫登陸用戶?
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();查看當前用戶的數(shù)據(jù)庫:?
MariaDB [(none)]> select database();使用小明用戶登錄數(shù)據(jù)庫:?
[root@localhost ~]# mysql -u xiaoming -p#查看可以訪問的數(shù)據(jù)庫?
MariaDB [(none)]> show databases;#以root用戶登錄給xiaoming用戶一張表的權限?
MariaDB [(none)]> grant select,update,insert,delete on mysql.person to xiaoming@localhost;?退出數(shù)據(jù)庫系統(tǒng),并使用xiaoming用戶重新登陸?
[root@localhost ~]# mysql -u xiaoming -pMariaDB [(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=1MariaDB [mysql]> delete from person where number=1;#使用root用戶登錄,改變xiaoming用戶的權限?
MariaDB [(none)]> revoke delete on mysql.person from xiaoming@localhost;#使用select語句進行刪除表數(shù)據(jù),確認權限已被禁用?
MariaDB [mysql]> delete from person where number=3 ;四、備份和還原?
備份整個數(shù)據(jù)庫的所有表?
[root@mail ~]# mysqldump -u root -p mysql > /mysql_backup_20160510.dump ????//做一個備份文件,位置可以選擇#使用root用戶登錄數(shù)據(jù)庫,刪除person表?
MariaDB [mysql]> drop table person;#退出系統(tǒng),進行還原操作?
[root@mail ~]# mysql -u root -p mysql < /mysql_backup_20160510.dump或者使用source命令讀入表信息。?
#登陸數(shù)據(jù)庫系統(tǒng)?
[root@mail ~]# mysql -u root -p#查看person表?
MariaDB [mysql]> select * from person;————————————————?
了解更多網(wǎng)絡知識關注:http://www.vecloud.com/