Mysql command line import and export SQL script statements

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

  1. Execute the import after logging into the database

    source​ Execute the import through the command source 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

    image-20220829180211351

    ​ Log in to mysql, select the corresponding database, and execute sourcethe command

     source usesr_innodb.sql
    

    image-20220829180624758

  2. Import without logging into the database

    mysql -u 用户名 -p 密码 -D 数据库 < sql脚本文件路径名
    

    Precautions

    1. If it is used in the sql script file use <database>, -D 数据库it can be added differently
    2. 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

    image-20220829182207259

export

mysqldump -u 你的用户名 -p 你的数据库名 >导出的文件名
## 若导出数据库中的单张表,则在数据库后添加表名

Execute in the user directorymysqldump -uroot mysql_test user_innodb > user_innodb2.sql

image-20220829182946557

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"

Guess you like

Origin blog.csdn.net/Hong_pro/article/details/126590369