Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mathieu-aubin
Created February 21, 2019 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathieu-aubin/a68e7873a7aa7a355ad2b972ab3f946d to your computer and use it in GitHub Desktop.
Save mathieu-aubin/a68e7873a7aa7a355ad2b972ab3f946d to your computer and use it in GitHub Desktop.
export mysql users
#!/bin/bash
SQLLOGIN='root';
SQLPWD='PASSWORD';
FINAL_SQL_FILE='mysql-exported_users.sql';
UHEADTMP=$(mktemp -t mysqltmp_uheader_XXXXXXX);
USRTMP=$(mktemp -t mysqltmp_usr_XXXXXXX);
USRGRTMP=$(mktemp -t mysqltmp_usrgr_XXXXXXX);
USRPRTMP=$(mktemp -t mysqltmp_usrpr_XXXXXXX);
mysql -u "${SQLLOGIN}" -p"${SQLPWD}" <<< "SELECT CONCAT('SHOW GRANTS FOR ','\'',user,'\'@\'',host,'\'') FROM mysql.user" > ${UHEADTMP};
sed '1d' ${UHEADTMP} > ${USRTMP};
while read SQLUSER; do
mysql -u "${SQLLOGIN}" -p"${SQLPWD}" <<< "${SQLUSER}" > ${USRGRTMP};
sed '1d' ${USRGRTMP} >> ${USRPRTMP};
done < ${USRTMP}
echo -e "FLUSH PRIVILEGES\n" >> ${USRPRTMP};
awk '{print $0";"}' ${USRPRTMP} >${FINAL_SQL_FILE};
sleep 0.1;
for TMPFILE in ${UHEADTMP} ${USRTMP} ${USRGRTMP} ${USRPRTMP}; do
if [[ -f ${TMPFILE} ]]; then
rm -f ${TMPFILE};
fi
done
@mathieu-aubin
Copy link
Author

To export mysql users and privileges from an installation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment