The MySQL command line packaged with maria-db is quite useful. You can use it to directly execute a saved sql script, query the database directly with a single command and even supports different parameters to better format your results.
Here are some examples.
- Querying maria–db using one line command.
This simply logs you into the database, -u specifies the user name and -p prompts for password. You can also type in your password within the command itself such as -p mypassword; however this would print your password in plain text.The -e switch allows you to directly enter a sql query to be executed as you log in.
- Executing a script while logged into database in command prompt.
Here you log into the database with mysql -u root -p as you normally would. The only difference is that you have a sql script saved somewhere and you can execute it by entering source \path\to\your\sqlscript.The output will be displayed in the console similar to if you entered a sql query manually in the command line.
- Write output of a sql script into a log file.
This is similar to the command in step # 1, you are just adding an additional > and putting it to a path and file.The format is: mysql -u username -p < “\path\to\sqlscript\” > “\path\to\log\file”
I got the following error from step 3
No database selected
Please advise. Thank you so much.
Do you have a script that you are running and trying to output into a log file?
I think the issue might be with your script, are you able to execute the contents of the script manually? Within the script, you might have to explicitly declare which database to use such as:
use database;
select * from database;
Let me know if this helps.
Regards,
Khoa
[…] Here you log into the database with mysql -u root -p as you normally would. The only difference is that you have a sql script saved somewhere and you can execute it by entering source pathtoyoursqlscript. The output will be displayed in the console similar to if you entered a sql query manually in the command line. via […]
Will it support using a “driver” file? A master file with 2 .. n additional script files to run?
For instance. In the main file (MySQL-RunTestSuite.sql) I would do :
MySQL – u MyTestRunUser -ppassword!
CREATE OR REPLACE DATABASE MY-TEST-DB
USE MY-TEST-DB
./Script/CreateTables.sql
./Script/CreateStoredProc.sql
./Script/ProcessSecurity.sql
./Script/RunTestSuite_01.sql
./Script/RunTestSuite_02.sql
What is the syntax for call a script file from a “master” script file?
[…] Executing Scripts with Maria-db Command Line – Syspanda […]
[…] » Visit Now Sep 07, 2017 · Write output of a sql script into a log file. This is similar to the command in step # 1, you are just adding an additional > and putting it to a path and file.The format is: mysql -u username -p < “pathtosqlscript” > “pathtologfile”. […]