using batch to detect whether database exists,if not ,create it - mysql

I am going to write a .bat file to realize this function:
to detect whether the database and the table existed,if not exist,create them.
I tried to write the bat file like this:
"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost -u root --password=
select * from martin.aaaperson
Then in cmd when I execute this, the bat file will not run the query until after I exit from mysql.
C:\000test>"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost
-u root --password=
(information of mysql)
mysql> exit
Bye
C:\000test>select * from martin.aaaperson
"select" is not knowby cmd

you need to do something like
mysql.exe -u root -p < your_sql_commands_in_this_file.sql
Put your sql into a file, and then redirect that file into mysql, so it's used as actual commands.
Batch files can only work at the command line. Once you run mysql.exe the .bat file is suspended until mysql exits, and then the batch resumes. That means by the time the batch fires back up again, you've exited mysql and are no longer doing sql operations - you're just back at a command prompt.

Related

sqldump somehow looks for a table instead of full backup?

I have a simple BAT file that I want to run at times to take backups of two local development MySQL databases.
The file name should be "testdb1-Fri 6/6/2018 194233.sql" for example. However, my BAT script is making mysqldump look for a table called 6/6/2018 somehow? Script below, and screenshot of error.
#ECHO OFF
set filename=%date:~0,4%%date:~5,2%%date:~8,2%%date:~10,4% %time:~0,2%%time:~3,2%%time:~6,2%
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" -u root -proot -hlocalhost cadsys > C:\temp\testdb1-%filename%.sql
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" -u root -proot -hlocalhost comm1 > C:\temp\testdb-%filename%.sql
pause
cls
exit
Any pointers?
Add quotes around the suggested filename, the spaces within the filename are interpreted by mysqldump as seperate commands
set filename="%date:~0,4%%date:~5,2%%date:~8,2%%date:~10,4% %time:~0,2%%time:~3,2%%time:~6,2%"

batch file to run mysql

I wrote a sql script to create a clean database (one.sql), I did this on CMD and it works,
cd MYSQL\path1\bin
mysql -u userID -ppassword -h host -P port < path2\one.sql > path2\test.log
it gives me the test.log file and database on the sql server was updated
And I tried to do the same thing using batch file,
#echo off
c:
cd "MYSQL\path1\bin"
mysql -u userID -ppassword -h host -P port < path2\one.sql > path2\test.log
pause
from the CMD prompt, it seems working, but no test.log file generated, and the new database in my sql server was not created.
I ran bat file by double clicking it in its folder.
Any suggestions?
Thank you

How to connect to MYSQL database on server via Terminal on Macbook?

Is there a way to connect to my mysql database and do sth on tables via terminal?
Yes. In your terminal start the mysql prompt using
mysql --user=user_name --password=your_password db_name
Where db_name is the name of your database and user_name and password are your username and password.
You can then run SQL statements/queries from .sql files
mysql db_name < script.sql > output.tab
Where db_name is your database name, script.sql is a file containing your script, and output.tab (optional) is a file in which to dump the output of the query
You then simply place an SQL query in a file and run it.
If you get the error mysql: command not found, this is because the mysql executable cannot be found in your system PATH. If so, you need to run the following command to add the mySQL folder to the PATH, so that OS X knows to look there for the executable
export PATH=${PATH}:/usr/local/mysql/bin
Where /usr/local/mysql is the location of your mysql installation.
You can add this to your .bash_profile file (located at ~\.bash_profile, or you can create it) in order to have it run every time you start a new terminal. Otherwise you'll have to enter it manually before using the mysql command
Once you've entered this command (or added it to .bash_profile) you can use the mysql command as above
Alternately navigate to /usr/local/mysql/bin (or the location of your mysql install) and use the command
./mysql command
Instead of
mysql command
As above (where command is the command described in the first half of this post). This runs the mysql binary directly, rather than searching for it in the PATH

Run sql script using batch file from command prompt

D:
cd Tools/MySQL5/bin
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
\q
When I run the above lines in command prompt it works fine but when I save it as a batch file and run the file it connects to mysql but doesn't perform the sql scripts.
Now what I see is wrong here is that while executing the above commands one by one in your prompt, once you run mysql -u root mysql, you are in the mysql console. So your source command would work there but would not work in your batch since you are not in mysql console while running the batch file.
Solution:
What you can do for this is, instead of using source in mysql you can use
mysql dbname < filename
in your batch file in place of
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
This link can assist you further if needed
This should work
mysql -u root xyz < C:/Users/abc/Desktop/xyz.sql;
It sources the SQL commands from your file
You could write something like this
mysql -u dbUsername yourDatabase -e "SELECT * FROM table;"
Or to run repeating tasks create a runtasks.bat file, save under the root of your project then write your cmd tasks inside
mysql -u dbUser -e "DROP DATABASE IF EXISTS testDatabase;"
mysql -u dbUser -e "CREATE DATABASE testDatabase;"
php index.php migration latest #runs your migration files
cd application\tests
phpunit
This would work.
mysql.exe -u user_name -p -h _host_ _schema_ -e "select 1 from dual;"
This will also give you output on same command terminal

execute mysql commands via desktop shortcut?

On a windows machine, every day i have to login to mysql via phpmyadmin, go to a particular table and run the same sql command to do some cleanup.
I want to automate this process without setting up a TRIGGER.....is there a command prompt solution for doing this, or an automatic process that can be simply run from a desktop shortcut?
Write a script Run.bat and copy the following contents in it:
mysql -h localhost -u root -ppassword -D database_name -e "cleanup command".
Schedule this command in your windows scheduler.
If you have multiple cleanup commands, then add them in a sql file and then schedule the following command:
mysql -h localhost -u root -ppassword -D database_name < path_to_sql_file
For windows you could use the task scheduler. You might not be able to get it to login to phpMyAdmin though. Perhaps cmd line for mysql?