mysql数据库备份命令大全
1. mysqldump命令
# 备份整个数据库
mysqldump -u username -p dbname >backup.sql
# 备份选择的表
mysqldump -u username -p dbname table1 table2 >backup.sql
# 压缩备份文件
mysqldump -u username -p dbname | gzip >backup.sql.gz
# 恢复备份
mysql -u username -p dbname< backup.sql
2. mysqlhotcopy命令
# 备份整个数据库
mysqlhotcopy -u username -p dbname /path/to/backup
# 备份指定表
mysqlhotcopy -u username -p -d dbname table1 table2 /path/to/backup
3. 复制命令
# 复制整个数据库
mysql -u username -p -e "CREATE DATABASE dbname2; GRANT ALL PRIVILEGES ON dbname2.* TO 'username'@'%' IDENTIFIED BY 'password';"
mysqldump -u username -p dbname | mysql -u username -p dbname2
# 复制指定表
mysql -u username -p -e "CREATE DATABASE dbname2; GRANT ALL PRIVILEGES ON dbname2.* TO 'username'@'%' IDENTIFIED BY 'password';"
mysqldump -u username -p dbname table1 table2 | mysql -u username -p dbname2
4. mysqlpump命令
# 备份整个数据库
mysqlpump -u username -p dbname >backup.sql
# 备份指定表
mysqlpump -u username -p --tables dbname table1 table2 >backup.sql
# 压缩备份文件
mysqlpump -u username -p dbname | gzip >backup.sql.gz
# 恢复备份
mysql -u username -p dbname< backup.sql