How to get output of shell in MySQL? - mysql

In linux I can use ! to start a shell command:
mysql >\! ping localhost
Is there a way to get the output of shell in MySQL?

While in the mysql prompt, no. You can however pipe the results of a command into mysql with something similar to:
mysql -u user -ppassword < `ping localhost`
The only issue with this is that whatever your shell command returns needs to be the sum of the commands/data you want to issue to mysql. This is most commonly used to execute a script or something similar.

Related

execute MYSQL commands from a file in terminal

I've been searching to understanding the following MySQL command:
framework_mariadb.sql | mysql -u username -p password -h 127.0.0.1 -P 26257 target
My guess was the sql statements within the sql file get executed by mysql for the given target/database. But then I came across the source command in MySQL, ie
\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
So my question is, does the first command and the second command essentially do the same thing? My apologies if this has already been asked, I haven't been able to find details for the first SQL command.
This is more about Linux shell capabilities than it is about MySQL.
The second form runs the mysql client, and uses the < symbol to tell it to take its input from the specified file.
The first form does essentially the same thing, but uses the pipe character | to indicate that the output of the first command should be sent to the input of the second command.
However, for the first form I'd expect the line to start with cat (as in cat framework_mariadb.sql | mysql ...) because the SQL script won't normally run as a shell command. cat is a command that reads one or more files and send s them to its output.
It is possible to set the SQL script up to run like this, but that requires a specific line (#! /bin/cat or similar) to be present at the top of the file, and the file must have the execution bit set. (At least, that's how I'd do it. There might be some other bash magic I'm not aware of. bash is not my forté)
There are many resources on the web that can teach the fundamentals of the Linux shell. You could try Microsoft's Introduction to bash, but there are many others.

Configuring MySQL's mysqlsh to accept SQL and connect by default

How do I configure the MySQL 8.0.15 shell. I just downloaded it and I am already having problems.
When I run the shell program I always have to switch from MySQL JS to MySQL SQL
I always have to reconnect using \connect root#localhost
I would like to open the MySQL shell and just get to work without having to do those two things.
If you start the shell with mysqlsh --sql --uri=myname#localhost it will accept SQL commands directly.
If you need only to use SQL you may wish to consider using the ancient and honorable mysql command line client program. mysql -h localhost -u myname

Series of SQL/DML/DDL commands and Syntax for running in DBMS

This is a small question, but what is the name for a series of SQL/ DML / DDL commands stored in a file? Also, what is the syntax for running this file in DBMS?
The help command in mysql refers to it as a SQL script file. The syntax for running it in MySQL from the shell is:
mysql ..options.. < filename
e.g.
mysql -u username -p databasename < filename.sql
See the MySQL documentation for details of using the mysql command-line tool.
You can also run it from within the mysql command with:
mysql>source filename
or
mysql>. filename
I don't use phpMyAdmin, so I don't know if there's a way to do it from there.

MySQL Syntax Error (Ubuntu LAMP Server)

Alright, so I've got a fairly fresh Ubuntu (server) installation. Just finished installing the LAMP server and when I go to create a database I'm getting the generic syntax error (1064 / 42000).
My query:
CREATE DATABASE phpbb;
Pretty simple and pretty standard, so I'm not sure what the issue is. Any ideas?
It looks from your error like you're trying to execute SQL on the command line, something like:
mysql -u mike -p CREATE DATABASE phpbb';
MySQL isn't going to like that, it separates the initiation of the tool from the SQL commands.
What I'd normally do for CREATE DATABASE, as it's a one off, I'd do it manually.
So start the tool with
mysql -u mike -p
This should prompt you for your password, and connect to the local database, giving you a shell prompt:
mysql>
You then issue your
CREATE DATABASE phpbb;
If you want to run scripts from the command line, put them in a file and redirect the input to mysql. Usually you'd redirect the output too - something like this:
mysql -u mike -p < mysqlscript.sql > outputofscript.log

Running SQL from file MySQL - User Input?

I'm running MySQL from the command line and executing SQL stored in files. What I'm trying to do, is prompt the user to enter input so that I can include this in the SQL script? Is there a way to do this with MySQL?
Many thanks,
James
If you want to execute sql script from the file try this:
mysql -u USER -p -D DATABASE < input_filename.sql