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”

Leave a Reply