Execute .SQL in PowerShell - mysql

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.

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.

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

Re-directing output of C code as input to MySQL server

I want to compile a C program related to databases in Windows. I have installed MySQL.
What I actually want to do is re-direct the output of my C code as an input command for my database.
I have done this thing in Ubuntu having followed the below given link:
Connecting to MySQL through C++
P.S. Does it require for me to compile the code in Command Prompt (because that's how I access the MySQL server).
How do I do it?
No - you can compile the code anyhow.
If you want to redirect output, it's easiest to make the program a console application (which can be compiled from the IDE or from the command line).
Then you pipe the output of the program to the mysql command line client.
yourprogram | mysql --user=user_name --password=your_password db_name

Erlang Mysql Source Command

I am using erlang odbc module to connect to mysql and it is working fine for almost all my commands like insert,delete,create,use,drop . But here i need to use the source command to load a file which contain all the commands like create and use database and create some 10 tables... but when i use it like odbc:sql_query(Database_Connection,"source filename") i am getting error and getting disconnected. The file is present in the current directory, i tried giving both the absolute and relative path to the file. PLease do suggest how to use the source command in erlang odbc.
there is no such 'source' sql command in mysql.
There is 'source' command in the mysql shell utlility. But it's just a command for mysql shell (to read and send sql commands to server).
'source' is MySQL CLI command rather than SQL one. I haven't used erlang ODBC personally, but I had similar problem when using JDBC. As far as I remember there was no other option but reading the SQL script, splitting it into separate statements and running them separately one by one.

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 !