mysql資料備份恢復和許可權
阿新 • • 發佈:2021-01-13
-- 資料匯入匯出:
--獲取資料表的完整結構:
mysql> show create table courses;
mysql> select * from courses into outfile '/root/mysql_test_courses.txt';
/*
報錯The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
檢視許可權,顯示只能匯出到指定目錄:mysql> show variables like '%secure%' ;
mysql> select * from courses into outfile '/var/lib/mysql-files/mysql_test_courses.txt';
*/
-- 匯出sql格式的資料mysqldump:
mysqldump -u root -p mysql courses > courses.txt
mysqldump -u root -p hellodb > hellodb.sql
-- 拷貝至本地:
mysqldump -h other-host.com -P port -u root -p database_name > dump.txt
-- 匯入到遠端主機:
mysqldump -u root -p database_name \| mysql -h other-host.com database_name
-- 匯入:登陸mysql,進入指定資料庫執行
source /root/courses.sql;
-- 使用者許可權:
grant all privileges on db.table to 'user'@'ip' identified by 'user_passwd' with grant option;
all privileges:表示將所有許可權授予給使用者
on:表示這些許可權對哪些資料庫和表生效,格式:資料庫名.表名
to:將許可權授予哪個使用者。格式:”使用者名稱”@”登入IP或域名”
identified by:指定使用者的登入密碼
with grant option:表示允許使用者將自己的許可權授權給其它使用者
-- 重新整理許可權:
flush privileges;
-- 檢視使用者許可權:
show grants for user
-- 回收使用者許可權:
revoke create on *.* from 'user'@'ip';
flush privileges;
-- 刪除使用者:
drop user 'user'@'ip';
-- 改名:
rename user 'old_name'@'ip' to 'new_name'@'ip';
-- 改密碼:
-- mysql5.7之前:
update user set password=password('[email protected]') where user='root';
-- mysql5.7之後:
update user set authentication_string=password('[email protected]') where user='root';
-- 忘記密碼:
/etc/my.cnf中新增skip-grant-tables
重啟 systemctl restart mysqld