mysqldump报错 1044: Access denied for user root@localhost to database information_schema when using LOCK TABLES

时间:2020-01-09 10:42:18  来源:igfitidea点击:

在使用mysqldump命令进行备份并得到以下错误或者警告:

mysqldump: Got error: 1044: Access denied for user root@localhost to database information_schema when using LOCK TABLES

我该如何解决这个问题?
您可以将single-transaction选项传递给mysqldump命令:

$ mysqldump --single-transaction -u user -p DBNAME > backup.sql

另一种选择是向用户授予LOCK TABLES:

$ mysql -u root -p

并执行:

mysql> GRANT SELECT,LOCK TABLES ON DBNAME.* TO 'username'@'localhost';

Shell脚本示例

#!/bin/bash
# Purpose: Backup mysql 
# Author: ; under GNU GPL v2.0+ 
NOW=$(date +"%d-%m-%Y")
DEST="/.backup/mysql"
# set mysql login info
MUSER="root"                # Username
MPASS='PASSWORD-HERE'   # Password
MHOST="127.0.0.1"  # Server Name
 
# guess binary names
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
 
[ ! -d "${DEST}" ] && mkdir -p "${DEST}"
# get all db names
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=${DEST}/mysql-${db}.${NOW}-$(date +"%T").gz
# get around error 
$MYSQLDUMP --single-transaction -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done