MySQL資料庫的使用者管理、Database Backup與恢複

來源:互聯網
上載者:User

標籤:一個   收購   存在   關閉   必須   否則   萬用字元   維護   損壞   

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
  • 使用 tar 打包檔案夾備份
    1) 安裝 xz 壓縮格式工具
    #yum install xz -y
    2) 打包資料庫檔案,預設存放在安裝目錄的 data 檔案夾
    #tar Jcvf /opt/mysql-$(data +%F).tar.xz /usr/local/mysql/data
    3) 如果資料庫檔案損壞,可以解壓縮備份檔案
    #tar Jxvf /opt/mysql-2018-07-30.tar.xz /usr/local/mysql/data
    4)我們可以使用計劃性任務來自動對資料庫進行備份並定期刪除之前打包的資料庫檔案

  • 使用 mysqldump 工具備份

    mysqldump -u root -p [密碼] [選項] [資料庫名] [資料表名] > /備份路徑/備份檔案名

  1. 對庫 test 中的表 test_1、test_2進行備份
    #mysqldump -u root -p123456 test test_1 test_2 > /opt/dump.sql

  2. 對整個 test 庫進行備份
    #mysqldump -u root -p123456 test > /opt/test.sql

  3. 對多個庫進行備份(test庫、word庫)
    #mysqldump -u root -p123456 --databases test word > /opt/test_word.sql

  4. 對所有庫進行備份
    #mysqldump -u root -p123456 --all-databases > /opt/all.sql

  5. 直接備份表結構(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與恢複

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.