Wood Chen

How to Import a .sql File into a Database from the Command Line

0 comments93 views269 words

This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文

How to import a .sql file into a database from the command line

You can use the following command to import a .sql file into a database:

mysql -u [用户名] -p [数据库名] < [文件路径]

The -u option is followed by your database username, the -p option prompts you for a password, and [数据库名] is the name of the database you want to import data into. Finally, the < symbol is followed by the path to the .sql file you want to import.

For example, if your username is root, your password is password, your database name is mydatabase, and you want to import data.sql into that database, you can run:

mysql -u root -p mydatabase < /path/to/data.sql

Make sure you run this command as a MySQL user with sufficient permissions, and that your .sql file is saved on your computer and you know its exact path.

Is it the same for mariadb?

Yes, MariaDB is a fork of MySQL.

question

MariaDB [(none)]> mysql -u root -p chatgpt_new < /volume1/web/chatgpt-new/db.sql
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'mysql -u root -p chatgpt_new < /volume1/web/chatgpt-new/db.sql' at line 1

The command you entered at the MariaDB prompt looks like this:

MariaDB [(none)]> mysql -u root -p chatgpt_new < /volume1/web/chatgpt-new/db.sql

Using the mysql command here is incorrect because mysql is a standalone command-line tool that starts the MySQL client. To import the .sql file into the chatgpt_new database, enter the following commands directly:

MariaDB [(none)]> use chatgpt_new;
MariaDB [chatgpt_new]> source /volume1/web/chatgpt-new/db.sql;

First, use the use command to select the database (in this case, chatgpt_new), then use the source command to import the .sql file. The source command must be followed by the file path, and the file must be on the local filesystem of the computer running MariaDB.

You need sufficient permissions to perform these operations in MariaDB. If you are unsure whether you have the required permissions, contact your MariaDB administrator for help.

Last updated 2024-01-14

Related posts

Comments 0