I have this command in linux
mysql -u xyz -p -e "show databases"
Where, the user "xyz" doesn't exist. When it asks for password
if i don't give any password it doesn't give error message or a non-zero value when i run echo $? command.
if i give a password it gives error message
Any reason why? And any suggestions what I have to do to get an error message for this command?
Try:
mysql -u xyz -pYourPassword -e "show databases"
Or:
mysql -u xyz --password=YourPassword -e "show databases"
I guess there is no other way but to run-
mysql -u xyz -p -e "use mysql"
as mysql database doesn't exist for non existing users.
Related
In a simple script like this one:
set -x
# Check if db exists, if not we make it, make user, give privileges
if ! mysql -u root -p -e "use $db" 2>/dev/null; then
c1="CREATE DATABASE $db"
c2="GRANT ALL PRIVILEGES ON ${db}.* to '$username'#'localhost' IDENTIFIED BY '$password'"
c3="FLUSH PRIVILEGES"
mysql -u root -p -e "$c1; $c2; $c3"
else
echo 'DATABASE ExISTS, ABORTING'; exit $DB_EXISTS
fi
I am asked each time, bash sees mysql command, for my root credentials.
Is there a way to avoid that, so that once entered the root password, all
additional mysql commands execute seamlessly?
Try looking into adding password to ~/.my.cnf
[client]
user = root
password = XXXXXXXX
Check out :
How to execute a MySQL command from a shell script?
Specifying the --password argument
mysql -u root --password=my_mysql_pass db_name
Safer using a bash variable
mysql -u root --password=$MYSQL_PASS db_name
So I want to pipe my netstat output into a mysql database
The plan is to make it a continuous crontab event. That way I can store that data and use it elsewhere easily.
I figured before I could figure that part out though I needed to figure out how to run a SQL Command from terminal.
It seems pretty straight forward
sudo mysql -u username -pMYPassword -e "SQL COMMAND"
This however doesn't work...
When I run this it prints the MYSQL Help
When I run this though
sudo mysql -u username -p -e "SQL COMMAND"
everything works perfect, it just prompts me for a password
Now I don't know if it makes a difference or not but my DB password does have exclamation points in it. It is also over 15 chars long
Could either of these be an issue?
mysql -u user -p -e 'SQL Query' database
Where,
-u : Specify mysql database user name
-p : Prompt for password
-e : Execute sql query
database : Specify database name
So the option -p is a PROMPT, the password need not to be linked, but to be separated from it:
sudo mysql -u username -p'PassWord' -e 'SELECT COUNT(*) FROM my_table'
EDIT: If your real password contains special characters, you need to escape them.
Maybe try : sudo mysql -u username -p'MYPassword' -e "SQL COMMAND"
Or : sudo mysql -u username --password=MyPassword -e "SQL COMMAND"
I am trying to run MySQL query on remote machine with this command:
ssh user#192.168.2.26 "mysql -uroot -proot -e \"use test";""
I am not able to use that database.
Please suggest a working command.
Try this:
mysql -h host -u root -proot -e "show databases;";
Try this:
ssh root#host "mysql database -e 'query to run on table_name; more queries to run;'"
Same can be done with user#host if that user has permission to execute SQL queries let alone launch mysql in general. Using -e is the same as --execute, which will run whatever you put within the trailing quotes (single or double) and quit. The standard output format would be the same as you would see using --batch.
MySql seems to have a special command line syntax which includes the database.
mysql -u user -p -e 'SQL Query' database
This documentation is rather old but I got it to work
http://www.cyberciti.biz/faq/run-sql-query-directly-on-the-command-line/
Final working command with ssh:
ssh user#host "mysql -u user -e 'show tables;' databasename"
This ended up working for me in a bash script:
query='USE [database]; SELECT ...'
mysql='mysql -u [username] -p[password] -e '"'""$query""'"
ssh [username]#[server] -t "$mysql"
If you want to make it more safe then add a prompt for the password instead of storing it somewhere potentially unsafe.
This worked for me after a few tests (basically same answer as #King-Wzrd):
ssh -t kom "mysql -uroot -p -e 'show databases;'"
ssh -t kom "mysql -uroot -p < /home/ling/websites/jin_test/.deploy/tmp.sql"
The "trick" was the quotes around the command.
The -t option allows for prompting password interactively via the remote shell.
The kom here is just a ssh config identifier defined in my ~/.ssh/config file (see more here: https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/).
Running this from my Host environment against MySQL within my Homestead VM produced a nice result... although I did have to set the root password from within the VM first in order for it to work.
ssh vagrant#192.168.10.10 mysql -h localhost -u root -p -e "'SELECT * FROM user;' mysql";
Making a script to print out data from a MySQL db via bash, I met the following problem:
While I try to log in, it uses the password as the database to log in to.
Script is like this:
#!/bin/bash
echo $1
db=$1
pasx=$2
CMD="use $db; select * from job_log;"
mysql -u sqluser -p "${pasx}" -e "$CMD"
If I'm going to run the script with the command
User#server:/path/with/file$ sh sql.sh ok hobo
MySQL returns the following:
User#server:/path/with/file$ sh sql.sh ok hobo
ok
Enter password: ERROR 1049 (42000): Unknown database 'hobo'
I might have fully misunderstood something, but I can't put my finger on what it might be.
You need to remove the space after the -p parameter. See the mysql man page. You also need to specify the database in the command (remove it from the query)
mysql -u sqluser -p$pasx -e "$CMD" $db
Or maybe more clear:
mysql --user=sqluser --password=$pasx --execute="$CMD" $db
Try this:
mysql -u sqluser --password=${pasx} -e "$CMD" $db
Question Rewritten:
HOMEDIR="ftpuser"
REMOTEIP="1.1.1.1"
MYSQLPASS="password"
Q1="DROP USER "$HOMEDIR"_shop;"
Q2="DROP DATABASE "$HOMEDIR"_shop;"
Q3="CREATE DATABASE IF NOT EXISTS "$HOMEDIR"_shop;"
Q4="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'#'localhost' IDENTIFIED BY '$MYSQLPASS';"
Q5="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'#'anotherip' IDENTIFIED BY '$MYSQLPASS';"
# Need to grant permissions from another server as well
Q6="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"
echo $SQL
echo " "
ssh -p 8899 root#$REMOTEIP "mysql -u root -p "$SQL""
I then run:
/root/testing/migratesite.sh
And get:
bash: DROP: command not found
bash: CREATE: command not found
bash: GRANT: command not found
bash: GRANT: command not found
bash: FLUSH: command not found
What am I missing?
You are missing quotes and a proper mysql client command line:
ssh -p 8899 root#$REMOTEIP "mysql -u root -p -e \"$SQL\""
You need to escape the quotes around the $SQL variable so they get passed to the remote shell, else they get interpreted by the local shell (that's why you get DROP: command not found, the semi colon is interpreted by the shell.) Also, to have the mysql client to execute a command you have to pass the -e command line option.
Did you try this:
ssh -p 8899 root#$REMOTEIP "echo \"$SQL\" | mysql -u root --password=$SQL_PASS"