If you set the MySQL password with mysql_config_editor, the file size of the mysqldump result is 0 only when you run the sh file via crontab.

Asked 2 years ago, Updated 2 years ago, 98 views

environment
·CentOS
·MySQL 5.6...password is set in mysql_config_editor
·Linux username...test

The following command creates the file as expected.

sh hoge.sh

▼hoge.sh

mysqldump --single-transaction-u database username database name >
/home/test/backup directory name/filename

After running the sh file via crontab, a file with a size of 0 is created and dump data cannot be retrieved
·The cron error email has not been sent either (because it is not a cron error?)

115**root/bin/bash/home/test/hoge.sh

I think it's because of permissions, but wouldn't it be possible to do anything if I run it as root?

ls-la

-rwxrwxrwx1 test test 270 January 19 14:52 2017 hoge.sh

add
·After that, I tried various things and found that if I gave the p option to mysqldump in the command described in the sh file, I could retrieve the file successfully through crontab.
·However, by using mysql_config_editor, I set it up so that I can log in without specifying a password from the command, but is it not possible to retrieve the data through crontab?
"·I tried to output a mysqldump error, so I named it ""-r file name"", but I couldn't get it

"

re-add

·I have finally confirmed the current situation (Is it a problem before cron?)
·If you run hoge.sh directly as a test user, it will work.
·When I run hoge.sh directly as the root user, the following error occurs

mysqldump:Got error:1045:Access denied for user'test'@'localhost'
(using password: NO) when trying to connect

·If you assign a password to the dump command on hoge.sh and execute it via cron (as the root user), it will work

"·If you ""select * from user;"" the mysql table, there are both root and test users."Passwords are the same

$mysql_config_editor print --all

[test]
user=test
password=*****
host=localhost
[mysqldump]
user=root
password=*****
[root]
user=root
password=*****
host=localhost

I don't know the root of it in the first place, but no matter what the permission to execute sh is to access MySQL with the database username described in the mysqldump command in sh?mymysqldump-u database username <

mysql linux shellscript sh

2022-09-30 14:44

3 Answers

mysql_config_editor saves your configuration to ~$HOME/.mylogin.cnf`

If you run mysqldump on root, you try to browse to /root/.mylogin.cnf, but this file does not exist, so I think authentication has failed.

I thought it was probably the punchline that test user set mysql_config_editor.

115 * * * root / bin / bash / home / test / hoge.sh

The sixth field in crontab says root, so hoge.sh is running with root user privileges.Try rewriting this to test.


2022-09-30 14:44

If you look closely, you can see the username...

"cron.d may specify ""root"" for the action to be performed automatically, but
" crontab is handled by the crontab running user permissions, so
You should not be able to do this with "root" permissions unless you run it with "root".

Also, crontab is created by the user who runs it for each user, but
Based on the above, if you change the permissions of the crontab file, the cron will not run.
(Security wise)

If you want to do more than one process with the permissions of various users, write it in "cron.d" or
It is recommended that you edit crontab with users who have the right to run it in the first place.


2022-09-30 14:44

In the environment running from crontab, it is HOME=/, so /root/.my.cnf or /root/.mylogin.cnf may not be able to load.
How about setting export HOME=/root on hoge.sh?


2022-09-30 14:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.