匯出MySQL使用者權限

來源:互聯網
上載者:User

標籤:database   dba   mysql   shell   sql   

    在對MySQL資料庫進行遷移的時候,有時候也需要遷移來源資料庫內的使用者與許可權。對於這個遷移我們可以從mysql.user表來擷取使用者的相關許可權來產生相應的SQL語句,然後在目標伺服器上來執行產生的SQL語句即可。本文提供了產生提取使用者權限的指令碼並給出示範。

 

1、產生使用者權限的指令碼

[[email protected] ~]# more exp_grant.sh#!/bin/bash#Function export user privilegespwd=123456expgrants(){  mysql -B -u‘root‘ -p${pwd} -N [email protected] -e "SELECT CONCAT(    ‘SHOW GRANTS FOR ‘‘‘, user, ‘‘‘@‘‘‘, host, ‘‘‘;‘    ) AS query FROM mysql.user" |   mysql -u‘root‘ -p${pwd} [email protected] |   sed ‘s/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}‘}expgrants > ./grants.sql

2、產生許可權SQL指令碼

[[email protected] ~]# ./exp_grant.sh [[email protected] ~]# head grants.sql-- Grants for [email protected] GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘127.0.0.1‘ IDENTIFIED BY PASSWORD ‘*EB3EA446C759C9DA93F84FCB56430DBEF051A9DD‘ WITH GRANT OPTION;GRANT ALL PRIVILEGES ON `CNBO0815`.* TO ‘root‘@‘127.0.0.1‘ WITH GRANT OPTION;-- Grants for [email protected]% GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘172.16.10.%‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘;-- Grants for [email protected]% GRANT USAGE ON *.* TO ‘CNBO‘@‘192.168.1.%‘ IDENTIFIED BY PASSWORD ‘*ABD91BAD4A3448428563952E281015B237310EA8‘;         ...........................--Author : Leshami--Blog   : http://blog.csdn.net/leshami

3、在目標伺服器上執行指令碼
將產生的指令碼在目標伺服器上執行即可。 mysql -uname -ppwd <grants.sql
需要注意:
a、目標服務上為非空伺服器,已經存在一些賬戶及許可權應考慮會覆蓋的問題。
b、如果僅僅需要遷移非root使用者,可以在原指令碼中添加過濾條件,即 where user<>‘root‘ 。

參考:
http://serverfault.com/questions/8860/how-can-i-export-the-privileges-from-mysql-and-then-import-to-a-new-server

匯出MySQL使用者權限

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.