unable to connect the mysql database using shell script - mysql

Am facing problem to connect the MySQL DB from shell script. Please find the below snippet i have written for connecting the MySQL data base. please suggest on this.
My shell Script:
#!bin/bash
Query="select * from Main"
MySQL -u root -p '!!root!!' -e kpi << EOF
$Query;
EOF
Please check the above code and suggest me how to connect the DB.

I think it should be
-pThePassword
So you should delete the space between -p and the pass. Also you should not use an apostrophe (except it is part of the pass itself. Use a backslash to escape special characters.
Second: *nix systems are case sensitive, please try mysql instead of MySQL
Update
You could also try to type your password into a file and read it with your script
mysql -u root -p`cat /tmp/pass` -e "SHOW DATABASES"
The file /tmp/pass should contain your password without any newline char at the end.
Update 2
Your Script is wrong.
You can either use mysql ... -e SELECT * FROM TABLE or mysql ... << EOF (without -e). You should not mix them.
Don't forget to pass the databasename as a parameter (or with use databasename;) in the sql
Don't forget to add a ; after every sql command, if you have multiple statements
Method One:
mysql -u root -ppassword databasename -e "SELECT * FROM main"
Method Two:
mysql -u root -ppassword databasename << EOF
SELECT * FROM main
EOF
Method Three:
mysql -u root -ppassword << EOF
USE databasename;
SELECT * FROM main;
EOF

mysql --user=root --password=xxxxxx -e "source dbscript.sql"
This should work for Windows and Linux.
If the password content contains a ! (Exclamation mark) you should add a \ (backslash) in front of it.

Related

connect to mysql db and execute query and export result to variable - bash script

I want to connect to mysql databse and execute some queries and export its result to a varibale, and do all of these need to be done entirely by bash script
I have a snippet code but does not work.
#!/bin/bash
BASEDIR=$(dirname $0)
cd $BASEDIR
mysqlUser=n_userdb
mysqlPass=d2FVR0NA3
mysqlDb=n_datadb
result=$(mysql -u $mysqlUser -p$mysqlPass -D $mysqlDb -e "select * from confs limit 1")
echo "${result}" >> a.txt
whats the problem ?
The issue was resolved in the chat by using the correct password.
If you further want to get only the data, use mysql with -NB (or --skip-column-names and --batch).
Also, the script needs to quote the variable expansions, or there will be issues with usernames/passwords containing characters that are special to the shell. Additionally, uppercase variable names are usually reserved for system variables.
#!/bin/sh
basedir=$(dirname "$0")
mysqlUser='n_userdb'
mysqlPass='d2FVR0NA3'
mysqlDb='n_datadb'
cd "$basedir" &&
mysql -NB -u "$mysqlUser" -p"$mysqlPass" -D "$mysqlDb" \
-e 'select * from confs limit 1' >a.txt 2>a-err.txt
Ideally though, you'd use a my.cnf file to configure the username and password.
See e.g.
MySQL Utilities - ~/.my.cnf option file
mysql .my.cnf not reading credentials properly?
Do this:
result=$(mysql -u $mysqlUser -p$mysqlPass -D $mysqlDb -e "select * from confs limit 1" | grep '^\|' | tail -1)
The $() statement of Bash has trouble handling variables which contain multiple lines so the above hack greps only the interesting part: the data

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

How does MySQL take variables in bash?

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

Unix : Can't use backquote in MySQL command

I wrote a MySQL command in bash (Ubuntu) :
[XXXX:~]$ mysql -h localhost -u XXXX -pXXXX -e "DROP DATABASE IF EXISTS `f-XXXX`;"
I need backquote in this command, cause database name is variable.
That command doesn't work and it sends f-XXXX command not found
I think my problem is related to backquotes. How can I do?
You need not use backtick for variable substitution here.
[XXXX:~]$ mysql -h localhost -u XXXX -pXXXX -e "DROP DATABASE IF EXISTS ${DB};""
Bash takes the content of the backtick and runs another bash process with that as a command.
This is a backtick. Backtick is not a quotation sign, it has a very special meaning. Everything you type between backticks is evaluated (executed) by the shell before the main command (like chown in your examples), and the output of that execution is used by that command, just as if you'd type that output at that place in the command line.
Use $(commands) instead.
mysql -h localhost -u XXXX -pXXXX -e "DROP DATABASE IF EXISTS $('f-XXXX');"

Mysql XML table export command line

Maybe I'm not seeing something here but WHY would this command line work perfectly fine and provide a result set when run on a LINUX command line but when executed from a WINDOWS command line it fails dismally and returns nothing?
mysql -hHOSTNAME -uroot -p --xml -e 'SELECT * FROM db.table' > c:\temp\output.xml
What am I missing here?
Windows command line does not recognize ' as a quote character, so your statement parameter becomes SELECT (truncated at the 1st space).
You must use " instead, as Heena Hussain suggested.
Can you please try this...
C:\>mysql -u <userid> -p<password> -e "SHOW VARIABLES LIKE '%version%'" –-xml
and this...
mysql -u db_user -p db_name --xml -e "SELECT * FROM table_name" > table_name.xml