1

How do I issue commands to MariaDB via the CLI without actually jumping into the interactive use mode?

I know I can type mysql which will then jump me into the interactive mode where I can write SQL commands like CREATE DATABASE dbname; and then exit to go back to the regular terminal.

However I'd like to skip that and do something like mysql 'CREATE DATABASE dbname;' all in one line.

Royal Wares
  • 1,192
  • 10
  • 23

2 Answers2

4
mysql --help | grep "\-execute"

Output:

-e, --execute=name Execute command and quit

So to create a database with command line client, you just need to execute

mysql -uuser -p -e"CREATE DATABASE dbname"

You can also concatenate several SQL statements, e.g.

mysql -uuser -p -e"CREATE DATABASE dbname;SHOW DATABASES"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Georg Richter
  • 5,970
  • 2
  • 9
  • 15
0

Put the commands that you want executed into a text file (optionally with a file extension of .sql) then, from the command line, do mysql -uuser -p < yourtextfile.sql to have all of the commands in the file executed.

Dave
  • 5,108
  • 16
  • 30
  • 40
  • Is it possible to swap the text file for a quoted string like `mysql -user -p < 'create database dbname;'` ? – Royal Wares Aug 24 '20 at 13:30
  • Have you tried it? It doesn't work for me and you would still need to interact with it to supply the password. – Dave Aug 24 '20 at 14:07