緣起
Linux系統(tǒng)下我們比較常用的數(shù)據(jù)庫軟件是開源又免費的MySQL。MariaDB是MySQL的一個分支,采用GPL授權(quán)許可,完全兼容MySQL的API與命令行。雖然MariaDB公司即將倒閉,但這不影響它的性能超越MySQL的事實。
數(shù)據(jù)庫備份
使用mysqldump命令。這個命令可以幫助導(dǎo)出一個含有數(shù)據(jù)庫結(jié)構(gòu)與數(shù)據(jù)的SQL腳本,導(dǎo)出腳本的功能大概是:創(chuàng)建數(shù)據(jù)庫判斷語句-刪除表-創(chuàng)建表-鎖表-禁用索引-插入數(shù)據(jù)-啟用索引-解鎖表
要導(dǎo)出所有數(shù)據(jù),可以這么用:
mysqldump -uroot -p --host=127.0.0.1 --port=3306 --all-databases >/tmp/full.sql
*其中host 127.0.0.1指數(shù)據(jù)庫所在服務(wù)器,需要改為自己在用的。Port是默認(rèn)的。
*上方命令需要使用者持有root賬號密碼,如果沒有,把-u后面的root替換為使用者持有的擁有相關(guān)權(quán)限的其他賬號名即可。
當(dāng)然,我們可以通過參數(shù)選擇只導(dǎo)出部分內(nèi)容。在這里不做展開。
數(shù)據(jù)庫用戶查詢
MySQL是一個多用戶管理的數(shù)據(jù)庫,并通過權(quán)限表來控制用戶對數(shù)據(jù)庫的訪問,權(quán)限表默認(rèn)放在了mysql數(shù)據(jù)庫中,主要的有user,db,table_priv,columns_prov和procs_priv。這里用戶信息,哈希加密用戶密碼,用戶權(quán)限等是在user表中。下方的命令可以簡單明了的查看user表的主要信息。
use mysql;
select host,user,password from user order by host desc,user desc;
host為%意味著可以在從任何地址連接到服務(wù)器。
數(shù)據(jù)庫新建用戶
方法一,使用root賬戶運行下面代碼,需要注意的是這種方式創(chuàng)建的用戶默認(rèn)沒有任何權(quán)限。參考下面的數(shù)據(jù)庫賦權(quán)部分分配權(quán)限。
create user 'User1'@'%' identified by 'password1';
方法二,使用root賬戶直接在user表新加。
use mysql;
#等同于grant all privileges on *.* to " User1 "@"%" identified by ' password1' with grant option;
insert into user values("%","User1",password("password1"),"Y","Y","Y","Y","Y","Y","Y","Y","Y","Y");
flush privileges;
#等同于grant select,insert on database1.datatable1 to "User2"@"0.0.0.0" with grant option;
insert into user (host,user) values("0.0.0.0","User2");
insert into db values("0.0.0.0","User2","Y","Y","Y","Y","Y","Y","N","N","N","N")
flush privileges;
#數(shù)據(jù)庫賦權(quán)
使用root賬號,運行下面命令,一般權(quán)限為以下順序:select、update、delete、insert、alter、drop、create等
grant all privileges on databse1.* to User1@'%';
#with grant option指可以將update權(quán)限傳遞給其他用戶
grant all privileges on databse1.* to User1@'%' with grant option;
#用identified by 'password1'指同時重置密碼為password1
grant all privileges on databse1.* to User1@'%' identified by 'password1';
flush privileges;
數(shù)據(jù)庫權(quán)限回收
Revoke命令用來回收權(quán)限。以下是一些示例
revoke all on *.* from "User3"@"localhost";
revoke all on database1.datatable1 from "User3"@"localhost";
revoke select on *.* from "User3"@"localhost";
數(shù)據(jù)庫更新密碼
本質(zhì)上也是更新User表內(nèi)容
update user set password=password('newpassword') where user='User1';
flush privileges;
#數(shù)據(jù)庫刪除用戶
本質(zhì)上也是更新User表
drop user 'User1'@'localhost'
delete from user where user='User1';
flush privileges;
#數(shù)據(jù)庫復(fù)原
source /tmp/full.sql
數(shù)據(jù)庫root密碼重置
在全新的安裝有MariaDB的機器上,如果我們丟失了root密碼,可以在配置文件里做一點調(diào)整。
首先,停止MariaDB服務(wù)
service mariadb stop
#用vim命令編輯安裝目錄下配置文件my.cnf,插入一行” skip-grant-tables”,這樣我們就可以跳過數(shù)據(jù)庫權(quán)限驗證
啟動服務(wù)
service mariadb restart
登陸數(shù)據(jù)庫
mysql -u root
將數(shù)據(jù)庫切換至mysql庫
use mysql;
修改密碼
update user set password=password('newpasswd') where user='root';
刷新MySQL權(quán)限相關(guān)的表
flush privileges;
退出文章來源:http://www.zghlxwxcb.cn/news/detail-610443.html
quit;
將之前插入的配置語句注釋掉,重啟服務(wù),即可使用新密碼登陸文章來源地址http://www.zghlxwxcb.cn/news/detail-610443.html
到了這里,關(guān)于Linux-MariaDB數(shù)據(jù)庫的備份與初始化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!