Using Shell Scripting with MySQL Causes Access Denied Message

OzBert

Bored
I'm trying to write a shell script to log into MySQL, chose the correct database and run MySQL's \. command.

The problem is that MySQL refuses the log in when running the command "mysql -uroot -pXXXX" through a script, if I type the command directly into the command line it works.

I figure that MySQL doesn't like scripts accessing it, but is there a way around it?
 
OzBert, MySQL is MADE for scripts :) That's what makes it so powerful.

Write the following two-line script and name it something like access_mysql.scr:
Code:
#!/bin/tcsh
#access_mysql.scr, executes a command as root.

mysql -uroot -pXXX -e "\. <filename> | source <filename>" database_name

Then chmod a+x access_mysql.scr and put it somewhere in $path or reference it with an absolue path (you can use ~/.. instead of absolute path to reference the script as well).

I'm not sure how you were trying to do it before, but it's always better to pass commands to applications as command-line arguments rather then trying to get the shell script to type them into the open application, if that can even be done.
 
I almost forgot, depending on what you have your script do, you can also use a reverse redirect operator to execute your script, like I'm doing right now:

mysql < create_table.sql
 
Back
Top