How to execute os command from mysql5 for windows? - mysql

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.

Related

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.

MySQL command line client for Windows

Is there any nice command line MySQL client for windows? I mean a single exe that allows connecting and running a sample query. I've googled and only could find big graphical environments like toad or mysql workbench. I need only a simple command line tool, where can I download sth like this?
EDIT: I don't want to install the whole MySQL package on my PC. I know it's inside the mysql package but how do I download only this cmd line client. Because i don't need anything else.
When you go to the MySQL download page, choose the platform "Microsoft Windows". Then download the "Windows (x86, xx-bit), ZIP Archive" (be sure to select the one with size over 140M.
The binaries will be in the "bin" folder.
I understand that this is not just the client binaries, but at least you don't have to install and setup the entire server.
mysql.exe can do just that....
To connect,
mysql -u root -p (press enter)
It should prompt you to enter root password (u = username, p = password)
Then you can use SQL database commands to do pretty much anything....
You can choose only install the client during server install. The website only offers to let you download the full installer (grab whatever version you want from http://www.mysql.com/downloads/mysql/).
In the install wizard, when prompted for installation type (typical, minimal, custom), choose 'Custom'. On the next screen, select to NOT install the server, and proceed with the rest of the install as normal.
When you're done, you should see just the relevant client programs (mysql, mysqldump, etc) in C:\Program Files\MySQL..\bin
Its pretty simple. I saved the mysql community server in my D:\ drive. Hence this is how i did it.
Goto D:\mysql-5.7.18-winx64\bin and in the address bar type cmd and press enter, so command prompt will open. Now if you're using it for the first time type as mysql -u root -ppress enter. Then it will ask for password, again press enter. Thats it you are connected to the mysql server.
Before this make sure wamp or xampp any of the local server is running because i couldn't able to connect to mysql wihthout xampp running.
Happy Coding.
You can also download MySql workbench (31Mo) which includes mysql.exe and mysqldump.exe.
I successfully tested this when i had to run Perl scripts using DBD:MySql module to run SQL statements against a distant MySql db.
You can access mySQL in command line just by typing:
C:\www\mysql\bin> mysql -u root -p
After which you can type sql commands normally such as:
mysql> SHOW DATABASES;
Here, I am assuming you mySQL installation directory is C:\www\mysql.
I have similar requirement where I need a MySQL client but not server (running in a virtual machine and don't want any additional overhead) and for me the easiest thing was to install MySQL community server taking typical installation options but NOT configure the server, so it never starts, never runs. Added C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin to system path environment variable and I'm able to use the MySQL command line client mssql.exe and mysqladmin.exe programs.
If you are looking for tools like the the mysql and mysqldump command line client for Windows for versions around mysql Ver 14.14 Distrib 5.6.13, for Win32 (x86) it seems to be in HOMEDRIVE:\Program Files (x86)\MySQL\MySQL Workbench version
This directory is also not placed in the path by default so you will need to add it to your PATH environment variable before you can easily run it from the command prompt.
Also, there is a mysql utilities console but it does not work for my needs. Below is a list of the capabilities on the mysql utilities console in case it works for you:
Utility Description
---------------- ---------------------------------------------------------
mysqlauditadmin audit log maintenance utility
mysqlauditgrep audit log search utility
mysqldbcompare compare databases for consistency
mysqldbcopy copy databases from one server to another
mysqldbexport export metadata and data from databases
mysqldbimport import metadata and data from files
mysqldiff compare object definitions among objects where the
difference is how db1.obj1 differs from db2.obj2
mysqldiskusage show disk usage for databases
mysqlfailover automatic replication health monitoring and failover
mysqlfrm show CREATE TABLE from .frm files
mysqlindexcheck check for duplicate or redundant indexes
mysqlmetagrep search metadata
mysqlprocgrep search process information
mysqlreplicate establish replication with a master
mysqlrpladmin administration utility for MySQL replication
mysqlrplcheck check replication
mysqlrplshow show slaves attached to a master
mysqlserverclone start another instance of a running server
mysqlserverinfo show server information
mysqluserclone clone a MySQL user account to one or more new users
mysql.exe is included in mysql package. You don't have to install anything additionally.
download the mysql-5.0.23-win32.zip (this is the smallest possible one) from archived versions in mysql.com website
cut and paste the installation in c drive as mysql folder
then install then follow instructions as per this page:
https://cyleft.wordpress.com/2008/07/20/fixing-mysql-service-could-not-start-1067-errors/
For Windows users:
1.Install the full version of MYSQL
2.On the Windows 10 start button click on search and type in MySQL
3. Select the MYSQL Command Line Client 5.5 (I am using version 5.5)
4. go ahead and run your sql queries/
5. to exit type \q or quit

Execute shell command from within a MySQL client?

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 !