execute mysql commands via desktop shortcut? - mysql

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?

Related

Mysql events using cpanel cronjob

We have a queueing system that was developed by our previous developer, and the truncate command was manually executed to the mysql query. We cant use event scheduler on cpanel so the best option we have is to use cronjob. however, we have no idea on how to execute linux command.
Can someone help me to make a linux query for this?
TRUNCATE TABLE counter_logs_vxphl;
Try to execute the command using the following command, replace {USER} with the username and {PASSWORD} with the password, if you want to specify the host just add -h {HOSTNAME}
mysql -u {USER} -p{PASSWORD} -e "TRUNCATE TABLE counter_logs_vxphl;" > mysql-truncate.log

Run mysql commands in bash script without logging in or adding -u root to every command

I'm writing a bash script to do some db stuff. New to MySQL. I'm on Mac and have MySQL installed via homebrew.
Am using username "root" right now and there isn't a pw set. I included the pw syntax below just to help others out that may have a pw.
My goal is to have mysql commands be as "clean" as possible in my bash script
Not a hige deal, but would like to do this if possible.
Example
# If I can do it without logging in (*ideal)
mysql CREATE DATABASE dbname;
# Or by logging in with - mysql -u root -pPassword
CREATE DATABASE dbname;
# Instead of
mysql -u root -pPassword -e"CREATE DATABASE dbname";
Tried to simplify it. I have a handful of things I gotta do, so would rather keep my code cleaner if possible. I tried logging in with the bash script, but the script stopped once logged into MySQL and didn't run any commands.
Another option I was considering (but don't really like) would be just to keep username and pw string in a var and call it for every commmand like so
# Set the login string variable
login_details="-u root -p password -e"
# example command
mysql $login_details"CREATE DATABASE dbname";
So any ideas?
Write a new bash script file and run this file after putting all your commands into it. Don't forget to give right username and password in your bash script.
For bash script:
#!/bin/bash
mysql -u root -pSeCrEt << EOF
use mysql;
show tables;
EOF
If you want to run single mysql command.
mysql -u [user] -p[pass] -e "[mysql commands]"
Example:
mysql -h 192.168.1.10 -u root -pSeCrEt -e "show databases"
To execute multiple mysql commands:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
Note: -B is for batch, print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. -s is silent mode. Produce less output. -e is to execute the statement and quit

try to connect to mysql through command line , leaves blank space after password

Trying to connect to an rds mysql server from an ec2 ubuntu server.
I use
mysql -h my_host_name -u admin_name -p database < data.sql
When the password prompts, I enter my password. However all this does it create a new blank line and does nothing else.
Any ideas?
When mysql is processing file input, it doesn't normally print informative messages, it only displays the results of SELECT queries. If you want to see messages from queries that modify the database, add the -v option to make it verbose.
mysql -v -h my_host_name -u admin_name -p database < data.sql
If you use -v -v it will produce even more details, and -v -v -v will be most informative.
This blank line probably means that your mysql is processing what is inside your "data.sql".
If you need to see what is been processed, you can first connect to mysql server with:
mysql -h my_host_name -u admin_name -p
Change to your database ( if you have one defined and your sql is not creating one... ):
mysql> change my_database;
Than you call your script execution with:
mysql> source data.sql;
{}'s

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

bash script command to inject a schema.sql into mysql db

i found this code but do not quite understand what the command is doing.
sudo -u test-user mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
i know the last part is using lib.model.schema.sql to create the tables and fields
the first part i dont quite understand: sudo -u test-user mysql -U test_traffic traffic
i know the command sudo and mysql
please explain?
thanks
Let's look at it bit by bit. Firstly the format
sudo -u username command
is an instruction to run command (which might be simple or complex) as the user username. So in your example, you are running the mysql command as the user test-user. You should note that this includes all the parameters to the mysql command - that's the entire rest of the line.
The command
mysql -U test_traffic traffic < ./phoenix/data/sql/lib.model.schema.sql
appears corrupt (certainly running it on 5.0.51a fails). It would make sense if the -U was a -u which would indicate that that the command was to be executed for mysql user test_traffic. If it was a -u you would then have an instruction to import the sql file into the traffic database.
So the combined instruction says, import the lib.model.schema.sql file into the database test_traffic using the mysql user test_traffic and executing the entire command as if you were logged-in as the user test-user.
Try Below steps for mysql:
mysql > -h hostname -u username -p password
mysql > use databasename;
mysql > source path/to/scriptfile
If you want to inject theschema.sql file into your database, with a shell script, simply use :
mysql -h [host] -u [username] -p[password] -D [database] < your_file
If you want to dynamicly tell which file should be loaded, replace your_file by $1 and pass the name of the file as an argument to your script.
Take care also to the -p option. There is no space between the -p and your password.