分类: MySQL
MySQL使用MySQLdump导出所有库

导出所有数据库

/usr/local/mysql/bin/mysqldump -u用户名 -p密码 --lock-all-tables --all-databases > /tmp/all.sql

导入数据库

/usr/local/mysql/bin/mysqldu -u用户名 -p密码 < /tmp/all.sql

导出指定库,排除不导出的库

#!/bin/bash
USER="用户名"
PASSWORD="密码" 
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $USER -p$PASSWORD --databases $db > `date +%Y%m%d`.$db.sql
    fi
done

备份所有用户名密码

mysql -BNe "select concat('\'',user,'\'@\'',host,'\'') from mysql.user where user != 'root'" | \
while read uh; do mysql -BNe "show grants for $uh" | sed 's/$/;/; s/\\\\/\\/g'; done > grants.sql


相关博文:

发表新评论