標籤:一個 收購 存在 關閉 必須 否則 萬用字元 維護 損壞
MySQL 是一個真正的多線程、多使用者的 SQL 資料庫服務,憑藉其高效能、高可靠和便於使用的特性,成為伺服器領域中最受歡迎的開來源資料庫系統。在2008年以前,MySQL 項目由 MySQL AB 公司進行開發、發布和支援,之後曆經 Sun 公司收購 MySQL AB 公司,Oracle 公司收購 Sun 公司的過程,目前 MySQL 項目由 Oracle 公司負責運營和維護。
MySQL 資料庫的使用者管理
-
建立使用者
mysql -u root -p ##登入資料庫
use mysql; ##進入mysql資料庫(所有建立的使用者都儲存在mysql資料庫的user表中)
CREATE USER ‘username‘@‘host‘ IDENTIFIED BY ‘password‘;
#其中host是指定使用者可以在哪些主機登入,可使用 IP地址、網段、主機名稱的形式,
#本機使用者可用 localhost ,任意遠程主機登入可以用萬用字元 %
-
查看使用者
use mysql;
select User,authentication_string,Host from user;
-
刪除使用者
DROP USER ‘username‘@‘host‘;
-
重新命名使用者
RENAME USER ‘old_user‘@‘host‘ TO ‘new_user‘@‘host‘;
-
修改使用者密碼
SET PASSWORD=PASSWORD(‘password‘); #修改目前使用者密碼,括弧內為新密碼
SET PASSWORD for ‘username‘@‘host‘=PASSWORD(‘password‘); #修改其他使用者的密碼
-
忘記 root 使用者密碼的解決辦法
1: 停止mysql服務進程
systemctl stop mysqld.service #關閉mysql服務
netstat -ntap | grep 3306 #確保mysql進程關閉
2: 跳過使用者列表 user 驗證,使使用者可以不使用密碼直接登入
mysqld --skip-grant-tables
3: 重新開啟一個終端,使用mysql直接登入到資料庫
mysql #直接進入資料庫
update mysql.user set authentication_string=password(‘password‘) where user=‘root‘; #修改root密碼
4: 重新整理資料庫,使用新密碼登入root使用者
flush privileges; #重新整理資料庫
mysql -u root -p #使用新密碼登入
- 授權管理
GRANT 許可權列表 ON 庫名.表名 TO ‘username‘@‘host‘ IDENTIFIED BY ‘password‘;
資料庫的備份及恢複MySQLDatabase Backup
-
對庫 test 中的表 test_1、test_2進行備份
#mysqldump -u root -p123456 test test_1 test_2 > /opt/dump.sql
-
對整個 test 庫進行備份
#mysqldump -u root -p123456 test > /opt/test.sql
-
對多個庫進行備份(test庫、word庫)
#mysqldump -u root -p123456 --databases test word > /opt/test_word.sql
-
對所有庫進行備份
#mysqldump -u root -p123456 --all-databases > /opt/all.sql
- 直接備份表結構(test庫中的test_1、test_2表)
#mysqldump -u root -p123456 -d test test_1 test_2 > /opt/tables.sql
恢複資料庫
-
登入 mysql 恢複
drop database test; #刪除損壞的test庫
source /opt/test.sql;
-
使用mysql命令恢複
drop database test; #刪除test庫
create database test; #必須先建立test庫,否則會報錯
exit; #退出資料庫
mysql -u root -p123456 test < /opt/test.sql #使用命令恢複
- 表的恢複和庫的恢複一致,只需注意先要進到表存放的庫裡面即可。
MySQL資料庫的使用者管理、Database Backup與恢複