溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux中mysql數(shù)據(jù)庫(kù)的管理

發(fā)布時(shí)間:2020-06-12 12:53:18 來(lái)源:億速云 閱讀:153 作者:Leah 欄目:系統(tǒng)運(yùn)維

這篇文章給大家分享的是有關(guān)Linux中mysql數(shù)據(jù)庫(kù)的管理,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),話不多說(shuō),一起往下看吧。

數(shù)據(jù)庫(kù)(Database)是按照數(shù)據(jù)結(jié)構(gòu)來(lái)組織、存儲(chǔ)和管理數(shù)據(jù)的,是建立在計(jì)算機(jī)存儲(chǔ)設(shè)備上的倉(cāng)庫(kù)。

簡(jiǎn)單來(lái)說(shuō)是本身可視為電子化的文件柜——存儲(chǔ)電子文件的處所,用戶可以對(duì)文件中的數(shù)據(jù)進(jìn)行新增、截取、更新、刪除等操作。
一、安裝部署http://www.daiqiyang.com

#系統(tǒng)默認(rèn)已經(jīng)安裝該數(shù)據(jù)庫(kù),如果沒(méi)有安裝,使用以下命令進(jìn)行安裝

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

#啟動(dòng)數(shù)據(jù)庫(kù)服務(wù)

[root@mail ~]# systemctl restart mariadb

#初始化數(shù)據(jù)庫(kù)

[root@mail ~]# mysql_secure_installation                                         

這里第一次直接回車:

為數(shù)據(jù)庫(kù)的root設(shè)置密碼: 

 后面做一些初始化設(shè)定,一般都選Y就可以了。

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

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

#重新加載防火墻配置

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

二、登陸使用

#數(shù)據(jù)庫(kù)系統(tǒng)登陸

[root@mail ~]# mysql -uroot -predhat                     //注意這里的命令與參數(shù)之間沒(méi)有空格

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

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

-u:連接mysql服務(wù)器的用戶名;

-h:mysql服務(wù)器的ip地址或主機(jī)名;

-p:連接mysql服務(wù)器的密碼;

#查看系統(tǒng)有多少數(shù)據(jù)庫(kù)

MariaDB [(none)]> show databases;                                          //在數(shù)據(jù)庫(kù)中的命令都以;結(jié)尾

#退出數(shù)據(jù)庫(kù)系統(tǒng)

MariaDB [(none)]> quit

MariaDB [(none)]> exit

#創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)

MariaDB [(none)]> create database luntan;

#切換到某個(gè)數(shù)據(jù)庫(kù)下

MariaDB [mysql]> use mysql;

#查看數(shù)據(jù)庫(kù)的表

MariaDB [mysql]> show tables;

#查看數(shù)據(jù)表的表結(jié)構(gòu)

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)建好的表的表結(jié)構(gòu)

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;

三、用戶的管理和訪問(wèn)權(quán)限的控制

創(chuàng)建數(shù)據(jù)庫(kù)登陸用戶

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;

查看當(dāng)前使用用戶:

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

查看當(dāng)前用戶的數(shù)據(jù)庫(kù):

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

使用小明用戶登錄數(shù)據(jù)庫(kù):

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

#查看可以訪問(wèn)的數(shù)據(jù)庫(kù)

MariaDB [(none)]> show databases;

#以root用戶登錄給xiaoming用戶一張表的權(quán)限

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

退出數(shù)據(jù)庫(kù)系統(tǒng),并使用xiaoming用戶重新登陸

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

MariaDB [(none)]> use mysql;

#測(cè)試各種權(quán)限

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用戶的權(quán)限

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

#使用select語(yǔ)句進(jìn)行刪除表數(shù)據(jù),確認(rèn)權(quán)限已被禁用

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

四、備份和還原

備份整個(gè)數(shù)據(jù)庫(kù)的所有表

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

#使用root用戶登錄數(shù)據(jù)庫(kù),刪除person表

MariaDB [mysql]> drop table person;

#退出系統(tǒng),進(jìn)行還原操作

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

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

#登陸數(shù)據(jù)庫(kù)系統(tǒng)

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

#查看person表

MariaDB [mysql]> select * from person;

關(guān)于Linux中mysql數(shù)據(jù)庫(kù)的管理就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI