Execute SQL File in MySQL: 2 Easy Methods
There are two ways to execute SQL files in MySQL.
- Use the command line tool: open the command line terminal (or use the command line client provided by MySQL), and enter the following command:
mysql -u username -p database_name < file.sql
In this case, ‘username’ is the MySQL username to connect, ‘database_name’ is the name of the database to connect to, and ‘file.sql’ is the path to the SQL file to be executed. After running the command, you will be prompted to enter the password for the MySQL user.
- To utilize the MySQL client: Open the MySQL client, connect to the specified database, and input the following command in the command line.
source file.sql
The file.sql is the path to the SQL file that needs to be executed. After running the command, the content of the SQL file will be executed directly.