Execute shell command from within a MySQL client? - mysql

In oracle database, command "host" can be used to run bash command from database command window. Is there a equivalent command as "host" in mySql?

You can use the system command.
system command, \! command
Executes the given command using your
default command interpreter.
The system command works only in Unix.
Example:
system ls -l

As an additional tweak, on most "'nix" systems you can actually do something like this:
mysql> system bash
And have the entire terminal at your disposal. This is particularly effective if you're doing code work in the terminal that interfaces with MySQL; from the shell, typing "exit" will take you back into the MySQL monitor/client, so you can go back and forth quite easily.
Obviously, other shells ("mysql> system tcsh") would work as well.

It's actually possible to execute shell commands on the server that mysqld is running though a client connection (rather than executing commands on the client machine locally) by using MySQL Proxy (scroll down to the "Shell commands from MySQL client" section ).

I'm running version 5.0.95-log on Linux. Prefacing the command either by "system" or by "!" works for the "pwd" and the "ls -l" commands. If I try to change directory using, e.g.
mysql> system cd /home/mydir
the command seems to be accepted. But this apparently does nothing, as following "pwd" and "ls -l" commands indicate that I am still in the same directory. So it appears that there is a stub of limited functionality built in for this, but that we do not actually have full access to the system shell.

In a linux machine you should be able to use the following example
! clear - to clear the screen
! ls - to list files in the current working directory
Basically you should be able to run any command or script using that syntax
NB: Add a back slash before !

Related

How to execute os command from mysql5 for windows?

In mysql5.X database on Linux the command system or ! can be used to run bash command from database command line.
Are there equivalent commands as system or ! in mysql5.X database for Windows?
Linux:
mysql> !ifconig
Windows:
mysql> !ipconfig
The Windows command does not work!
In mysql5.X database (for Linux), command "system" or "!" can be used to run bash command from database command Linux.
You misunderstand.
This is not SQL command. This is command-line client command. And it will work only in CLI or another client which supports this command. It will NOT work if you try to send in to MySQL server directly by any technique.
Is there a equivalent command as "system" or "!" in mysql5.X database (for Windows)?
If used CLI version supports this command then it will work otherwise no. This not depends on MySQL server version.
Windows is not the same operating system, and as such the names of the tools internally are, in many cases, completely different, like ls vs. dir.
ipconfig is a Linux thing but it's also not necessarily a given that it'll be present. On more modern distributions the ip command is becoming the default. That's to say you'll need to lookup what the command you're going to run is, and verify that with testing.
You'll need to run the command appropriate for the environment you're running on, whatever that is.
That being said, running shell commands inside of the mysql> shell is highly irregular and should be a last resort. Doing it can be viewed as highly suspicious and may be restricted.

Execute .SQL in PowerShell

I wanted to know how I can run a .sql file which contains several queries in powershell
What I need is that through the command console of powershell, execute this .sql file and start creating the db with its respective tables from the querys that has this file, I understand that in linux I can do it, but I would like to know how to do it in powershell
from now on thanks
The powershell command looks like this
C:\> cmd.exe /c "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe -u root -p password ]schema_name} < DataBase72.sql"
there are more Options for mysql.exe
Without getting into the specifics, there are two general ways to do this:
Remember there's a reason it's named PowerShell: it's also a full command line shell. This means you can connect to MySql the same way as in other shells, using the same tools provided by the database (ie: mysql.exe, mysql-cli, etc)
Powershell can use .Net libraries, including the same .Net libraries used for connecting to MySql from a C# or VB.Net program. If Connector/Net is available on the system, or if you can install it for the version of .Net available to you, you can create and use these ADO.Net objects from PowerShell code.

Cannot connect to MySQL via Windows PowerShell

I have MySQL 5.7 Community installed on Win10 and I have been using it with MySQL Workbench. Now I want to set up a replication slave and from what I found this is not possible using Workbench.
That means I have to use the Shell. According to the MySQL docs I open the Windows PowerShell (as I understand “your command interpreter”) and type the name of my database.
so the line in the PowerShell looks like this:
PS C:> mysql my_database
What I get is: “mysql: the name ‘mysql’ was not recognized as the name of a Cmdlet, a function …” (this is my translation). I tried it with the path to where MySQL is installed but same output. Where is my mistake? What is missing? Would be happy if someone could give me a hint! Thanks!
You need to set mysql in your windows environment variables to use the command "mysql" in your terminal. You could also use the full path in your terminal, but it's annoying and bad for scripting

Is there a .profile-like file for use with MySQL Shell?

I am new to MySQL. I use MySQL Shell 1.0.9 on Windows 10. Each time I log in, I have to issue a connect command (\c localhost), and supply the password. Also, I have to issue a \sql command to switch out of the shell's default JavaScript mode.
Is there a mechanism where I can automate these per each log in, much like with a .profile file used on Linux for the Bash shell?

Run system commands when logged into MySQL

This is NOT about MySQL's "data directory".
When I log into MySQL on my machine (Windows 10 OS) I know that MySQL still has a "current working directory": importing a .sql file works, giving only the filename, if the file is located in the same directory as the one where I went
> mysql -u ....
So I wondered whether it was possible to get the CWD when actually logged into MySQL. Then I wondered whether it might be possible to change it, i.e. to navigate somewhere else. Then I wondered about running system (i.e. command prompt) commands generally, while still logged in to MySQL.
Here we see a claim that you can indeed do all these things... by going something like:
\! ls -l
On my machine this doesn't work... probably because it's not a Linux OS. I get unknown command '\!'...
Is there any way to do this in Windows?
You're right. The Windows version of the mysql command line client lacks the shell escape \! command.
It is an open source project :-). You or I could give them a pull request with a fix. But then we'd have to decide whether we wanted cmd or powershell in the escaped process.