Mysql command line import and export sql script statement
In the windows environment, the import and export are usually performed using the database visualization tool (Navicat), and the command line method is often performed on the server of the Linux system. Here, ubuntu is used as an example
import
-
Execute the import after logging into the database
source
Execute the import through the commandsource sql文件路径
, if the sql file is not in the current directory, you need to use an absolute address The SQL file user_innodb.sql exists in the user directory
Log in to mysql, select the corresponding database, and execute
source
the commandsource usesr_innodb.sql
-
Import without logging into the database
mysql -u 用户名 -p 密码 -D 数据库 < sql脚本文件路径名
Precautions
- If it is used in the sql script file
use <database>
,-D 数据库
it can be added differently - If sql does not have a statement to create a database, and there is no database in MySQL, you must first create a new database with the command
Switch the current path and the sql file is not in the same directory, execute
mysql -uroot -D mysql_test < ../user_innodb1.sql
- If it is used in the sql script file
export
mysqldump -u 你的用户名 -p 你的数据库名 >导出的文件名
## 若导出数据库中的单张表,则在数据库后添加表名
Execute in the user directorymysqldump -uroot mysql_test user_innodb > user_innodb2.sql
SELECT query result export
Add into outfile "xx/xx/xx" after the select statement
eg
select id from user where id =1 into file "./userId.txt"