create database test; use test; create table test(id int,name char(8)); insert into test values(1,'peter');
mysql服务bin目下执行:
1. 备份数据
mysqldump --opt -uroot -p123456 test > test.dump
2. 从备份文件恢复数据,需要先建立好数据库test
mysql -uroot -p123456 test < test.dump
3. 只备份表结构
mysqldump --no-data -uroot -p123456 test > test-nodata.dump
4. 备份多个数据库
mysqldump --opt -uroot -p123456 --databases test test2 test3> test.dump
导入多个库数据
mysql -uroot -p123456 < test.dump
5. 跨机器备份数据,源端mysql服务器src_host_ip,目标端mysql服务器dsc_host_ip
目标mysql端建立提供远程访问的用户,并赋予数据库权限
create database test create user root@'src_host_ip' identified by '123456'; grant all privileges on *.* to root@'src_host_ip' identified by '123456';
源端mysql拷贝数据
mysqldump --opt -uroot -p123456 test |mysql -uroot -p123456 -hdsc_host_ip test
6. 与mysqlimport结合使用,备份大表
执行备份之前,先在两台host上执行如下命令:
mysql> show variables like '%secure_file%'; +------------------+----------+ | Variable_name | Value | +------------------+----------+ | secure_file_priv | e: est | +------------------+----------+ 1 row in set, 1 warning (0.00 sec)
如果secure_file_priv值为NULL,在mysql服务器配置文件my.ini中增加一项,并重启服务器
secure-file-priv=e:/test
源服务器上执行以下命令,备份test2数据库,并将e:/test文件夹备份到目标服务器上同一目录
mysqldump -uroot -p123456 --tab=e:/test test2
目标服务器上,执行以下命令:
>type e:\test\test2.sql | mysql -uroot -p123456 test2 mysql: [Warning] Using a password on the command line interface can be insecure. >mysqlimport -uroot -p123456 test2 e:/test/test2.txt mysqlimport: [Warning] Using a password on the command line interface can be ins ecure. test2.test2: Records: 1 Deleted: 0 Skipped: 0 Warnings: 0 >mysqladmin -uroot -p123456 flush-privileges
注意:mysqlimport中涉及到的e:/test/test2.txt路径前缀e:/test/一定要写,与secure-file-priv一致
type命令类似Linux中的cat,后面的路径得用windows中的\