0

I have a script that do some controls and update a mysql entry given the result. In detail, the script calls this command

mysql --database="mydatabase" --execute="UPDATE mytable SET current_value=\"$CONTROL_RESULT\" WHERE name=\"register_name\";" 

The script works correctly when run manually from terminal.

I've added a line in /etc/rc.local to run it on startup, and in this case the script returns the error

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 

More precisely, if I run /etc/rc.local manually it works (as expected; it's the same as running the script manually); when /etc/rc.local runs on system startup I get this error.

What I've done so far is granting all privileges to root user and adding root password in /root/.my.cnf (if I access mysql with mysql -u root it doesn't ask me any password). These are the main suggestion I found in other discussions, that awfully are always related to an error when accessing mysql -u username, that works like a charm in my case.

Any idea about what can be the problem? And hopefully how to get rid of the error?

I'm working on Linux (as you've already guessed...). Here are the mysql version info

mysql> SHOW VARIABLES LIKE "%version%"; +-------------------------+-------------------------+ | Variable_name | Value | +-------------------------+-------------------------+ | innodb_version | 5.5.50 | | protocol_version | 10 | | slave_type_conversions | | | version | 5.5.50-0ubuntu0.14.04.1 | | version_comment | (Ubuntu) | | version_compile_machine | armv7l | | version_compile_os | debian-linux-gnu | +-------------------------+-------------------------+ 

1 Answer 1

0

$HOME isn't set in the environment that /etc/rc.local runs in so mysql won't find the user's .my.cnf file.

You could fix this by either explicitly setting $HOME before running your command ...

export HOME=/root 

Or explicitly set the config file to use with --defaults-file=/root/.my.cnf

1
  • Great! That was the problem. Solved with the --defaults-file option, so editing the script command call as: mysql --defaults-file=/root/.my.cnf --database="mydatabase" --execute="UPDATE mytable SET current_value=\"$CONTROL_RESULT\" WHERE name=\"register_name\";" Commented Sep 22, 2016 at 9:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.