Executing Scripts with Maria-db Command Line

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.

  1. Querying mariadb 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.

  2. 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.

  3. 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”

0 0 votes
Article Rating
Subscribe
Notify of
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Kate Nguyen
Kate Nguyen
6 years ago

I got the following error from step 3
No database selected

Please advise. Thank you so much.

trackback

[…] 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 […]

Tom Miller
Tom Miller
2 years ago

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?

trackback

[…] Executing Scripts with Maria-db Command Line – Syspanda […]

trackback

[…] » 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”. […]