What's the proper way to execute SQL files in MySQL database?

I’m trying to run a SQL script file that contains multiple database queries in MySQL but I keep getting errors. When I attempt to use the command source /Documents/queries.sql in the MySQL command line, I get this error message:

mysql> . \users\john\Documents\queries.sql ERROR: Cannot open file ‘\users\john\Documents\queries.sql’, error code: 2

I have the SQL file saved on my computer and I’m sure the path is correct. I’ve tried different variations of the command but nothing seems to work. Is there a specific syntax I need to follow or am I missing something obvious? What’s the correct method to import and execute SQL script files in MySQL? Any help would be appreciated since I need to run several database scripts for my project.

hmm that error code 2 usually means file not found - are you using backslashes on windows? try forward slashes instead like source /users/john/Documents/queries.sql or maybe double backslashes. also curious, what operating system are you running this on? sometimes the path format makes all the diference

The source command requires an absolute path or proper relative path syntax. Your error suggests a path resolution issue. Instead of using backslashes, try the full absolute path with forward slashes: source C:/users/john/Documents/queries.sql. Another reliable method is using the -e parameter when starting MySQL: mysql -u username -p database_name < C:/users/john/Documents/queries.sql from your system command prompt. This approach bypasses the MySQL shell entirely and often handles path issues more gracefully. I’ve found this external method particularly useful when dealing with large script files or when automating database deployments, as it provides clearer error messages and better handles special characters in file paths.

make sure you’re in the mysql prompt when trying that source command. also, try wrapping the path in quotes like source '/users/john/Documents/queries.sql', since spaces can cause issues. alternatively, cd to the dir first and do source queries.sql to avoid path mess.