shell scripting grant command is not working in mysql - shell

I have created a shell script as given below.The aim is to create a database in a remote machine and give all privileges on that database for a specific user in remote machine.The shell script is as follows.
ssh root#10.3.2.0 'echo "db name :";
read db_name;
echo "db user :";
read db_user;
echo "user password:";
read password;
host=localhost;
sql1="create database $db_name;";
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by "${password}";";
sql3="${sql1}${sql2}";
mysql -u root -p -e "${sql3}";
'
And the output Iam getting is as foloows
root#10.3.2.0's password:
db name :
amblex
db user :
qbadmin
user password:
xxxx
Enter password: xxxx
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'qburst' at line 1
What could be the mistake in my script.PLease help me to resolve this.
Thanks in advance.

Instead of ...
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by "${password}";";
... try this ...
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by \"${password}\";";
Also you might want to consider using expect.

Related

how to connect to multiple oracle databases of different servers via shell script

I want to connect to multiple (30) oracle databases which resides in different servers (27) via shell script and fetch details from each database. I have a user (test) created on all databases but the details I want to fetch needs sysdba privileges. In my environment, I cannot provide DBA privs to any other user due to limitations. Hence my idea is to connect to each database using sqlplus -s "$user/$password#$tnsentry" and then connect as sysdba to fetch details.
Though I am able to connect to all databases using test, the "connect as sysdba" is getting executed on current database in current server.
My script:
cat tmp/db.par | while read LINE
do
if [ -n "$LINE" ] ; then
tns_entry=$LINE
export tns_entry
sqlplus -s /nolog >> $tmp/query.log <
exit
sqlconn
fi
done
In the tns_entry loop, I have given ABC,DEF,GHI,JKL,MNO databases to get details from and I am running this query from a server where database XYZ resides. I didn't give XYZ in my loop and didn't connect to database but the query results are from XYZ database. Please help me on this.
Thank you!
You should connect as sysdba
add "as sysdba" in the script and try.
conn sys/password#tnsentry as sysdba

Access Denied for User ODBC # localhost

i have installed Mysql on Windows7, i used mysql with mysql command line client & i also use Mysqldump in windows cmd, & it was working without any problem. But today, i tried to export database using mysqldump with this command in cmd
mysqldump –u root -p mypassword db_name > f:\mydb.sql
i tried many other commands and i always see error
Access Denied for User 'ODBC'#'localhost' (using password: yes) when trying to connect
as you can see, in mysqldump command i am using root as user then why i get user ODBC error ? one more thing, using mysql command line client i am still using mysql normally without any problem using root as user. i also tried to login in cmd with this command
mysql –u root -p mypassword
but still same error. and my password is 100% correct. kindly tell me how to solve this problem. Thanks
Have you tried storing pass in the cfg file? http://dev.mysql.com/doc/refman/5.7/en/password-security-user.html
try from command line:
mysql -u root -p
and then, when you are being asked for password, just type it.
Try the same with mysqldump command without typing your password after -p.

Passing variable user,pass,sid in sqlplus comman

I want to pass variables in sqlplus command in my bash script, answer might be given but doesn't work for me.
I tried some thing like this
#!/bin/bash
ssh oracle#10.116.12.26 <<XX
echo Please enter an Oracle Username:
read USERNAME
echo "Please enter the Oracle Username's Password:"
read -s PASS
SID=XE
export conn_str=$USERNAME/$PASS#$SID
sqlplus $conn_str << EOF
select * FROM tabs;
exit
EOF
XX
also tried
sqlplus $USERNAME/$PASS#SID #with option -s and -l
I also find solution like this
but not worked for me.
oracle : 11g
,os : fedora 18
Is there any solution available for this ?
thanks.
You haven't said what actually happens, but I'm guessing you aren't prompted for the credentials, and that maybe it can't find sqlplus. On a Red Hat box this works:
#!/bin/bash
echo Please enter an Oracle Username:
read USERNAME
echo "Please enter the Oracle Username's Password:"
read -s PASS
SID=XE
conn_str=$USERNAME/$PASS#$SID
ssh oracle#10.116.12.26 << EOF
# set these for your specific environment
ORACLE_HOME=<path to ORACLE_HOME>
PATH=$PATH:$ORACLE_HOME/bin # or without .../bin depending on client
TNS_ADMIN=<path to tnsnames.ora directory, if not default>
sqlplus -s /nolog
connect $conn_str
select * FROM user_tables;
exit
EOF
This is collecting the values from the user on the local machine to avoid the 'Pseudo-terminal will not be allocated because stdin is not a terminal' issue.
It is then setting up the Oracle environment once on the remote server - what you need to set depends on which client you're using (particularly whether you're using the instant client, though if you're connecting as oracle that seems unlikely, and you can probably run oraenv).
I've also tweaked it to run SQL*Plus with /nolog and then connect once running, so the credentials aren't exposed in the ps output. And I've switched from the old tabs to the more common user_tables.
The nested heredocs work but aren't necessary; the SQL commands are already being entered in the right place and will be seen by SQL*Plus rather than the remote shell.
Of course, since I don't know what error you were actually seeing this is largely speculation. It would probably be simpler to have the client installed locally and use a SQL*Net connection rather than SSH, but there could be firewall restrictions we don't know about.
Try quoting your here doc delimiter to prevent stale variable expansions on the local machine.
ssh oracle#10.116.12.26 <<'XX'

grant command is not working in shell script because of syntax error

I am stuck with an issue with shell scripting.
I have already used \ character before double quotes (") because of some reason, as in the following line of my shell script.
sql2=\"grant all on \${db_name}.* to \${db_user}#\${host} identified by \${dbpass};\";
But as in the MySQL "grant" command syntax, I need to put double quotes before and after \${dbpass}, which is the part of the shell script line I given above.When I put like this, \"\${dbpass}\", it is throwing syntax error during execution.What should I need to solve this.
This is an update.
This is the shell script which I want to run.
ssh -t qbadmin#10.3.2.0 '
su root -c "
echo \"Give db name :\";
read db_name;
echo \"Give password :\";
read db_pass;
host=localhost;
sql1=\"create database \$db_name;\";
sql2=\"grant all on \${db_name}.* to \${db_name}#\${host} identified by \"\${db_pass}\";\";
sql3=\"\${sql1}\${sql2}\";
echo \"==============\";
mysql -u root -p -e \"\${sql3}\";
";
'
Please refer this script and please let me know the necessary changes i need to do with this.
Thanks.
Try this
db_name="yourdbname"
db_user="yourdbuser"
host="yourhost"
dbpass="yourdbpass"
sql2="grant all on ${db_name}.* to ${db_user}#${host} identified by ${dbpass};"
echo $sql2 | mysql -u yourusername -pmysql

grant command syntax error near password area in shell script

I have created a shell script like this.But getting syntax error in grant command, I think near password area.Please help me if anyone could.
ssh -t qbadmin#10.3.2.0 '
su root -c "
echo \"Give db name :\";
read db_name;
echo \"Give password :\";
read db_pass;
host=localhost;
sql1=\"create database \$db_name;\";
sql2=\"grant all on \${db_name}.* to \${db_name}#\${host} identified by \"\${db_pass}\";\";
sql3=\"\${sql1}\${sql2}\";
mysql -u root -p -e \"\${sql3}\";
";
'
The database has created in the remote machine successfully, but the grant command returned error..! My guess is the error might be because of the usage of \" character before and after ${db_pass} in the grant command.Please do help me to solve this.
Thanks.
Nested quoting is always tricky. Why don't you just ssh to the remote host as root?
ssh -t root#10.3.2.0 '
echo "Give db name :"
read db_name;
echo "Give password :"
read db_pass
host=localhost
sql1="create database \$db_name;"
sql2="grant all on ${db_name}.* to ${db_name}#${host} identified by ${db_pass};"
sql3="${sql1}${sql2}"
mysql -p -e "${sql3}"
'

Resources