A MySQL script file(.sql) is as good as a normal text file.Just put your queries in a text file and your script is ready to run.In this post i explain the procedure to run a mysql script through the command line as well as mysql shell
The script file
I created my script using vi editor in Linux(you may use any text editor :) )Here is my script file
I placed my script file in the root directory.The location of the script is important.Why?? We will get to know it soon.
Executing the script
We can execute our script in two ways.- From the Linux/Unix command line with the help of the redirection operator(<)
- From the mysql shell.
mysql -u
Login to mysql with root privileges(Note:Login into mysql from the same directory where you placed your script)
2. Once in the mysql shell,enter this command to execute your script
source scriptname
Replace scriptname with the script you earlier created and want to execute.
3. Once the mysql shell finds your script file,it will execute it and the results can be instantly seen on your screen.
Now to confirm the changes made by the script, use the show databases and show tables command.
In case the mysql is unable to locate the file,it will throw up an error something like this
No comments:
Post a Comment