- source
不接路径时,查找文件的路径是执行mysql命令时的路径
接路径时,使用路径+filename
[[email protected] mysql]# vi demo.mysql [[email protected] mysql]# pwd /root/mysql [root@wordpressserver mysql]# ls -al total 12 drwxr-xr-x. 2 root root 4096 Nov 2 21:45 . dr-xr-x---. 31 root root 4096 Nov 2 21:49 .. -rw-r--r--. 1 root root 87 Nov 2 21:45 demo.mysql [root@wordpressserver mysql]# mysql> source demo.mysql ERROR: Failed to open file ‘demo.mysql‘, error: 2 mysql> source /root/mysql/demo.mysql Database changed Query OK, 0 rows affected (0.01 sec) mysql> show tables; +----------------+ | Tables_in_demo | +----------------+ | example | +----------------+ 1 row in set (0.00 sec)
- mysqldump 导出一个表,使用sql文件恢复表
[[email protected] ~]# mysqldump -u root -p demodabasename exampletablename > sqldumptest1.sql Enter password: [root@wordpressserver ~]# ls anaconda-ks.cfg Desktop Downloads mysql post-install Public Templates vmtool wordpress wordpress-4.4.2-zh_CN.tar.gz core.3990 Documents Music Pictures post-install.log sqldumptest1.sql Videos vmware-install.pl wordpress (2) [root@wordpressserver ~]# vi sqldumptest1.sql [[email protected] ~]# mysql -uroot -f demo < sqldumptest1.sql ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO) [root@wordpressserver ~]# mysql -uroot -p123456 -f demo < sqldumptest1.sql [[email protected] ~]#
或者
[[email protected] ~]# mysql -uroot -p123456 -f demo < /root/mysql/demo.mysql -f是忽略错误然后继续执行 mysql> show tables; +----------------+ | Tables_in_demo | +----------------+ | example | +----------------+ 1 row in set (0.00 sec) mysql> desc example; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | data | varchar(100) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
shell> mysqladmin create target_db_name
shell> mysql target_db_name < backup-file.sql
就是
shell> mysql 库名 < 文件名
或者
B:使用mysqldump命令
mysqldump -u username -p dbname < filename.sql
C:使用mysql命令
mysql -u username -p -D dbname < filename.sql
时间: 2024-10-12 13:08:37