How do you execute a script in the MySQL command line?
There are various ways to execute a script in the MySQL command line.
- Use the source command: use the source command in the command line to load and execute a script file. The syntax is as follows:
source /path/to/script.sql;
The path to the script file is /path/to/script.sql.
- To use the -i option in MySQL command: simply add the path of the script file directly after the MySQL command to execute the script. The syntax is as follows:
mysql -u username -p -i /path/to/script.sql
The parameter -u username is for the MySQL login username, -p indicates that a password needs to be entered, and /path/to/script.sql is the path to the script file.
- Redirect a script file as input to the mysql command using the < operator. Syntax is as follows:
mysql -u username -p < /path/to/script.sql
The -u username is the username for logging into MySQL, -p indicates that a password is required, and /path/to/script.sql is the path to the script file.
All three methods can be used to execute MySQL scripts, choose one based on the actual situation.